git.iko.soy/typeable/pandoc
on master help sign in
Lua: added 'pipe', which encapsulates Text.Pandoc.Process.pipeProcess.
3e77ea4John MacFarlane
doc/lua-filters.md+14
@@ -1075,6 +1075,20 @@ Lua functions for pandoc scripts.
1075
1075
1076 local fp = pandoc.mediabag.sha1("foobar")
1076 local fp = pandoc.mediabag.sha1("foobar")
1077
1077
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+
1078
1092
1079# Submodule mediabag
1093# Submodule mediabag
1080
1094
src/Text/Pandoc/Lua/PandocModule.hs+17
@@ -48,6 +48,8 @@ import Text.Pandoc.Options (ReaderOptions(readerExtensions))
48import Text.Pandoc.Lua.StackInstances ()
48import Text.Pandoc.Lua.StackInstances ()
49import Text.Pandoc.Readers (Reader (..), getReader)
49import Text.Pandoc.Readers (Reader (..), getReader)
50import Text.Pandoc.MIME (MimeType)
50import Text.Pandoc.MIME (MimeType)
51+import Text.Pandoc.Process (pipeProcess)
52+import System.Exit (ExitCode(..))
51import Data.Digest.Pure.SHA (sha1, showDigest)
53import Data.Digest.Pure.SHA (sha1, showDigest)
52
54
53import qualified Foreign.Lua as Lua
55import qualified Foreign.Lua as Lua
@@ -66,6 +68,9 @@ pushPandocModule datadir = do
66 Lua.push "sha1"
68 Lua.push "sha1"
67 Lua.pushHaskellFunction sha1HashFn
69 Lua.pushHaskellFunction sha1HashFn
68 Lua.rawset (-3)
70 Lua.rawset (-3)
71+ Lua.push "pipe"
72+ Lua.pushHaskellFunction pipeFn
73+ Lua.rawset (-3)
69
74
70-- | Get the string representation of the pandoc module
75-- | Get the string representation of the pandoc module
71pandocModuleScript :: Maybe FilePath -> IO String
76pandocModuleScript :: Maybe FilePath -> IO String
@@ -109,6 +114,18 @@ sha1HashFn contents = do
109 Lua.push $ showDigest (sha1 contents)
114 Lua.push $ showDigest (sha1 contents)
110 return 1
115 return 1
111
116
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+
112insertMediaFn :: IORef MB.MediaBag
129insertMediaFn :: IORef MB.MediaBag
113 -> FilePath
130 -> FilePath
114 -> OrNil MimeType
131 -> OrNil MimeType