ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* slashes in paths (Lua code)
@ 2017-05-31 18:51 Pablo Rodriguez
  2017-05-31 20:00 ` Hans Hagen
  0 siblings, 1 reply; 5+ messages in thread
From: Pablo Rodriguez @ 2017-05-31 18:51 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Dear list,

I need to add checksums to files I attach to PDF documents.

Following the suggestion from https://stackoverflow.com/a/326715, I wrote:

    \starttext
    \startluacode
    function document.capture(cmd, raw)
        local f = assert(io.popen(cmd, 'r'))
        local s = assert(f:read('*a'))
        f:close()
        if raw then return s end
        s = string.gsub(s, '^%s+', '')
        s = string.gsub(s, '%s+$', '')
        s = string.gsub(s, '[\n\r]+', ' ')
        return s
    end

    function document.sha256(file)
        command_output= document.capture("sha256sum -b " .. file)
        context(command_output:sub(0,64))
    end

    function document.sha512(file)
        command_output= document.capture("sha512sum -b " .. file)
        context(command_output:sub(0,128))
    end
    \stopluacode

    \def\shatwo#1%
        {\ctxlua{document.sha256("#1")}}

    \def\shafive#1%
        {\ctxlua{document.sha512("#1")}}

    \shatwo{Desktop/i-context.pdf}

    \shafive{i-context.pdf}
    \stoptext

Directories are a problem here. I mean, \shatwo{Desktop/i-context.pdf}
cannot work in Windows.

Which would be the way to make it work in Windows?

Many thanks for your help,

Pablo
-- 
http://www.ousia.tk
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: slashes in paths (Lua code)
  2017-05-31 18:51 slashes in paths (Lua code) Pablo Rodriguez
@ 2017-05-31 20:00 ` Hans Hagen
  2017-06-01 16:13   ` Pablo Rodriguez
  0 siblings, 1 reply; 5+ messages in thread
From: Hans Hagen @ 2017-05-31 20:00 UTC (permalink / raw)
  To: ntg-context

On 5/31/2017 8:51 PM, Pablo Rodriguez wrote:
> Dear list,
> 
> I need to add checksums to files I attach to PDF documents.
> 
> Following the suggestion from https://stackoverflow.com/a/326715, I wrote:
> 
>      \starttext
>      \startluacode
>      function document.capture(cmd, raw)
>          local f = assert(io.popen(cmd, 'r'))
>          local s = assert(f:read('*a'))
>          f:close()
>          if raw then return s end
>          s = string.gsub(s, '^%s+', '')
>          s = string.gsub(s, '%s+$', '')
>          s = string.gsub(s, '[\n\r]+', ' ')
>          return s
>      end
> 
>      function document.sha256(file)
>          command_output= document.capture("sha256sum -b " .. file)
>          context(command_output:sub(0,64))
>      end
> 
>      function document.sha512(file)
>          command_output= document.capture("sha512sum -b " .. file)
>          context(command_output:sub(0,128))
>      end
>      \stopluacode
> 
>      \def\shatwo#1%
>          {\ctxlua{document.sha256("#1")}}
> 
>      \def\shafive#1%
>          {\ctxlua{document.sha512("#1")}}
> 
>      \shatwo{Desktop/i-context.pdf}
> 
>      \shafive{i-context.pdf}
>      \stoptext
> 
> Directories are a problem here. I mean, \shatwo{Desktop/i-context.pdf}
> cannot work in Windows.
> 
> Which would be the way to make it work in Windows?
\starttext
     \startluacode
     local function sha(cmd,name)
         local f = io.popen(string.format("%s -b %q",cmd,name),"rb")
         if f then
             local s = f:read("*a")
             f:close()
          -- s = string.longtostring(s)
             return string.match(s,"%s*([A-Fa-f0-9]+)")
         end
     end

     function document.sha256(name)
         local ok = sha("sha256sum",name)
         if ok then
             context(ok)
         end
     end

     function document.sha512(name)
         local ok = sha("sha512sum",name)
         if ok then
             context(ok)
         end
     end
\stopluacode

\def\shatwo #1{\ctxlua{document.sha256("#1")}}
\def\shafive#1{\ctxlua{document.sha512("#1")}}

\shafive{fd.pdf}
\shafive{foo/fd.pdf}

\stoptext


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: slashes in paths (Lua code)
  2017-05-31 20:00 ` Hans Hagen
@ 2017-06-01 16:13   ` Pablo Rodriguez
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Rodriguez @ 2017-06-01 16:13 UTC (permalink / raw)
  To: ntg-context

On 05/31/2017 10:00 PM, Hans Hagen wrote:
> On 5/31/2017 8:51 PM, Pablo Rodriguez wrote:
>> Dear list,
>>
>> I need to add checksums to files I attach to PDF documents.
>> [...]
>> Directories are a problem here. I mean, \shatwo{Desktop/i-context.pdf}
>> cannot work in Windows.
>>
>> Which would be the way to make it work in Windows?

Many thanks for your code, Hans.

Pablo

> \starttext
>      \startluacode
>      local function sha(cmd,name)
>          local f = io.popen(string.format("%s -b %q",cmd,name),"rb")
>          if f then
>              local s = f:read("*a")
>              f:close()
>           -- s = string.longtostring(s)
>              return string.match(s,"%s*([A-Fa-f0-9]+)")
>          end
>      end
> 
>      function document.sha256(name)
>          local ok = sha("sha256sum",name)
>          if ok then
>              context(ok)
>          end
>      end
> 
>      function document.sha512(name)
>          local ok = sha("sha512sum",name)
>          if ok then
>              context(ok)
>          end
>      end
> \stopluacode
> 
> \def\shatwo #1{\ctxlua{document.sha256("#1")}}
> \def\shafive#1{\ctxlua{document.sha512("#1")}}
> 
> \shafive{fd.pdf}
> \shafive{foo/fd.pdf}
> 
> \stoptext


-- 
http://www.ousia.tk
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: slashes in paths (Lua code)
  2017-06-01  3:47 Akira Kakuto
@ 2017-06-01 16:26 ` Pablo Rodriguez
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Rodriguez @ 2017-06-01 16:26 UTC (permalink / raw)
  To: ntg-context

On 06/01/2017 05:47 AM, Akira Kakuto wrote:
>> Directories are a problem here. I mean, 
>> \shatwo{Desktop/i-context.pdf}
>> cannot work in Windows.
> 
> Here both of your example and an example by Hans
> worked fine on Windows.
> Note that
> \shatwo{Desktop/i-context.pdf}
> works only if the current directory is your %USERPROFILE%.

Many thanks for your reply, Akira.

I didn’t check my code on Windows (all my computers at home run Linux).
I’m suprised that it worked.

And thanks for warning about the path and %USERPROFILE%. Of course,
ConTeXt and LuaTeX do magic, but organizing files is a task that belongs
to the user ;-).

Many thanks for your help,

Pablo
-- 
http://www.ousia.tk
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* slashes in paths (Lua code)
@ 2017-06-01  3:47 Akira Kakuto
  2017-06-01 16:26 ` Pablo Rodriguez
  0 siblings, 1 reply; 5+ messages in thread
From: Akira Kakuto @ 2017-06-01  3:47 UTC (permalink / raw)
  To: ntg-context

> Directories are a problem here. I mean, 
> \shatwo{Desktop/i-context.pdf}
> cannot work in Windows.

Here both of your example and an example by Hans
worked fine on Windows.
Note that
\shatwo{Desktop/i-context.pdf}
works only if the current directory is your %USERPROFILE%.

Best,
Akira

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-06-01 16:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-31 18:51 slashes in paths (Lua code) Pablo Rodriguez
2017-05-31 20:00 ` Hans Hagen
2017-06-01 16:13   ` Pablo Rodriguez
2017-06-01  3:47 Akira Kakuto
2017-06-01 16:26 ` Pablo Rodriguez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).