diff options
author | John MacFarlane <jgm@berkeley.edu> | 2017-02-25 23:13:23 +0100 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-02-25 23:13:23 +0100 |
commit | 9ab30c6495897ca02c873897bf7f4be66687d8b2 (patch) | |
tree | 8fd401ad1da049cf656cadb7aa4bd5b3823991ad /src/Text | |
parent | 03941ca9b0ce69140bb3ca1b4f0deef6d1c10ef3 (diff) | |
download | pandoc-9ab30c6495897ca02c873897bf7f4be66687d8b2.tar.gz |
Writers.Shared: export metaToJSON', addVariablesToJSON.
This allows us to add the variables AFTER using the metadata
to generate a YAML header (in the Markdown writer).
Addresses the problem shown by
https://travis-ci.org/jgm/pandoc/jobs/205154181#L705
See #3439
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/Writers/Shared.hs | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs index 34928e13e..a9431a920 100644 --- a/src/Text/Pandoc/Writers/Shared.hs +++ b/src/Text/Pandoc/Writers/Shared.hs @@ -30,6 +30,8 @@ Shared utility functions for pandoc writers. -} module Text.Pandoc.Writers.Shared ( metaToJSON + , metaToJSON' + , addVariablesToJSON , getField , setField , resetField @@ -57,24 +59,40 @@ import Data.Maybe ( isJust ) -- of variables, specified at the command line or in the writer. -- Variables overwrite metadata fields with the same names. -- If multiple variables are set with the same name, a list is --- assigned. +-- assigned. Does nothing if 'writerTemplate' is Nothing. metaToJSON :: Monad m => WriterOptions -> ([Block] -> m String) -> ([Inline] -> m String) -> Meta -> m Value -metaToJSON opts blockWriter inlineWriter (Meta metamap) - | isJust (writerTemplate opts) = do - renderedMap <- Traversable.mapM - (metaValueToJSON blockWriter inlineWriter) - metamap - let metadata = M.foldWithKey defField (Object H.empty) renderedMap - return $ foldl (\acc (x,y) -> resetField x y acc) - (defField "meta-json" (toStringLazy $ encode metadata) metadata) - (writerVariables opts) +metaToJSON opts blockWriter inlineWriter meta + | isJust (writerTemplate opts) = + addVariablesToJSON opts <$> metaToJSON' blockWriter inlineWriter meta | otherwise = return (Object H.empty) +-- | Like 'metaToJSON', but does not include variables and is +-- not sensitive to 'writerTemplate'. +metaToJSON' :: Monad m + => ([Block] -> m String) + -> ([Inline] -> m String) + -> Meta + -> m Value +metaToJSON' blockWriter inlineWriter (Meta metamap) = do + renderedMap <- Traversable.mapM + (metaValueToJSON blockWriter inlineWriter) + metamap + return $ M.foldWithKey defField (Object H.empty) renderedMap + +-- | Add variables to JSON object, replacing any existing values. +-- Also include @meta-json@, a field containing a string representation +-- of the original JSON object itself, prior to addition of variables. +addVariablesToJSON :: WriterOptions -> Value -> Value +addVariablesToJSON opts metadata = + foldl (\acc (x,y) -> resetField x y acc) + (defField "meta-json" (toStringLazy $ encode metadata) metadata) + (writerVariables opts) + metaValueToJSON :: Monad m => ([Block] -> m String) -> ([Inline] -> m String) |