git.iko.soy/typeable/pandoc
on master help sign in
Define a `meta-json` variable for all writers.

This contains a JSON version of all the metadata, in the format selected for the writer. So, for example, to get just the YAML metadata, you can run pandoc with the following custom template: $meta-json$ Closes #2019. The intent is to make it easier for static site generators and other tools to get at the metadata.

commit4361dc0245a65d4f24f2df062684cdb1a0c3bc5a authorJohn MacFarlane <jgm@berkeley.edu> dateMon 23 Nov 2015, 20:26 (11 years ago) parent902c63e
3 files changed+10−4
README+4−1 src/Text/Pandoc/Writers/Markdown.hs+2−1 src/Text/Pandoc/Writers/Shared.hs+4−2
README+4−1
@@ -1035,7 +1035,7 @@ depending on the output format, but include metadata fields as well as the follo
10351035 ...
10361036
10371037`subtitle`
1038−: document subtitle; also used as subject in PDF metadata
1038+: document subtitle
10391039
10401040`abstract`
10411041: document summary, included in LaTeX, ConTeXt, AsciiDoc, and Word docx
@@ -1064,6 +1064,9 @@ depending on the output format, but include metadata fields as well as the follo
10641064`body`
10651065: body of document
10661066
1067+`meta-json`
1068+: JSON representation of all of the document's metadata
1069+
10671070Language variables
10681071------------------
10691072
src/Text/Pandoc/Writers/Markdown.hs+2−1
@@ -126,7 +126,8 @@ jsonToYaml (Object hashmap) =
126126 | otherwise -> (k' <> ":") $$ x
127127 (k', Object _, x) -> (k' <> ":") $$ nest 2 x
128128 (_, String "", _) -> empty
129− (k', _, x) -> k' <> ":" <> space <> hang 2 "" x)
129+ (k', _, x) | k == "meta-json" -> empty
130+ | otherwise -> k' <> ":" <> space <> hang 2 "" x)
130131 $ sortBy (comparing fst) $ H.toList hashmap
131132jsonToYaml (Array vec) =
132133 vcat $ map (\v -> hang 2 "- " (jsonToYaml v)) $ V.toList vec
src/Text/Pandoc/Writers/Shared.hs+4−2
@@ -45,7 +45,8 @@ import Text.Pandoc.Options (WriterOptions(..))
4545import qualified Data.HashMap.Strict as H
4646import qualified Data.Map as M
4747import qualified Data.Text as T
48−import Data.Aeson (FromJSON(..), fromJSON, ToJSON (..), Value(Object), Result(..))
48+import Data.Aeson (FromJSON(..), fromJSON, ToJSON (..), Value(Object), Result(..), encode)
49+import Text.Pandoc.UTF8 (toStringLazy)
4950import qualified Data.Traversable as Traversable
5051import Data.List ( groupBy )
5152
@@ -67,7 +68,8 @@ metaToJSON opts blockWriter inlineWriter (Meta metamap)
6768 renderedMap <- Traversable.mapM
6869 (metaValueToJSON blockWriter inlineWriter)
6970 metamap
70− return $ M.foldWithKey defField baseContext renderedMap
71+ let metadata = M.foldWithKey defField baseContext renderedMap
72+ return $ defField "meta-json" (toStringLazy $ encode metadata) metadata
7173 | otherwise = return (Object H.empty)
7274
7375metaValueToJSON :: Monad m