@@ -30,6 +30,8 @@ Shared utility functions for pandoc writers.
31module Text.Pandoc.Writers.Shared (
31module Text.Pandoc.Writers.Shared (
32 metaToJSON
32 metaToJSON
33 , getField
35 , getField
34 , setField
36 , setField
35 , resetField
37 , resetField
@@ -57,24 +59,40 @@ import Data.Maybe ( isJust )
57-- of variables, specified at the command line or in the writer.
59-- of variables, specified at the command line or in the writer.
58-- Variables overwrite metadata fields with the same names.
60-- Variables overwrite metadata fields with the same names.
59-- If multiple variables are set with the same name, a list is
61-- If multiple variables are set with the same name, a list is
60−-- assigned.
62+-- assigned. Does nothing if 'writerTemplate' is Nothing.
61metaToJSON :: Monad m
63metaToJSON :: Monad m
62 => WriterOptions
64 => WriterOptions
63 -> ([Block] -> m String)
65 -> ([Block] -> m String)
64 -> ([Inline] -> m String)
66 -> ([Inline] -> m String)
66 -> m Value
68 -> m Value
67−metaToJSON opts blockWriter inlineWriter (Meta metamap)
69+metaToJSON opts blockWriter inlineWriter meta
68− | isJust (writerTemplate opts) = do
70+ | isJust (writerTemplate opts) =
69− renderedMap <- Traversable.mapM
70− (metaValueToJSON blockWriter inlineWriter)
71+ addVariablesToJSON opts <$> metaToJSON' blockWriter inlineWriter meta
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)
76 | otherwise = return (Object H.empty)
72 | otherwise = return (Object H.empty)
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)
81+metaToJSON' blockWriter inlineWriter (Meta metamap) = do
82+ renderedMap <- Traversable.mapM
83+ (metaValueToJSON blockWriter inlineWriter)
85+ return $ M.foldWithKey defField (Object H.empty) renderedMap
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)
78metaValueToJSON :: Monad m
96metaValueToJSON :: Monad m
79 => ([Block] -> m String)
97 => ([Block] -> m String)
80 -> ([Inline] -> m String)
98 -> ([Inline] -> m String)