diff options
| author | Albert Krewinkel <[email protected]> | 2022-10-02 23:25:10 +0200 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2022-10-03 08:47:32 -0700 |
| commit | 3324664aab23f1a5e92cc7b8bf712e2fcc6b6749 (patch) | |
| tree | f14af7d4351a89eeb7343057bfd251aaef27f2dc /pandoc-lua-engine/src | |
| parent | 921d7dd2711828d188fe4af2ba6d2765571cde2d (diff) | |
[API Change] Support bytestring custom writers.
New-style custom Lua writers can now define an alternative entry function
`BinaryWriter`. If a function with that name is defined, then pandoc
will treat the returned string as binary output. This allows to generate
formats like docx and odt with custom writers.
Diffstat (limited to 'pandoc-lua-engine/src')
| -rw-r--r-- | pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs index 8dbae9eae..f2d75b905 100644 --- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs +++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs @@ -56,13 +56,25 @@ writeCustom luaFile = do let writerField = "PANDOC Writer function" rawgetglobal "Writer" >>= \case - TypeNil -> do - -- Neither `Writer` nor `BinaryWriter` are defined. Try to - -- use the file as a classic writer. - pop 1 -- remove nil - return . TextWriter $ \opts doc -> - liftIO $ withGCManagedState luaState $ do - Classic.runCustom @PandocError opts doc + TypeNil -> rawgetglobal "ByteStringWriter" >>= \case + TypeNil -> do + -- Neither `Writer` nor `BinaryWriter` are defined. Try to + -- use the file as a classic writer. + pop 1 -- remove nil + return . TextWriter $ \opts doc -> + liftIO $ withGCManagedState luaState $ do + Classic.runCustom @PandocError opts doc + _ -> do + -- Binary writer. Writer function is on top of the stack. + setfield registryindex writerField + return . ByteStringWriter $ \opts doc -> + -- Call writer with document and writer options as arguments. + liftIO $ withGCManagedState luaState $ do + getfield registryindex writerField + push doc + push opts + callTrace 2 1 + forcePeek @PandocError $ peekLazyByteString top _ -> do -- New-type text writer. Writer function is on top of the stack. setfield registryindex writerField |
