-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Pandoc to parse metadata rather than doing it in the plugin.
This involves a fairly complicated dance with a Pandoc "filter" module in order to get all of the metadata to be visible in the output, but means that all metadata formats supported by Pandoc are available without the need for any additional Python modules. It also means strings in metadata will be processed as Markdown. NOTE: Thanks to jgm/pandoc#2026 and backward compatibility constraints, this change defaults to enabling 'mmd_title_block' and *disabling* 'pandoc_title_block' and 'yaml_metadata_block'. Moreover, putting either +pandoc_title_block or +yaml_metadata_block in PANDOC_EXTENSIONS will cause mmd_title_block to be disabled.
- Loading branch information
Showing
3 changed files
with
249 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# This is a filter script which embeds all of the metadata parsed by | ||
# Pandoc into the HTML output, where the main body of the reader can | ||
# pick it up. In order to preserve Pandoc's translation of Markdown | ||
# in metadata values, we convert the metadata structure into an HTML | ||
# tree structure. A <hr> separates the translated metadata from the | ||
# document itself. | ||
# | ||
# See http://johnmacfarlane.net/pandoc/scripting.html for documentation | ||
# of the JSON-serialized AST that we are manipulating. | ||
|
||
import json | ||
import sys | ||
|
||
def N(t, c, cls=None): | ||
if cls is not None: c = [ ["", [cls], []], c ] | ||
return { "t": t, "c": c } | ||
|
||
def cvt_metainlines(c): | ||
return N("Plain", [N("Span", c, "metavalue")]) | ||
|
||
def cvt_metamap(c): | ||
return N("DefinitionList", [ ( [N("Str", key)], [[ convert(val) ]] ) | ||
for key, val in sorted(c.items()) ]) | ||
|
||
CONVERTERS = { | ||
"MetaMap": cvt_metamap, | ||
"MetaInlines": cvt_metainlines, | ||
"MetaBool": lambda c: cvt_metainlines([N("Str", str(c).lower())]), | ||
"MetaString": lambda c: cvt_metainlines([N("Str", c)]), | ||
"MetaBlocks": lambda c: N("Div", c, "metavalue"), | ||
"MetaList": lambda c: N("BulletList", [ [convert(item)] for item in c ]) | ||
} | ||
|
||
def convert(item): | ||
return CONVERTERS[item["t"]](item["c"]) | ||
|
||
def main(): | ||
blob = json.load(sys.stdin) | ||
metadata = blob[0]['unMeta'] | ||
rendered = [cvt_metamap(metadata), N("HorizontalRule", [])] | ||
rendered.extend(blob[1]) | ||
blob = [blob[0], rendered] | ||
json.dump(blob, sys.stdout, separators=(',',':')) | ||
|
||
# This filter script is imported by pandoc_reader in order to learn its | ||
# actual filename, so don't do anything unless invoked as __main__. | ||
if __name__ == '__main__': main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters