File: metavars.py

package info (click to toggle)
python-pandocfilters 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: python: 802; makefile: 26
file content (29 lines) | stat: -rwxr-xr-x 850 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3

"""
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.
"""

from pandocfilters import toJSONFilter, attributes, Span, Str
import re

pattern = re.compile('%\{(.*)\}$')


def metavars(key, value, format, meta):
    if key == 'Str':
        m = pattern.match(value)
        if m:
            field = m.group(1)
            result = meta.get(field, {})
            if 'MetaInlines' in result['t']:
                return Span(attributes({'class': 'interpolated',
                                        'field': field}),
                            result['c'])
            elif 'MetaString' in result['t']:
                return Str(result['c'])

if __name__ == "__main__":
    toJSONFilter(metavars)