git.iko.soy/typeable/pandoc
on master help sign in
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.

commit4066a385ace1cee53336bf4c10734239044a92ae authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 6 Dec 2017, 20:45 (9 years ago) parentd5b1c7b
5 files changed+46−14
data/init.lua+6 doc/lua-filters.md+29−1 pandoc.cabal+2 src/Text/Pandoc/Lua.hs+5−10 src/Text/Pandoc/Lua/PandocModule.hs+4−3
data/init.luanew file+6
@@ -0,0 +1,6 @@
1+-- This Lua script is run every time the Lua interpreter is started when running
2+-- a Lua filter. It can be customized to load additional modules or to alter the
3+-- default modules.
4+
5+pandoc = require 'pandoc'
6+pandoc.mediabag = require 'pandoc.mediabag'
doc/lua-filters.md+29−1
@@ -3,7 +3,7 @@ title: Pandoc Lua Filters
33author:
44- Albert Krewinkel
55- John MacFarlane
6−date: 'November 20, 2017'
6+date: 'December 6, 2017'
77---
88
99# Introduction
@@ -176,6 +176,34 @@ Some pandoc functions have been made available in lua:
176176 which stores binary content such as images that may be
177177 included in the final document.
178178
179+# Lua interpreter initialization
180+
181+The way the Lua interpreter is set-up can be controlled by
182+placing a file `init.lua` in pandoc's data directory. The
183+default init file loads the `pandoc` and `pandoc.mediabag`
184+modules:
185+
186+``` lua
187+pandoc = require 'pandoc'
188+pandoc.mediabag = require 'pandoc.mediabag'
189+```
190+
191+A common use-case would be to add code to load additional
192+modules or to alter default modules. E.g., the following snippet
193+adds all unicode-aware functions defined in the [`text`
194+module](#module-text) to the default `string` module, prefixed
195+with the string `uc_`.
196+
197+```lua
198+for name, fn in pairs(require 'text') do
199+ string['uc_' .. name] = fn
200+end
201+```
202+
203+This makes it possible to apply these functions on strings using
204+colon syntax (`mystring:uc_upper()`).
205+
206+
179207# Examples
180208
181209## Macro substitution.
pandoc.cabal+2
@@ -108,6 +108,8 @@ data-files:
108108 data/abbreviations
109109 -- sample lua custom writer
110110 data/sample.lua
111+ -- lua init script
112+ data/init.lua
111113 -- pandoc lua module
112114 data/pandoc.lua
113115 -- lua List module
src/Text/Pandoc/Lua.hs+5−10
@@ -48,10 +48,11 @@ import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
4848 Status (OK), ToLuaStack (push))
4949import Text.Pandoc.Class (PandocIO, getCommonState, getMediaBag, setMediaBag)
5050import Text.Pandoc.Definition
51+import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
5152import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
5253 installPandocPackageSearcher)
53−import Text.Pandoc.Lua.PandocModule (pushMediaBagModule, pushPandocModule)
54−import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
54+import Text.Pandoc.Lua.PandocModule (pushPandocModule) -- TODO: remove
55+import Text.Pandoc.Lua.Util (loadScriptFromDataDir)
5556import qualified Foreign.Lua as Lua
5657import qualified Foreign.Lua.Module.Text as Lua
5758
@@ -101,17 +102,11 @@ luaPackageParams datadir = do
101102
102103-- Initialize the lua state with all required values
103104initLuaState :: LuaPackageParams -> Lua ()
104−initLuaState luaPkgParams@(LuaPackageParams commonState datadir mbRef) = do
105+initLuaState luaPkgParams = do
105106 Lua.openlibs
106107 Lua.preloadTextModule "text"
107108 installPandocPackageSearcher luaPkgParams
108− pushPandocModule datadir
109− -- add MediaBag module
110− push "mediabag"
111− pushMediaBagModule commonState mbRef
112− Lua.rawset (-3)
113− Lua.setglobal "pandoc"
114− return ()
109+ loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
115110
116111pushGlobalFilter :: Lua ()
117112pushGlobalFilter = do
src/Text/Pandoc/Lua/PandocModule.hs+4−3
@@ -60,7 +60,7 @@ import qualified Text.Pandoc.MediaBag as MB
6060
6161-- | Push the "pandoc" on the lua stack. Requires the `list` module to be
6262-- loaded.
63−pushPandocModule :: Maybe FilePath -> Lua ()
63+pushPandocModule :: Maybe FilePath -> Lua NumResults
6464pushPandocModule datadir = do
6565 loadScriptFromDataDir datadir "pandoc.lua"
6666 addFunction "_pipe" pipeFn
@@ -68,6 +68,7 @@ pushPandocModule datadir = do
6868 addFunction "sha1" sha1HashFn
6969 addFunction "walk_block" walkBlock
7070 addFunction "walk_inline" walkInline
71+ return 1
7172
7273walkElement :: (ToLuaStack a, Walkable [Inline] a, Walkable [Block] a)
7374 => a -> LuaFilter -> Lua NumResults
@@ -99,14 +100,14 @@ readDoc formatSpec content = do
99100--
100101-- MediaBag submodule
101102--
102−pushMediaBagModule :: CommonState -> IORef MB.MediaBag -> Lua ()
103+pushMediaBagModule :: CommonState -> IORef MB.MediaBag -> Lua NumResults
103104pushMediaBagModule commonState mediaBagRef = do
104105 Lua.newtable
105106 addFunction "insert" (insertMediaFn mediaBagRef)
106107 addFunction "lookup" (lookupMediaFn mediaBagRef)
107108 addFunction "list" (mediaDirectoryFn mediaBagRef)
108109 addFunction "fetch" (fetch commonState mediaBagRef)
109− return ()
110+ return 1
110111
111112addFunction :: ToHaskellFunction a => String -> a -> Lua ()
112113addFunction name fn = do