summaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2022-11-09 17:36:23 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2022-12-20 20:58:44 -0800
commit063480accd3421bfc77478b8dbddc1985553ed75 (patch)
tree4ef6d846f17085cbc2f7410123d7698c4196b069 /pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs
parent8553459f61eb2290008fafedcfbb1df0bb1fe1b4 (diff)
T.P.Scripting: Refactor the scripting engine.
The new type CustomComponents is exported from T.P.Scripting, and the ScriptEngine fields are changed. Instead of separate fields for custom readers and writers, we now have a single function that loads any number of "components" from a script: these may be custom readers, custom writers, templates for writers, or extension configs. (Note: it's possible to have a custom reader and a custom writer for a format together in the same file.) Pandoc now checks the folder `custom` in the user's data directory for a matching script if it can't find one in the local directory. Previously, the `readers` and `writers` data directories were search for custom readers and writers, respectively. Scripts in those directories must be moved to the `custom` folder. Custom readers used to implement a fallback behavior that allowed to consume just a string value as input to the `Reader` function. This has been removed, the first argument is now always a list of sources. Use `tostring` on that argument to get a string. Closes #8417. Signed-off-by: Albert Krewinkel <albert@zeitkraut.de>
Diffstat (limited to 'pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs106
1 files changed, 0 insertions, 106 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs
deleted file mode 100644
index 91573c87b..000000000
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Writer.hs
+++ /dev/null
@@ -1,106 +0,0 @@
-{-# LANGUAGE LambdaCase #-}
-{-# LANGUAGE OverloadedStrings #-}
-{-# LANGUAGE TupleSections #-}
-{-# LANGUAGE TypeApplications #-}
-{- |
- Module : Text.Pandoc.Lua.Writer
- Copyright : Copyright (C) 2012-2022 John MacFarlane
- License : GNU GPL, version 2 or above
-
- Maintainer : John MacFarlane <jgm@berkeley.edu>
- Stability : alpha
- Portability : portable
-
-Conversion of Pandoc documents using a custom Lua writer.
--}
-module Text.Pandoc.Lua.Writer
- ( writeCustom
- ) where
-
-import Control.Exception
-import Control.Monad ((<=<))
-import Data.Default (def)
-import Data.Maybe (fromMaybe)
-import Data.Text (Text)
-import HsLua
-import HsLua.Core.Run (newGCManagedState, withGCManagedState)
-import Control.Monad.IO.Class (MonadIO)
-import Text.Pandoc.Class (PandocMonad, findFileWithDataFallback)
-import Text.Pandoc.Error (PandocError (..))
-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.WriterOptions (pushWriterOptions)
-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, Maybe Text)
-writeCustom luaFile = do
- luaState <- liftIO newGCManagedState
- luaFile' <- fromMaybe luaFile <$> findFileWithDataFallback "writers" luaFile
- either throw pure <=< runLuaWith luaState $ do
- setGlobals [ PANDOC_DOCUMENT mempty
- , PANDOC_SCRIPT_FILE luaFile'
- , PANDOC_WRITER_OPTIONS def
- ]
- dofileTrace luaFile' >>= \case
- OK -> pure ()
- _ -> throwErrorAsException
- -- Most classic writers contain code that throws an error if a global
- -- is not present. This would break our check for the existence of a
- -- "Writer" function. We resort to raw access for that reason, but
- -- could also catch the error instead.
- let rawgetglobal x = do
- pushglobaltable
- pushName x
- rawget (nth 2) <* remove (nth 2) -- remove global table
-
- let writerField = "Pandoc Writer function"
-
- extsConf <- rawgetglobal "Extensions" >>= \case
- TypeNil -> ExtensionsConfig mempty mempty <$ pop 1
- _ -> forcePeek $ peekExtensionsConfig top `lastly` pop 1
-
- 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 addProperties = (, extsConf, mtemplate)
-
- rawgetglobal "Writer" >>= \case
- 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
- pure $ addProperties . 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
- pure $ addProperties . ByteStringWriter $ \opts doc ->
- -- Call writer with document and writer options as arguments.
- liftIO $ withGCManagedState luaState $ do
- getfield registryindex writerField
- push doc
- pushWriterOptions opts
- callTrace 2 1
- forcePeek @PandocError $ peekLazyByteString top
- _ -> do
- -- New-type text writer. Writer function is on top of the stack.
- setfield registryindex writerField
- pure $ addProperties . TextWriter $ \opts doc ->
- liftIO $ withGCManagedState luaState $ do
- getfield registryindex writerField
- push doc
- pushWriterOptions opts
- callTrace 2 1
- forcePeek @PandocError $ peekText top