diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2017-10-03 20:45:11 +0200 |
---|---|---|
committer | Albert Krewinkel <albert@zeitkraut.de> | 2017-10-03 20:45:11 +0200 |
commit | 371f9b708478700992a74864985cfea0af2fd4c3 (patch) | |
tree | 0eb4c00af3079b2342ae452ec285cd28f5155944 | |
parent | 9b750f7d879b1da386e49ecfd51ef9d023dc5d66 (diff) | |
download | pandoc-371f9b708478700992a74864985cfea0af2fd4c3.tar.gz |
pandoc.lua: use wrapper funciton for pipe command
The pipe command is wrapped in a lua function, throwing a lua error if
the command returns with an error. A wrapper is needed as Haskell
functions exposed to lua may not throw lua errors due to limitations of
hslua.
The error handling is written such that a table can be returned as an
error object in the future. This is potentially useful when finer
control is required while catching the error in lua code. Current
limitations of hslua require error objects to be strings.
-rw-r--r-- | data/pandoc.lua | 23 | ||||
-rw-r--r-- | src/Text/Pandoc/Lua/PandocModule.hs | 2 |
2 files changed, 24 insertions, 1 deletions
diff --git a/data/pandoc.lua b/data/pandoc.lua index e6cfbc90c..fc83103e0 100644 --- a/data/pandoc.lua +++ b/data/pandoc.lua @@ -790,6 +790,29 @@ function M.read(markup, format) end end +--- Runs command with arguments, passing it some input, and returns the output. +-- @treturn string Output of command. +-- @usage +-- local ec, output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc") +function M.pipe (command, args, input) + local ec, output = pandoc._pipe(command, args, input) + if ec ~= 0 then + err = setmetatable( + { command = command, error_code = ec, output = output}, + { __tostring = function(e) + return "Error running " .. e.command + .. " (error code " .. e.error_code .. "): " + .. e.output + end + } + ) + -- TODO: drop the wrapping call to `tostring` as soon as hslua supports + -- non-string error objects. + error(tostring(err)) + end + return output +end + --- Use functions defined in the global namespace to create a pandoc filter. -- All globally defined functions which have names of pandoc elements are -- collected into a new table. diff --git a/src/Text/Pandoc/Lua/PandocModule.hs b/src/Text/Pandoc/Lua/PandocModule.hs index c689edc4e..f9b072dff 100644 --- a/src/Text/Pandoc/Lua/PandocModule.hs +++ b/src/Text/Pandoc/Lua/PandocModule.hs @@ -63,7 +63,7 @@ pushPandocModule datadir = do script <- liftIO (pandocModuleScript datadir) status <- Lua.loadstring script unless (status /= Lua.OK) $ Lua.call 0 1 - addFunction "pipe" pipeFn + addFunction "_pipe" pipeFn addFunction "_read" readDoc addFunction "sha1" sha1HashFn |