git.iko.soy/typeable/pandoc
on master help sign in
Lua: register script name in global variable

The name of the Lua script which is executed is made available in the global Lua variable `PANDOC_SCRIPT_FILE`, both for Lua filters and custom writers. Closes: #4393

commitb5bd8a9461dc317ff61abec68feba4a86d39e9f2 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 24 Feb 2018, 21:59 (9 years ago) parent39dd7c7
5 files changed+21−4
src/Text/Pandoc/Lua.hs+3−2 src/Text/Pandoc/Lua/Init.hs+7−1 src/Text/Pandoc/Writers/Custom.hs+2−1 test/Tests/Lua.hs+6 test/lua/script-name.lua+3
src/Text/Pandoc/Lua.hs+3−2
@@ -37,7 +37,7 @@ import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
3737import Text.Pandoc.Class (PandocIO)
3838import Text.Pandoc.Definition (Pandoc)
3939import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
40−import Text.Pandoc.Lua.Init (runPandocLua)
40+import Text.Pandoc.Lua.Init (runPandocLua, registerScriptPath)
4141import Text.Pandoc.Lua.Util (popValue)
4242import Text.Pandoc.Options (ReaderOptions)
4343import qualified Foreign.Lua as Lua
@@ -55,11 +55,12 @@ runLuaFilter' :: ReaderOptions -> FilePath -> String
5555runLuaFilter' ropts filterPath format pd = do
5656 registerFormat
5757 registerReaderOptions
58+ registerScriptPath filterPath
5859 top <- Lua.gettop
5960 stat <- Lua.dofile filterPath
6061 if stat /= OK
6162 then do
62− luaErrMsg <- peek (-1) <* Lua.pop 1
63+ luaErrMsg <- popValue
6364 Lua.throwLuaError luaErrMsg
6465 else do
6566 newtop <- Lua.gettop
src/Text/Pandoc/Lua/Init.hs+7−1
@@ -31,6 +31,7 @@ module Text.Pandoc.Lua.Init
3131 , runPandocLua
3232 , initLuaState
3333 , luaPackageParams
34+ , registerScriptPath
3435 ) where
3536
3637import Control.Monad.Trans (MonadIO (..))
@@ -88,6 +89,11 @@ initLuaState luaPkgParams = do
8889 loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
8990 putConstructorsInRegistry
9091
92+registerScriptPath :: FilePath -> Lua ()
93+registerScriptPath fp = do
94+ Lua.push fp
95+ Lua.setglobal "PANDOC_SCRIPT_FILE"
96+
9197putConstructorsInRegistry :: Lua ()
9298putConstructorsInRegistry = do
9399 Lua.getglobal "pandoc"
@@ -101,7 +107,7 @@ putConstructorsInRegistry = do
101107 Lua.pop 1
102108 where
103109 constrsToReg :: Data a => a -> Lua ()
104− constrsToReg = mapM_ putInReg . map showConstr . dataTypeConstrs . dataTypeOf
110+ constrsToReg = mapM_ (putInReg . showConstr) . dataTypeConstrs . dataTypeOf
105111
106112 putInReg :: String -> Lua ()
107113 putInReg name = do
src/Text/Pandoc/Writers/Custom.hs+2−1
@@ -44,7 +44,7 @@ import Foreign.Lua.Api
4444import Text.Pandoc.Class (PandocIO)
4545import Text.Pandoc.Definition
4646import Text.Pandoc.Error
47−import Text.Pandoc.Lua.Init (runPandocLua)
47+import Text.Pandoc.Lua.Init (runPandocLua, registerScriptPath)
4848import Text.Pandoc.Lua.StackInstances ()
4949import Text.Pandoc.Lua.Util (addValue, dostring')
5050import Text.Pandoc.Options
@@ -106,6 +106,7 @@ writeCustom :: FilePath -> WriterOptions -> Pandoc -> PandocIO Text
106106writeCustom luaFile opts doc@(Pandoc meta _) = do
107107 luaScript <- liftIO $ UTF8.readFile luaFile
108108 res <- runPandocLua $ do
109+ registerScriptPath luaFile
109110 stat <- dostring' luaScript
110111 -- check for error in lua script (later we'll change the return type
111112 -- to handle this more gracefully):
test/Tests/Lua.hs+6
@@ -111,6 +111,12 @@ tests = map (localOption (QuickCheckTests 20))
111111 , plain (str "to_roman_numeral: OK")
112112 ])
113113
114+ , testCase "Script filename is set" $
115+ assertFilterConversion "unexpected script name"
116+ "script-name.lua"
117+ (doc $ para "ignored")
118+ (doc $ para "lua/script-name.lua")
119+
114120 , testCase "Pandoc version is set" . runPandocLua' $ do
115121 Lua.getglobal' "table.concat"
116122 Lua.getglobal "PANDOC_VERSION"
test/lua/script-name.luanew file+3
@@ -0,0 +1,3 @@
1+function Para (_)
2+ return pandoc.Para{pandoc.Str(PANDOC_SCRIPT_FILE)}
3+end