@@ -15,13 +15,14 @@ module Text.Pandoc.Lua.Init
1515 ( runLua
1616 , runLuaNoEnv
1717 , runLuaWith
18+ , userInit
1819 ) where
1920
2021import Control.Monad (when )
2122import Control.Monad.Catch (throwM , try )
2223import Control.Monad.Trans (MonadIO (.. ))
2324import HsLua as Lua hiding (status , try )
24- import Text.Pandoc.Class (PandocMonad (.. ), report )
25+ import Text.Pandoc.Class (CommonState , PandocMonad (.. ), report )
2526import Text.Pandoc.Data (readDataFile )
2627import Text.Pandoc.Error (PandocError (PandocLuaError ))
2728import Text.Pandoc.Logging (LogMessage (ScriptingWarning ))
@@ -67,7 +68,12 @@ initLuaState = do
6768 liftPandocLua Lua. openlibs
6869 setWarnFunction
6970 initModules
70- liftPandocLua runInitScript
71+
72+ -- | Initialize the user-configured pandoc state and run the init script.
73+ userInit :: CommonState -> LuaE PandocError ()
74+ userInit st = do
75+ unPandocLua $ putCommonState st
76+ runInitScript
7177
7278-- | Run the @init.lua@ data file as a Lua script.
7379runInitScript :: LuaE PandocError ()
@@ -92,26 +98,17 @@ runPandocLuaWith :: (PandocMonad m, MonadIO m)
9298 -> PandocLua a
9399 -> m a
94100runPandocLuaWith runner pLua = do
95- origState <- getCommonState
96- globals <- defaultGlobals
97101 (result, newState) <- liftIO . runner . unPandocLua $ do
98- putCommonState origState
99- liftPandocLua $ setGlobals globals
102+ liftPandocLua $ setGlobals defaultGlobals
100103 r <- pLua
101104 c <- getCommonState
102105 return (r, c)
103106 putCommonState newState
104107 return result
105108
106109-- | Global variables which should always be set.
107- defaultGlobals :: PandocMonad m => m [Global ]
108- defaultGlobals = do
109- commonState <- getCommonState
110- return
111- [ PANDOC_API_VERSION
112- , PANDOC_STATE commonState
113- , PANDOC_VERSION
114- ]
110+ defaultGlobals :: [Global ]
111+ defaultGlobals = [PANDOC_API_VERSION , PANDOC_VERSION ]
115112
116113setWarnFunction :: PandocLua ()
117114setWarnFunction = liftPandocLua . setwarnf' $ \ msg -> do
0 commit comments