aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2019-11-14 11:01:43 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2019-11-14 11:01:43 -0800
commit871bfaf794f132133e35641e55619991253221f2 (patch)
tree4adb7249edad6c7dd822cdfb8b99af9407442a3a /src/Text
parent703cbf437c9bcef9ee135f80422fb681cbbc76fa (diff)
downloadpandoc-871bfaf794f132133e35641e55619991253221f2.tar.gz
Writers.Shared: Clean up code for adding metadata to variables.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Writers/Shared.hs19
1 files changed, 9 insertions, 10 deletions
diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs
index 9aa19c2d9..39035832f 100644
--- a/src/Text/Pandoc/Writers/Shared.hs
+++ b/src/Text/Pandoc/Writers/Shared.hs
@@ -86,19 +86,18 @@ metaToContext' :: (Monad m, TemplateTarget a)
metaToContext' blockWriter inlineWriter (Meta metamap) =
Context <$> mapM (metaValueToVal blockWriter inlineWriter) metamap
--- | Add variables to a template Context, replacing any existing values.
+-- | Add variables to a template Context, using monoidal append.
+-- Also add `meta-json`. Note that metadata values are used
+-- in template contexts only when like-named variables aren't set.
addVariablesToContext :: TemplateTarget a
=> WriterOptions -> Context a -> Context a
-addVariablesToContext opts (Context m1) =
- Context (m1 `M.union` m2 `M.union` m3)
+addVariablesToContext opts c1 =
+ c2 <> (fromText <$> writerVariables opts) <> c1
where
- m2 = case traverse go (writerVariables opts) of
- Just (Context x) -> x
- Nothing -> mempty
- m3 = M.insert "meta-json" (SimpleVal $ literal $ fromText jsonrep)
- mempty
- go = Just . fromText
- jsonrep = UTF8.toText $ BL.toStrict $ encode $ toJSON m1
+ c2 = Context $
+ M.insert "meta-json" (SimpleVal $ literal $ fromText jsonrep)
+ mempty
+ jsonrep = UTF8.toText $ BL.toStrict $ encode $ toJSON c1
metaValueToVal :: (Monad m, TemplateTarget a)
=> ([Block] -> m (Doc a))