git.iko.soy/typeable/pandoc
on master help sign in
Lua: added 'pipe', which encapsulates Text.Pandoc.Process.pipeProcess.

This is hard to do in lua, so it's helpful to provide this.

commit3e77ea4792979879a80e67f20712766e4af2fdf5 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 1 Oct 2017, 15:23 (9 years ago) parent4c3b3bf
2 files changed+31
doc/lua-filters.md+14 src/Text/Pandoc/Lua/PandocModule.hs+17
doc/lua-filters.md+14
@@ -1075,6 +1075,20 @@ Lua functions for pandoc scripts.
10751075
10761076 local fp = pandoc.mediabag.sha1("foobar")
10771077
1078+[`pipe (command, args, input)`]{#mediabag-sha1}
1079+
1080+: Runs command with arguments, passing it some input,
1081+ and returns the exit code and the output.
1082+
1083+ Returns:
1084+
1085+ - Exit code from command.
1086+ - Output of command.
1087+
1088+ Usage:
1089+
1090+ local ec, output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")
1091+
10781092
10791093# Submodule mediabag
10801094
src/Text/Pandoc/Lua/PandocModule.hs+17
@@ -48,6 +48,8 @@ import Text.Pandoc.Options (ReaderOptions(readerExtensions))
4848import Text.Pandoc.Lua.StackInstances ()
4949import Text.Pandoc.Readers (Reader (..), getReader)
5050import Text.Pandoc.MIME (MimeType)
51+import Text.Pandoc.Process (pipeProcess)
52+import System.Exit (ExitCode(..))
5153import Data.Digest.Pure.SHA (sha1, showDigest)
5254
5355import qualified Foreign.Lua as Lua
@@ -66,6 +68,9 @@ pushPandocModule datadir = do
6668 Lua.push "sha1"
6769 Lua.pushHaskellFunction sha1HashFn
6870 Lua.rawset (-3)
71+ Lua.push "pipe"
72+ Lua.pushHaskellFunction pipeFn
73+ Lua.rawset (-3)
6974
7075-- | Get the string representation of the pandoc module
7176pandocModuleScript :: Maybe FilePath -> IO String
@@ -109,6 +114,18 @@ sha1HashFn contents = do
109114 Lua.push $ showDigest (sha1 contents)
110115 return 1
111116
117+pipeFn :: String
118+ -> [String]
119+ -> BL.ByteString
120+ -> Lua NumResults
121+pipeFn command args input = do
122+ (ec, output) <- liftIO $ pipeProcess Nothing command args input
123+ Lua.push $ case ec of
124+ ExitSuccess -> 0
125+ ExitFailure n -> n
126+ Lua.push output
127+ return 2
128+
112129insertMediaFn :: IORef MB.MediaBag
113130 -> FilePath
114131 -> OrNil MimeType