aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/src
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2022-12-04 23:16:01 +0100
committerJohn MacFarlane <[email protected]>2022-12-05 08:52:37 -0800
commita03b77fbd35a0547f51429cd24388728aadd2008 (patch)
tree52d64efed811804febc2b6352e80887e9ded6792 /pandoc-lua-engine/src
parenta4ac1ebb954b0eaf0eac0805b8340b1609ef5d8f (diff)
Lua: support `-D` CLI option for custom writers [API change]
A new error `PandocNoTemplateError` (code 87) is thrown if a template is required but cannot be found.
Diffstat (limited to 'pandoc-lua-engine/src')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs24
1 files changed, 9 insertions, 15 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs
index c5e3e2469..91573c87b 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs
@@ -31,15 +31,13 @@ import Text.Pandoc.Format (ExtensionsConfig (..))
import Text.Pandoc.Lua.Global (Global (..), setGlobals)
import Text.Pandoc.Lua.Init (runLuaWith)
import Text.Pandoc.Lua.Marshal.Format (peekExtensionsConfig)
-import Text.Pandoc.Lua.Marshal.Template (peekTemplate)
import Text.Pandoc.Lua.Marshal.WriterOptions (pushWriterOptions)
-import Text.Pandoc.Templates (Template)
import Text.Pandoc.Writers (Writer (..))
import qualified Text.Pandoc.Lua.Writer.Classic as Classic
-- | Convert Pandoc to custom markup.
writeCustom :: (PandocMonad m, MonadIO m)
- => FilePath -> m (Writer m, ExtensionsConfig, m (Template Text))
+ => FilePath -> m (Writer m, ExtensionsConfig, Maybe Text)
writeCustom luaFile = do
luaState <- liftIO newGCManagedState
luaFile' <- fromMaybe luaFile <$> findFileWithDataFallback "writers" luaFile
@@ -66,19 +64,15 @@ writeCustom luaFile = do
TypeNil -> ExtensionsConfig mempty mempty <$ pop 1
_ -> forcePeek $ peekExtensionsConfig top `lastly` pop 1
- -- Store template function in registry
- let templateField = "Pandoc Writer Template"
- rawgetglobal "Template" *> setfield registryindex templateField
+ mtemplate <- rawgetglobal "Template" >>= \case
+ TypeNil -> pure Nothing
+ TypeFunction -> Just <$> do
+ callTrace 0 1
+ forcePeek $ peekText top `lastly` pop 1
+ _ -> Just <$> do
+ forcePeek $ peekText top `lastly` pop 1
- let getTemplate = liftIO $ withGCManagedState @PandocError luaState $ do
- getfield registryindex templateField >>= \case
- TypeNil -> failLua $ "No default template for writer; " <>
- "the global variable Template is undefined."
- _ -> do
- callTrace 0 1
- forcePeek $ peekTemplate top `lastly` pop 1
-
- let addProperties = (, extsConf, getTemplate)
+ let addProperties = (, extsConf, mtemplate)
rawgetglobal "Writer" >>= \case
TypeNil -> rawgetglobal "ByteStringWriter" >>= \case