diff options
Diffstat (limited to 'src/Text/Pandoc/Lua')
-rw-r--r-- | src/Text/Pandoc/Lua/PandocModule.hs | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/Text/Pandoc/Lua/PandocModule.hs b/src/Text/Pandoc/Lua/PandocModule.hs index ac7839d0f..ba3193211 100644 --- a/src/Text/Pandoc/Lua/PandocModule.hs +++ b/src/Text/Pandoc/Lua/PandocModule.hs @@ -59,10 +59,12 @@ import qualified Foreign.Lua as Lua import qualified Text.Pandoc.MediaBag as MB import Text.Pandoc.Lua.Filter (walkInlines, walkBlocks, LuaFilter) --- | Push the "pandoc" on the lua stack. +-- | Push the "pandoc" on the lua stack. Requires the `list` module to be +-- loaded. pushPandocModule :: Maybe FilePath -> Lua () pushPandocModule datadir = do - script <- liftIO (pandocModuleScript datadir) + loadListModule datadir + script <- liftIO (moduleScript datadir "pandoc.lua") status <- Lua.loadstring script unless (status /= Lua.OK) $ Lua.call 0 1 addFunction "_pipe" pipeFn @@ -72,9 +74,25 @@ pushPandocModule datadir = do addFunction "walk_inline" walkInline -- | Get the string representation of the pandoc module -pandocModuleScript :: Maybe FilePath -> IO String -pandocModuleScript datadir = unpack <$> - runIOorExplode (setUserDataDir datadir >> readDataFile "pandoc.lua") +moduleScript :: Maybe FilePath -> FilePath -> IO String +moduleScript datadir moduleFile = unpack <$> + runIOorExplode (setUserDataDir datadir >> readDataFile moduleFile) + +-- Loads pandoc's list module without assigning it to a variable. +pushListModule :: Maybe FilePath -> Lua () +pushListModule datadir = do + script <- liftIO (moduleScript datadir "List.lua") + status <- Lua.loadstring script + if status == Lua.OK + then Lua.call 0 1 + else Lua.throwTopMessageAsError' ("Error while loading module `list`\n" ++) + +loadListModule :: Maybe FilePath -> Lua () +loadListModule datadir = do + Lua.getglobal' "package.loaded" + pushListModule datadir + Lua.setfield (-2) "List" + Lua.pop 1 walkElement :: (ToLuaStack a, Walkable [Inline] a, Walkable [Block] a) => a -> LuaFilter -> Lua NumResults |