aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/src/Text
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2025-11-19 12:22:41 +0100
committerAlbert Krewinkel <[email protected]>2025-11-19 12:22:41 +0100
commit4e0f62689891c05bdc0b9cc9a6935fb3f3d778dd (patch)
tree04c7e6d2c56f422efac570ebfdd709725dba3b70 /pandoc-lua-engine/src/Text
parentf162c7a1cde0c22e6773180ee130b040fca6644d (diff)
Lua: preserve common state of custom Lua readers
The common state is transferred to Lua when calling a custom Lua reader, and is now also transferred back after the reader has finished. This ensures that info messages, warnings, and mediabag entries are available to the main program and all subsequent processing steps.
Diffstat (limited to 'pandoc-lua-engine/src/Text')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs19
1 files changed, 13 insertions, 6 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs
index ff78f5f5d..6d902c9f0 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Custom.hs
@@ -148,12 +148,19 @@ readerField = "Pandoc Reader function"
writerField :: Name
writerField = "Pandoc Writer function"
--- | Runs a Lua action in a continueable environment.
-inLua :: MonadIO m => GCManagedState -> LuaE PandocError a -> m a
-inLua st = liftIO . withGCManagedState @PandocError st
+-- | Runs a Lua action in a continuable environment and transfers the common
+-- state after the Lua action has finished.
+inLua :: (PandocMonad m, MonadIO m)
+ => GCManagedState -> LuaE PandocError a -> m a
+inLua st luaAction = do
+ let inLua' = liftIO . withGCManagedState @PandocError st
+ result <- inLua' luaAction
+ cstate <- inLua' (unPandocLua PandocMonad.getCommonState)
+ PandocMonad.putCommonState cstate
+ return result
-- | Returns the ByteStringReader function
-byteStringReader :: MonadIO m => GCManagedState -> Reader m
+byteStringReader :: (PandocMonad m, MonadIO m) => GCManagedState -> Reader m
byteStringReader st = ByteStringReader $ \ropts input -> inLua st $ do
getfield registryindex readerField
push input
@@ -163,12 +170,12 @@ byteStringReader st = ByteStringReader $ \ropts input -> inLua st $ do
_ -> throwErrorAsException
-- | Returns the TextReader function
-textReader :: MonadIO m => GCManagedState -> Reader m
+textReader :: (PandocMonad m, MonadIO m) => GCManagedState -> Reader m
textReader st = TextReader $ \ropts srcs -> inLua st $ do
let input = toSources srcs
getfield registryindex readerField
push input
push ropts
pcallTrace 2 1 >>= \case
- OK -> forcePeek $ peekPandoc top
+ OK -> forcePeek (peekPandoc top)
_ -> throwErrorAsException