summaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2022-10-12 21:37:47 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2022-10-14 10:37:37 -0700
commit8900b0f953798b01087e0135a3a9708a95eb7fde (patch)
treeb7221db3118cff0d81daf9282aa33c05536631f6 /src/Text
parent06ba4e9788eb5d53d566c01ad8e24aa91e104a74 (diff)
Lua: Support built-in default templates for custom writers
Custom writers can define a default template via a global `Template` function; the data directory is no longer searched for a default template. Writer authors can restore the old lookup behavior with ``` lua Template = function () local template return template.compile(template.default(PANDOC_SCRIPT_FILE)) end ```
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/App/OutputSettings.hs52
-rw-r--r--src/Text/Pandoc/Scripting.hs5
2 files changed, 33 insertions, 24 deletions
diff --git a/src/Text/Pandoc/App/OutputSettings.hs b/src/Text/Pandoc/App/OutputSettings.hs
index 537def363..2499832ba 100644
--- a/src/Text/Pandoc/App/OutputSettings.hs
+++ b/src/Text/Pandoc/App/OutputSettings.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
@@ -104,20 +105,40 @@ optToOutputSettings scriptingEngine opts = do
flvrd@(Format.FlavoredFormat format _extsDiff) <-
Format.parseFlavoredFormat writerName
- (writer, writerExts) <-
+
+ let standalone = optStandalone opts || not (isTextFormat format) || pdfOutput
+ let processCustomTemplate getDefault =
+ case optTemplate opts of
+ _ | not standalone -> return Nothing
+ Nothing -> Just <$> getDefault
+ Just tp -> do
+ -- strip off extensions
+ let tp' = case takeExtension tp of
+ "" -> tp <.> T.unpack format
+ _ -> tp
+ getTemplate tp'
+ >>= runWithPartials . compileTemplate tp'
+ >>= (\case
+ Left e -> throwError $ PandocTemplateError (T.pack e)
+ Right t -> return $ Just t)
+
+ (writer, writerExts, mtemplate) <-
if "lua" `T.isSuffixOf` format
then do
- (w, extsConf) <- engineWriteCustom scriptingEngine (T.unpack format)
- wexts <- Format.applyExtensionsDiff extsConf flvrd
- return (w, wexts)
+ (w, extsConf, mt) <- engineWriteCustom scriptingEngine (T.unpack format)
+ wexts <- Format.applyExtensionsDiff extsConf flvrd
+ templ <- processCustomTemplate mt
+ return (w, wexts, templ)
else do
+ tmpl <- processCustomTemplate (compileDefaultTemplate format)
if optSandbox opts
then case runPure (getWriter flvrd) of
- Right (w, wexts) -> return (makeSandboxed w, wexts)
+ Right (w, wexts) -> return (makeSandboxed w, wexts, tmpl)
Left e -> throwError e
- else getWriter flvrd
+ else do
+ (w, wexts) <- getWriter flvrd
+ return (w, wexts, tmpl)
- let standalone = optStandalone opts || not (isTextFormat format) || pdfOutput
let addSyntaxMap existingmap f = do
res <- liftIO (parseSyntaxDefinition f)
@@ -186,23 +207,8 @@ optToOutputSettings scriptingEngine opts = do
setVariableM "dzslides-core" dzcore vars
else return vars)
- templ <- case optTemplate opts of
- _ | not standalone -> return Nothing
- Nothing ->
- let filename = T.pack . takeFileName . T.unpack
- in Just <$> compileDefaultTemplate (filename format)
- Just tp -> do
- -- strip off extensions
- let tp' = case takeExtension tp of
- "" -> tp <.> T.unpack format
- _ -> tp
- res <- getTemplate tp' >>= runWithPartials . compileTemplate tp'
- case res of
- Left e -> throwError $ PandocTemplateError $ T.pack e
- Right t -> return $ Just t
-
let writerOpts = def {
- writerTemplate = templ
+ writerTemplate = mtemplate
, writerVariables = variables
, writerTabStop = optTabStop opts
, writerTableOfContents = optTableOfContents opts
diff --git a/src/Text/Pandoc/Scripting.hs b/src/Text/Pandoc/Scripting.hs
index d4be7e377..5e26b0279 100644
--- a/src/Text/Pandoc/Scripting.hs
+++ b/src/Text/Pandoc/Scripting.hs
@@ -23,6 +23,7 @@ import Text.Pandoc.Definition (Pandoc)
import Text.Pandoc.Error (PandocError (PandocNoScriptingEngine))
import Text.Pandoc.Filter.Environment (Environment)
import Text.Pandoc.Format (ExtensionsConfig)
+import Text.Pandoc.Templates (Template)
import Text.Pandoc.Readers (Reader)
import Text.Pandoc.Writers (Writer)
@@ -40,10 +41,12 @@ data ScriptingEngine = ScriptingEngine
-- ^ Function to parse input into a 'Pandoc' document.
, engineWriteCustom :: forall m. (PandocMonad m, MonadIO m)
- => FilePath -> m (Writer m, ExtensionsConfig)
+ => FilePath -> m (WriterProperties m)
-- ^ Invoke the given script file to convert to any custom format.
}
+type WriterProperties m = (Writer m, ExtensionsConfig, m (Template Text))
+
noEngine :: ScriptingEngine
noEngine = ScriptingEngine
{ engineName = "none"