Lua: added 'pipe', which encapsulates Text.Pandoc.Process.pipeProcess.

This is hard to do in lua, so it's helpful to provide this.
This commit is contained in:
John MacFarlane 2017-10-01 15:23:20 -07:00
parent 4c3b3bf65a
commit 3e77ea4792
2 changed files with 31 additions and 0 deletions

View file

@ -1075,6 +1075,20 @@ Lua functions for pandoc scripts.
local fp = pandoc.mediabag.sha1("foobar")
[`pipe (command, args, input)`]{#mediabag-sha1}
: Runs command with arguments, passing it some input,
and returns the exit code and the output.
Returns:
- Exit code from command.
- Output of command.
Usage:
local ec, output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")
# Submodule mediabag

View file

@ -48,6 +48,8 @@ import Text.Pandoc.Options (ReaderOptions(readerExtensions))
import Text.Pandoc.Lua.StackInstances ()
import Text.Pandoc.Readers (Reader (..), getReader)
import Text.Pandoc.MIME (MimeType)
import Text.Pandoc.Process (pipeProcess)
import System.Exit (ExitCode(..))
import Data.Digest.Pure.SHA (sha1, showDigest)
import qualified Foreign.Lua as Lua
@ -66,6 +68,9 @@ pushPandocModule datadir = do
Lua.push "sha1"
Lua.pushHaskellFunction sha1HashFn
Lua.rawset (-3)
Lua.push "pipe"
Lua.pushHaskellFunction pipeFn
Lua.rawset (-3)
-- | Get the string representation of the pandoc module
pandocModuleScript :: Maybe FilePath -> IO String
@ -109,6 +114,18 @@ sha1HashFn contents = do
Lua.push $ showDigest (sha1 contents)
return 1
pipeFn :: String
-> [String]
-> BL.ByteString
-> Lua NumResults
pipeFn command args input = do
(ec, output) <- liftIO $ pipeProcess Nothing command args input
Lua.push $ case ec of
ExitSuccess -> 0
ExitFailure n -> n
Lua.push output
return 2
insertMediaFn :: IORef MB.MediaBag
-> FilePath
-> OrNil MimeType