@@ -30,6 +30,8 @@ Shared utility functions for pandoc writers.
3030-}
3131module Text.Pandoc.Writers.Shared (
3232 metaToJSON
33+ , metaToJSON'
34+ , addVariablesToJSON
3335 , getField
3436 , setField
3537 , resetField
@@ -57,24 +59,40 @@ import Data.Maybe ( isJust )
5759-- of variables, specified at the command line or in the writer.
5860-- Variables overwrite metadata fields with the same names.
5961-- If multiple variables are set with the same name, a list is
60−-- assigned.
62+-- assigned. Does nothing if 'writerTemplate' is Nothing.
6163metaToJSON :: Monad m
6264 => WriterOptions
6365 -> ([Block] -> m String)
6466 -> ([Inline] -> m String)
6567 -> Meta
6668 -> m Value
67−metaToJSON opts blockWriter inlineWriter (Meta metamap)
68− | isJust (writerTemplate opts) = do
69− renderedMap <- Traversable.mapM
70− (metaValueToJSON blockWriter inlineWriter)
71− metamap
72− let metadata = M.foldWithKey defField (Object H.empty) renderedMap
73− return $ foldl (\acc (x,y) -> resetField x y acc)
74− (defField "meta-json" (toStringLazy $ encode metadata) metadata)
75− (writerVariables opts)
69+metaToJSON opts blockWriter inlineWriter meta
70+ | isJust (writerTemplate opts) =
71+ addVariablesToJSON opts <$> metaToJSON' blockWriter inlineWriter meta
7672 | otherwise = return (Object H.empty)
7773
74+-- | Like 'metaToJSON', but does not include variables and is
75+-- not sensitive to 'writerTemplate'.
76+metaToJSON' :: Monad m
77+ => ([Block] -> m String)
78+ -> ([Inline] -> m String)
79+ -> Meta
80+ -> m Value
81+metaToJSON' blockWriter inlineWriter (Meta metamap) = do
82+ renderedMap <- Traversable.mapM
83+ (metaValueToJSON blockWriter inlineWriter)
84+ metamap
85+ return $ M.foldWithKey defField (Object H.empty) renderedMap
86+
87+-- | Add variables to JSON object, replacing any existing values.
88+-- Also include @meta-json@, a field containing a string representation
89+-- of the original JSON object itself, prior to addition of variables.
90+addVariablesToJSON :: WriterOptions -> Value -> Value
91+addVariablesToJSON opts metadata =
92+ foldl (\acc (x,y) -> resetField x y acc)
93+ (defField "meta-json" (toStringLazy $ encode metadata) metadata)
94+ (writerVariables opts)
95+
7896metaValueToJSON :: Monad m
7997 => ([Block] -> m String)
8098 -> ([Inline] -> m String)