Skip to content

Commit

Permalink
Define a meta-json variable for all writers.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jgm committed Nov 24, 2015
1 parent 902c63e commit 4361dc0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ depending on the output format, but include metadata fields as well as the follo
...

`subtitle`
: document subtitle; also used as subject in PDF metadata
: document subtitle

`abstract`
: document summary, included in LaTeX, ConTeXt, AsciiDoc, and Word docx
Expand Down Expand Up @@ -1064,6 +1064,9 @@ depending on the output format, but include metadata fields as well as the follo
`body`
: body of document

`meta-json`
: JSON representation of all of the document's metadata

Language variables
------------------

Expand Down
3 changes: 2 additions & 1 deletion src/Text/Pandoc/Writers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ jsonToYaml (Object hashmap) =
| otherwise -> (k' <> ":") $$ x
(k', Object _, x) -> (k' <> ":") $$ nest 2 x
(_, String "", _) -> empty
(k', _, x) -> k' <> ":" <> space <> hang 2 "" x)
(k', _, x) | k == "meta-json" -> empty
| otherwise -> k' <> ":" <> space <> hang 2 "" x)
$ sortBy (comparing fst) $ H.toList hashmap
jsonToYaml (Array vec) =
vcat $ map (\v -> hang 2 "- " (jsonToYaml v)) $ V.toList vec
Expand Down
6 changes: 4 additions & 2 deletions src/Text/Pandoc/Writers/Shared.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ import Text.Pandoc.Options (WriterOptions(..))
import qualified Data.HashMap.Strict as H
import qualified Data.Map as M
import qualified Data.Text as T
import Data.Aeson (FromJSON(..), fromJSON, ToJSON (..), Value(Object), Result(..))
import Data.Aeson (FromJSON(..), fromJSON, ToJSON (..), Value(Object), Result(..), encode)
import Text.Pandoc.UTF8 (toStringLazy)
import qualified Data.Traversable as Traversable
import Data.List ( groupBy )

Expand All @@ -67,7 +68,8 @@ metaToJSON opts blockWriter inlineWriter (Meta metamap)
renderedMap <- Traversable.mapM
(metaValueToJSON blockWriter inlineWriter)
metamap
return $ M.foldWithKey defField baseContext renderedMap
let metadata = M.foldWithKey defField baseContext renderedMap
return $ defField "meta-json" (toStringLazy $ encode metadata) metadata
| otherwise = return (Object H.empty)

metaValueToJSON :: Monad m
Expand Down

0 comments on commit 4361dc0

Please sign in to comment.