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 -----------------------------------------------------------------