@@ -17,7 +17,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717-}
1818{-# LANGUAGE FlexibleContexts #-}
1919{- |
20− Module : Text.Pandoc.Lua.PandocModule
20+ Module : Text.Pandoc.Lua.Module.Pandoc
2121 Copyright : Copyright © 2017 Albert Krewinkel
2222 License : GNU GPL, version 2 or above
2323
@@ -26,8 +26,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2626
2727Pandoc module for lua.
2828-}
29−module Text.Pandoc.Lua.PandocModule
30− ( pushPandocModule
29+module Text.Pandoc.Lua.Module.Pandoc
30+ ( pushModule
3131 , pushMediaBagModule
3232 ) where
3333
@@ -44,7 +44,8 @@ import Text.Pandoc.Class (CommonState (..), fetchItem, putCommonState,
4444import Text.Pandoc.Definition (Block, Inline)
4545import Text.Pandoc.Lua.Filter (walkInlines, walkBlocks, LuaFilter)
4646import Text.Pandoc.Lua.StackInstances ()
47−import Text.Pandoc.Lua.Util (addFunction, addValue, loadScriptFromDataDir)
47+import Text.Pandoc.Lua.Util (OrNil (toMaybe), addFunction, addValue,
48+ loadScriptFromDataDir, raiseError)
4849import Text.Pandoc.Walk (Walkable)
4950import Text.Pandoc.MIME (MimeType)
5051import Text.Pandoc.Options (ReaderOptions (readerExtensions))
@@ -58,8 +59,8 @@ import qualified Text.Pandoc.MediaBag as MB
5859
5960-- | Push the "pandoc" on the lua stack. Requires the `list` module to be
6061-- loaded.
61−pushPandocModule :: Maybe FilePath -> Lua NumResults
62−pushPandocModule datadir = do
62+pushModule :: Maybe FilePath -> Lua NumResults
63+pushModule datadir = do
6364 loadScriptFromDataDir datadir "pandoc.lua"
6465 addFunction "read" readDoc
6566 addFunction "pipe" pipeFn
@@ -91,10 +92,6 @@ readDoc content formatSpecOrNil = do
9192 Right pd -> (1 :: NumResults) <$ Lua.push pd -- success, push Pandoc
9293 Left s -> raiseError (show s) -- error while reading
9394 _ -> raiseError "Only string formats are supported at the moment."
94− where
95− raiseError s = do
96− Lua.push s
97− fromIntegral <$> Lua.lerror
9895
9996--
10097-- MediaBag submodule
@@ -122,12 +119,8 @@ pipeFn :: String
122119pipeFn command args input = do
123120 (ec, output) <- liftIO $ pipeProcess Nothing command args input
124121 case ec of
125− ExitSuccess -> do
126− Lua.push output
127− return 1
128− ExitFailure n -> do
129− Lua.push (PipeError command n output)
130− fromIntegral <$> Lua.lerror
122+ ExitSuccess -> 1 <$ Lua.push output
123+ ExitFailure n -> raiseError (PipeError command n output)
131124
132125data PipeError = PipeError
133126 { pipeErrorCommand :: String
@@ -218,16 +211,3 @@ fetch commonState mbRef src = do
218211 Lua.push $ fromMaybe "" mimeType
219212 Lua.push bs
220213 return 2 -- returns 2 values: contents, mimetype
221−
222−--
223−-- Helper types
224−--
225−
226−newtype OrNil a = OrNil { toMaybe :: Maybe a }
227−
228−instance FromLuaStack a => FromLuaStack (OrNil a) where
229− peek idx = do
230− noValue <- Lua.isnoneornil idx
231− if noValue
232− then return (OrNil Nothing)
233− else OrNil . Just <$> Lua.peek idx