diff options
| author | Albert Krewinkel <[email protected]> | 2024-01-20 18:02:02 +0100 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2024-01-20 10:14:48 -0800 |
| commit | c2f7f5c63fe223f07033cfc686cb258508970301 (patch) | |
| tree | f73d24f2c3666c646ef0238e1ceeedbfc6ed447d /pandoc-lua-engine | |
| parent | 74f4ae25d21c40a8f4dc921b810d04244c0595b4 (diff) | |
Custom writers: fix handling of common state
The CommonState (`PANDOC_STATE` in Lua) may change between the time that
a custom writer script is first loaded and when the writer is run.
However, the writer was always using the initial state, which led to
problems, e.g. when the mediabag was updated in a filter, as those
updates where not visible to the writer.
The state is now updated right before the writer function runs.
Fixes: #9229
Diffstat (limited to 'pandoc-lua-engine')
| -rw-r--r-- | pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs index 0a2eb2edc..f81c951ed 100644 --- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs +++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs @@ -22,11 +22,13 @@ import Text.Pandoc.Lua.Init (runLuaWith) import Text.Pandoc.Lua.Marshal.Format (peekExtensionsConfig) import Text.Pandoc.Lua.Marshal.Pandoc (peekPandoc) import Text.Pandoc.Lua.Marshal.WriterOptions (pushWriterOptions) +import Text.Pandoc.Lua.PandocLua (unPandocLua) import Text.Pandoc.Readers (Reader (..)) import Text.Pandoc.Sources (ToSources(..)) import Text.Pandoc.Scripting (CustomComponents (..)) import Text.Pandoc.Writers (Writer (..)) import qualified Text.Pandoc.Lua.Writer.Classic as Classic +import qualified Text.Pandoc.Class as PandocMonad -- | Convert custom markup to Pandoc. loadCustom :: (PandocMonad m, MonadIO m) @@ -80,15 +82,21 @@ loadCustom luaFile = do pure $ if docType /= TypeFunction then Nothing - else Just . TextWriter $ \opts doc -> + else Just . TextWriter $ \opts doc -> do + -- See TextWriter below for why the state is updated + st <- PandocMonad.getCommonState liftIO $ withGCManagedState luaState $ - Classic.runCustom @PandocError opts doc + unPandocLua (PandocMonad.putCommonState st) >> + Classic.runCustom @PandocError opts doc _ -> Just <$!> do -- Binary writer. Writer function is on top of the stack. setfield registryindex writerField - pure $ ByteStringWriter $ \opts doc -> + pure $ ByteStringWriter $ \opts doc -> do + -- See TextWriter below for why the state is updated + st <- PandocMonad.getCommonState -- Call writer with document and writer options as arguments. liftIO $ withGCManagedState luaState $ do + unPandocLua (PandocMonad.putCommonState st) getfield registryindex writerField push doc pushWriterOptions opts @@ -97,8 +105,13 @@ loadCustom luaFile = do _ -> Just <$!> do -- New-type text writer. Writer function is on top of the stack. setfield registryindex writerField - pure $ TextWriter $ \opts doc -> + pure $ TextWriter $ \opts doc -> do + -- The CommonState might have changed since the Lua file was + -- loaded. That's why the state must be updated when the + -- writer is run. (#9229) + st <- PandocMonad.getCommonState liftIO $ withGCManagedState luaState $ do + unPandocLua (PandocMonad.putCommonState st) getfield registryindex writerField push doc pushWriterOptions opts |
