git.iko.soy/typeable/pandoc
on master help sign in
Lua: register script name in global variable
b5bd8a9Albert Krewinkel
src/Text/Pandoc/Lua.hs+3−2
@@ -37,7 +37,7 @@ import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
37import Text.Pandoc.Class (PandocIO)
37import Text.Pandoc.Class (PandocIO)
38import Text.Pandoc.Definition (Pandoc)
38import Text.Pandoc.Definition (Pandoc)
39import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
39import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
40−import Text.Pandoc.Lua.Init (runPandocLua)
40+import Text.Pandoc.Lua.Init (runPandocLua, registerScriptPath)
41import Text.Pandoc.Lua.Util (popValue)
41import Text.Pandoc.Lua.Util (popValue)
42import Text.Pandoc.Options (ReaderOptions)
42import Text.Pandoc.Options (ReaderOptions)
43import qualified Foreign.Lua as Lua
43import qualified Foreign.Lua as Lua
@@ -55,11 +55,12 @@ runLuaFilter' :: ReaderOptions -> FilePath -> String
55runLuaFilter' ropts filterPath format pd = do
55runLuaFilter' ropts filterPath format pd = do
56 registerFormat
56 registerFormat
57 registerReaderOptions
57 registerReaderOptions
58+ registerScriptPath filterPath
58 top <- Lua.gettop
59 top <- Lua.gettop
59 stat <- Lua.dofile filterPath
60 stat <- Lua.dofile filterPath
60 if stat /= OK
61 if stat /= OK
61 then do
62 then do
62− luaErrMsg <- peek (-1) <* Lua.pop 1
63+ luaErrMsg <- popValue
63 Lua.throwLuaError luaErrMsg
64 Lua.throwLuaError luaErrMsg
64 else do
65 else do
65 newtop <- Lua.gettop
66 newtop <- Lua.gettop
src/Text/Pandoc/Lua/Init.hs+7−1
@@ -31,6 +31,7 @@ module Text.Pandoc.Lua.Init
31 , runPandocLua
31 , runPandocLua
32 , initLuaState
32 , initLuaState
33 , luaPackageParams
33 , luaPackageParams
34+ , registerScriptPath
34 ) where
35 ) where
35
36
36import Control.Monad.Trans (MonadIO (..))
37import Control.Monad.Trans (MonadIO (..))
@@ -88,6 +89,11 @@ initLuaState luaPkgParams = do
88 loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
89 loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
89 putConstructorsInRegistry
90 putConstructorsInRegistry
90
91
92+registerScriptPath :: FilePath -> Lua ()
93+registerScriptPath fp = do
94+ Lua.push fp
95+ Lua.setglobal "PANDOC_SCRIPT_FILE"
96+
91putConstructorsInRegistry :: Lua ()
97putConstructorsInRegistry :: Lua ()
92putConstructorsInRegistry = do
98putConstructorsInRegistry = do
93 Lua.getglobal "pandoc"
99 Lua.getglobal "pandoc"
@@ -101,7 +107,7 @@ putConstructorsInRegistry = do
101 Lua.pop 1
107 Lua.pop 1
102 where
108 where
103 constrsToReg :: Data a => a -> Lua ()
109 constrsToReg :: Data a => a -> Lua ()
104− constrsToReg = mapM_ putInReg . map showConstr . dataTypeConstrs . dataTypeOf
110+ constrsToReg = mapM_ (putInReg . showConstr) . dataTypeConstrs . dataTypeOf
105
111
106 putInReg :: String -> Lua ()
112 putInReg :: String -> Lua ()
107 putInReg name = do
113 putInReg name = do
src/Text/Pandoc/Writers/Custom.hs+2−1
@@ -44,7 +44,7 @@ import Foreign.Lua.Api
44import Text.Pandoc.Class (PandocIO)
44import Text.Pandoc.Class (PandocIO)
45import Text.Pandoc.Definition
45import Text.Pandoc.Definition
46import Text.Pandoc.Error
46import Text.Pandoc.Error
47−import Text.Pandoc.Lua.Init (runPandocLua)
47+import Text.Pandoc.Lua.Init (runPandocLua, registerScriptPath)
48import Text.Pandoc.Lua.StackInstances ()
48import Text.Pandoc.Lua.StackInstances ()
49import Text.Pandoc.Lua.Util (addValue, dostring')
49import Text.Pandoc.Lua.Util (addValue, dostring')
50import Text.Pandoc.Options
50import Text.Pandoc.Options
@@ -106,6 +106,7 @@ writeCustom :: FilePath -> WriterOptions -> Pandoc -> PandocIO Text
106writeCustom luaFile opts doc@(Pandoc meta _) = do
106writeCustom luaFile opts doc@(Pandoc meta _) = do
107 luaScript <- liftIO $ UTF8.readFile luaFile
107 luaScript <- liftIO $ UTF8.readFile luaFile
108 res <- runPandocLua $ do
108 res <- runPandocLua $ do
109+ registerScriptPath luaFile
109 stat <- dostring' luaScript
110 stat <- dostring' luaScript
110 -- check for error in lua script (later we'll change the return type
111 -- check for error in lua script (later we'll change the return type
111 -- to handle this more gracefully):
112 -- to handle this more gracefully):
test/Tests/Lua.hs+6
@@ -111,6 +111,12 @@ tests = map (localOption (QuickCheckTests 20))
111 , plain (str "to_roman_numeral: OK")
111 , plain (str "to_roman_numeral: OK")
112 ])
112 ])
113
113
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+
114 , testCase "Pandoc version is set" . runPandocLua' $ do
120 , testCase "Pandoc version is set" . runPandocLua' $ do
115 Lua.getglobal' "table.concat"
121 Lua.getglobal' "table.concat"
116 Lua.getglobal "PANDOC_VERSION"
122 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