From 0105a3c2930675971ea83da3b987c6b62f97ce26 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Wed, 29 Nov 2017 01:20:01 +0100 Subject: 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 --- src/Text/Pandoc/Lua/PandocModule.hs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/Text/Pandoc/Lua') 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 -- cgit v1.2.3