Skip to content

Instantly share code, notes, and snippets.

@wcaleb
Last active December 29, 2020 06:59
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wcaleb/17ca606788f9b4b9a36b to your computer and use it in GitHub Desktop.
Pandoc filter to convert all notes to inline notes in Pandoc Markdown output
#!/usr/bin/env python
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)
@Hipomenes
Copy link

Hi Caleb! I wonder what's the status of this filter. Is it still supported? How would you deploy it? I tried with this but it didn't work:

pandoc -t markdown --filter inlinenotes.py input.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment