From 131f546282953a549eeddc688f16748a51063bff Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 25 Jan 2023 09:51:59 -0800 Subject: Ensure that automatically set variables ... `pandoc-version`, `outputfile`, `title-prefix`, `epub-cover-image`, `curdir`, `dzslides-core` can be overridden by `--variable` on the command line. Previously they would create lists in the template Context, which is not desirable. --- src/Text/Pandoc/App/CommandLineOptions.hs | 3 ++- src/Text/Pandoc/App/OutputSettings.hs | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/App/CommandLineOptions.hs b/src/Text/Pandoc/App/CommandLineOptions.hs index fc636e438..b54279e02 100644 --- a/src/Text/Pandoc/App/CommandLineOptions.hs +++ b/src/Text/Pandoc/App/CommandLineOptions.hs @@ -1062,7 +1062,8 @@ deprecatedOption o msg = do Right () -> return () Left e -> optError e --- | Set text value in text context. +-- | Set text value in text context. Create list if it has a value already, +-- or add to a list value. setVariable :: Text -> Text -> Context Text -> Context Text setVariable key val (Context ctx) = Context $ M.alter go key ctx where go Nothing = Just $ toVal val diff --git a/src/Text/Pandoc/App/OutputSettings.hs b/src/Text/Pandoc/App/OutputSettings.hs index 4fd30ff66..17df09e9d 100644 --- a/src/Text/Pandoc/App/OutputSettings.hs +++ b/src/Text/Pandoc/App/OutputSettings.hs @@ -38,7 +38,7 @@ import System.IO (stdout) import Text.Pandoc import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths) import Text.Pandoc.App.Opt (Opt (..)) -import Text.Pandoc.App.CommandLineOptions (engines, setVariable) +import Text.Pandoc.App.CommandLineOptions (engines) import qualified Text.Pandoc.Format as Format import Text.Pandoc.Highlighting (lookupHighlightingStyle) import Text.Pandoc.Scripting (ScriptingEngine (engineLoadCustom), @@ -163,8 +163,6 @@ optToOutputSettings scriptingEngine opts = do hlStyle <- traverse (lookupHighlightingStyle . T.unpack) $ optHighlightStyle opts - let setVariableM k v = return . setVariable k v - let setListVariableM _ [] ctx = return ctx setListVariableM k vs ctx = do let ctxMap = unContext ctx @@ -263,6 +261,13 @@ optToOutputSettings scriptingEngine opts = do , outputPdfProgram = maybePdfProg } +-- | Set text value in text context unless it is already set. +setVariableM :: Monad m + => T.Text -> T.Text -> Context T.Text -> m (Context T.Text) +setVariableM key val (Context ctx) = return $ Context $ M.alter go key ctx + where go Nothing = Just $ toVal val + go (Just x) = Just x + baseWriterName :: T.Text -> T.Text baseWriterName = T.takeWhile (\c -> c /= '+' && c /= '-') -- cgit v1.2.3