diff options
author | Albert Krewinkel <albert+github@zeitkraut.de> | 2017-11-29 01:20:01 +0100 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-11-28 17:20:01 -0700 |
commit | 0105a3c2930675971ea83da3b987c6b62f97ce26 (patch) | |
tree | 16221c1f6e610118a721f5b072464a9c48adb161 /src/Text/Pandoc | |
parent | 5a225aa6037621209d41927e24d682389d719f50 (diff) |
Add basic lua List module (#4099)
The List module is automatically loaded, but not assigned to a global
variable. It can be included in filters by calling `List = require
'List'`.
Lists of blocks, lists of inlines, and lists of classes are now given
`List` as a metatable, making working with them more convenient. E.g.,
it is now possible to concatenate lists of inlines using Lua's
concatenation operator `..` (requires at least one of the operants to
have `List` as a metatable):
function Emph (emph)
local s = {pandoc.Space(), pandoc.Str 'emphasized'}
return pandoc.Span(emph.content .. s)
end
Closes: #4081
Diffstat (limited to 'src/Text/Pandoc')
-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 |