File: inlinenotes.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 (31 lines) | stat: -rw-r--r-- 902 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
30
31
#!/usr/bin/env python3

from pandocfilters import toJSONFilter, RawInline, Space, Str, walk

"""
Pandoc filter for Markdown that converts most endnotes into
Pandoc's inline notes. If input notes had multiple paragraphs,
the paragraphs are joined by a space. But if an input note
had any blocks other than paragraphs, the note is left as is.
"""

def query(k, v, f, meta):
    global inlines
    if k == 'BlockQuote':
        inlines.append(v)
    elif isinstance(v, list):
        if inlines and k == 'Para':
            inlines.append(Space())
        inlines.extend(v)
    return v

def inlinenotes(k, v, f, meta):
    global inlines
    inlines = []
    if k == 'Note' and f == 'markdown':
       walk(v, query, f, meta)
       if all(isinstance(x, dict) for x in inlines):
           return [RawInline('html', '^[')] + inlines + [Str(']')]

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