Skip to content

Instantly share code, notes, and snippets.

@rbdixon
Created March 7, 2016 14:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbdixon/bbbd7364ed7ea1f5f2a9 to your computer and use it in GitHub Desktop.
Save rbdixon/bbbd7364ed7ea1f5f2a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Pandoc filter to allow interpolation of metadata fields
into a document. %{fields} will be replaced by the field's
value, assuming it is of the type MetaInlines or MetaString.
"""
import sys
from pandocfilters import toJSONFilter, attributes, Span, Str, Table
import re
pattern = re.compile('([^%]*)%\{(.*)\}([^%]*)')
def metavars(key, value, format, meta):
if key == 'Str':
m = pattern.match(value)
if m:
pre = m.group(1)
field = m.group(2)
post = m.group(3)
result = meta.get(field, {})
if result:
if 'MetaInlines' in result['t']:
return Span(attributes({'class': 'interpolated',
'field': field}),
[Str(pre)] + result['c'] + [Str(post)])
elif 'MetaString' in result['t']:
return Str(result['c'])
else:
sys.stderr.write("\n\nError: Variable %{{{0}}} used but not declared in document.\n\n".format(field))
exit(-1)
elif key == 'Para' and type(value) == list and len(value) == 1 and value[0]['t'] == 'Str':
# Test to see if contained string is a variable
m = pattern.match(value[0]['c'])
if m:
pre = m.group(1)
field = m.group(2)
post = m.group(3)
# Get substitution value
result = meta.get(field, {})
if result:
# Only substitute at this level _if_ the replacement is a MetaBlocks containing a Table
if 'MetaBlocks' in result['t'] and result['c'][0]['t'] == 'Table':
res = result['c'][0]
# sys.stderr.write("Key: {0}, Value: {1}, Result: {2}\n".format(key, value, res))
return res
else:
sys.stderr.write("\n\nError: Variable %{{{0}}} used but not declared in document.\n\n".format(field))
exit(-1)
if __name__ == "__main__":
toJSONFilter(metavars)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment