aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2017-12-06 20:45:38 +0100
committerAlbert Krewinkel <albert@zeitkraut.de>2017-12-06 22:50:56 +0100
commit4066a385ace1cee53336bf4c10734239044a92ae (patch)
tree1ed8d3bb1201f298840c53ae8e72bf4afa8f8ad9 /src/Text
parentd5b1c7b767a24bda592ea35902b8e1dc971d6d80 (diff)
downloadpandoc-4066a385ace1cee53336bf4c10734239044a92ae.tar.gz
Lua filters: use script to initialize the interpreter
The file `init.lua` is used to initialize the Lua interpreter which is used in Lua filters. This gives users the option to require libraries which they want to use in all of their filters, and to extend default modules.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Lua.hs15
-rw-r--r--src/Text/Pandoc/Lua/PandocModule.hs7
2 files changed, 9 insertions, 13 deletions
diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs
index 1ca67dced..7132ad718 100644
--- a/src/Text/Pandoc/Lua.hs
+++ b/src/Text/Pandoc/Lua.hs
@@ -48,10 +48,11 @@ import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
Status (OK), ToLuaStack (push))
import Text.Pandoc.Class (PandocIO, getCommonState, getMediaBag, setMediaBag)
import Text.Pandoc.Definition
+import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
installPandocPackageSearcher)
-import Text.Pandoc.Lua.PandocModule (pushMediaBagModule, pushPandocModule)
-import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
+import Text.Pandoc.Lua.PandocModule (pushPandocModule) -- TODO: remove
+import Text.Pandoc.Lua.Util (loadScriptFromDataDir)
import qualified Foreign.Lua as Lua
import qualified Foreign.Lua.Module.Text as Lua
@@ -101,17 +102,11 @@ luaPackageParams datadir = do
-- Initialize the lua state with all required values
initLuaState :: LuaPackageParams -> Lua ()
-initLuaState luaPkgParams@(LuaPackageParams commonState datadir mbRef) = do
+initLuaState luaPkgParams = do
Lua.openlibs
Lua.preloadTextModule "text"
installPandocPackageSearcher luaPkgParams
- pushPandocModule datadir
- -- add MediaBag module
- push "mediabag"
- pushMediaBagModule commonState mbRef
- Lua.rawset (-3)
- Lua.setglobal "pandoc"
- return ()
+ loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
pushGlobalFilter :: Lua ()
pushGlobalFilter = do
diff --git a/src/Text/Pandoc/Lua/PandocModule.hs b/src/Text/Pandoc/Lua/PandocModule.hs
index 744edfe82..75f884c46 100644
--- a/src/Text/Pandoc/Lua/PandocModule.hs
+++ b/src/Text/Pandoc/Lua/PandocModule.hs
@@ -60,7 +60,7 @@ import qualified Text.Pandoc.MediaBag as MB
-- | Push the "pandoc" on the lua stack. Requires the `list` module to be
-- loaded.
-pushPandocModule :: Maybe FilePath -> Lua ()
+pushPandocModule :: Maybe FilePath -> Lua NumResults
pushPandocModule datadir = do
loadScriptFromDataDir datadir "pandoc.lua"
addFunction "_pipe" pipeFn
@@ -68,6 +68,7 @@ pushPandocModule datadir = do
addFunction "sha1" sha1HashFn
addFunction "walk_block" walkBlock
addFunction "walk_inline" walkInline
+ return 1
walkElement :: (ToLuaStack a, Walkable [Inline] a, Walkable [Block] a)
=> a -> LuaFilter -> Lua NumResults
@@ -99,14 +100,14 @@ readDoc formatSpec content = do
--
-- MediaBag submodule
--
-pushMediaBagModule :: CommonState -> IORef MB.MediaBag -> Lua ()
+pushMediaBagModule :: CommonState -> IORef MB.MediaBag -> Lua NumResults
pushMediaBagModule commonState mediaBagRef = do
Lua.newtable
addFunction "insert" (insertMediaFn mediaBagRef)
addFunction "lookup" (lookupMediaFn mediaBagRef)
addFunction "list" (mediaDirectoryFn mediaBagRef)
addFunction "fetch" (fetch commonState mediaBagRef)
- return ()
+ return 1
addFunction :: ToHaskellFunction a => String -> a -> Lua ()
addFunction name fn = do