User guide

A Simple filter

Suppose we want to create a filter that sets all headers to level 1. For this, write this python script:

"""
Set all headers to level 1
"""

from panflute import *

def action(elem, doc):
    if isinstance(elem, Header):
        elem.level = 1

def main(doc=None):
    return run_filter(action, doc=doc) 

if __name__ == '__main__':
    main()

Note

a more complete template is located here

Tip

import panflute as pf (as in the template) makes a filter start faster than from panflute import *: since panflute 3.0, the first one does not import the modules used by panflute’s command line tools (such as click), while the star import still imports all of them. This saves a few tens of milliseconds every time pandoc runs the filter.

More complex filters

We might want filters that replace an element instead of just modifying it. For instance, suppose we want to replace all emphasized text with striked out text:

"""
Replace Emph elements with Strikeout elements
"""

from panflute import *


def action(elem, doc):
    if isinstance(elem, Emph):
        return Strikeout(*elem.content)


def main(doc=None):
    return run_filter(action, doc=doc)


if __name__ == '__main__':
    main()

Or if we want to remove all tables:

"""
Remove all tables
"""

from panflute import *


def action(elem, doc):
    if isinstance(elem, Table):
        return []


def main(doc=None):
    return run_filter(action, doc=doc)


if __name__ == '__main__':
    main()

Globals and backmatter

Suppose we want to add a table of contents based on all headers, or move all tables to a specific location in the document. This requires tracking global variables (which can be stored as attributes of doc).

To add a table of contents at the beginning:

"""
Add table of contents at the beginning;
uses optional metadata value 'toc-depth'
"""

from panflute import *


def prepare(doc):
    doc.toc = BulletList()
    doc.depth = int(doc.get_metadata('toc-depth', default=1))


def action(elem, doc):
    if isinstance(elem, Header) and elem.level <= doc.depth:
        item = ListItem(Plain(*elem.content))
        doc.toc.content.append(item)


def finalize(doc):
    doc.content.insert(0, doc.toc)
    del doc.toc, doc.depth


def main(doc=None):
    return run_filter(action, prepare=prepare, finalize=finalize, doc=doc) 


if __name__ == '__main__':
    main()

To move all tables to the place where the string $tables is:

"""
Move tables to where the string $tables is.
"""

from panflute import *


def prepare(doc):
    doc.backmatter = []


def action(elem, doc):
    if isinstance(elem, Table):
        doc.backmatter.append(elem)
        return []


def finalize(doc):
    div = Div(*doc.backmatter)
    doc = doc.replace_keyword('$tables', div)


def main(doc=None):
    return run_filter(action, prepare, finalize, doc=doc)


if __name__ == '__main__':
    main()

Using the included batteries

There are several functions and methods that make your life easier, such as the replace_keyword method shown above.

Other useful functions include convert_text (to load and parse markdown or other formatted text) and stringify (to extract the underlying text from an element and its children). For metadata, you can use the doc.get_metadata attribute to extract user–specified options (booleans, strings, etc.)

For instance, you can combine these functions to allow for include directives (so you can include and parse markdown files from other files).

"""
Panflute filter to allow file includes

Each include statement has its own line and has the syntax:

    $include ../somefolder/somefile

Each include statement must be in its own paragraph. That is, in its own line
and separated by blank lines.

If no extension was given, ".md" is assumed.
"""

import os
import panflute as pf


def is_include_line(elem):
    if len(elem.content) < 3:
        return False
    elif not all (isinstance(x, (pf.Str, pf.Space)) for x in elem.content):
        return False
    elif elem.content[0].text != '$include':
        return False
    elif type(elem.content[1]) != pf.Space:
        return False
    else:
        return True


def get_filename(elem):
    fn = pf.stringify(elem, newlines=False).split(maxsplit=1)[1]
    if not os.path.splitext(fn)[1]:
        fn += '.md'
    return fn


def action(elem, doc):
    if isinstance(elem, pf.Para) and is_include_line(elem):
        
        fn = get_filename(elem)
        if not os.path.isfile(fn):
            return
        
        with open(fn) as f:
            raw = f.read()

        new_elems = pf.convert_text(raw)
        
        # Alternative A:
        return new_elems
        # Alternative B:
        # div = pf.Div(*new_elems, attributes={'source': fn})
        # return div


def main(doc=None):
    return pf.run_filter(action, doc=doc) 


if __name__ == '__main__':
    main()

YAML code blocks

A YAML filter is a filter that parses fenced code blocks that contain YAML metadata. For instance:

Some text

~~~ csv
title: Some Title
has-header: True
---
Col1, Col2, Col3
1, 2, 3
10, 20, 30
~~~

More text

Note that fenced code blocks use three or more tildes or backticks as separators. Within a code block, use three hyphens or three dots to separate the YAML options from the rest of the block.

As an example, we will design a filter that will be applied to all code blocks with the csv class, like the one shown above. To avoid boilerplate code (such as parsing the YAML part), we use the useful yaml_filter function:

"""
Panflute filter to parse CSV in fenced YAML code blocks
"""

import io
import csv
import panflute as pf


def fenced_action(options, data, element, doc):
    # We'll only run this for CodeBlock elements of class 'csv'
    title = options.get('title', 'Untitled Table')
    title = [pf.Str(title)]
    has_header = options.get('has-header', False)

    with io.StringIO(data) as f:
        reader = csv.reader(f)
        body = []
        for row in reader:
            cells = [pf.TableCell(pf.Plain(pf.Str(x))) for x in row]
            body.append(pf.TableRow(*cells))

    head = pf.TableHead(body.pop(0)) if has_header else None
    caption = pf.Caption(pf.Plain(*title))
    table = pf.Table(pf.TableBody(*body), head=head, caption=caption)
    return table


def main(doc=None):
    return pf.run_filter(pf.yaml_filter, tag='csv', function=fenced_action,
                      doc=doc)


if __name__ == '__main__':
    main()

Note

a more complete template is here , a fully developed filter for CSVs is also available.

Note

yaml_filter now allows a strict_yaml=True option, which allows multiple YAML blocks, but with the caveat that all YAML blocks must start with — and end with — or …. In this mode, a YAML block that cannot be parsed, or that is not a mapping of key: value pairs, is reported on stderr and the code block is left unchanged.

Calling external programs

We might also want to embed results from other programs.

One option is to do so through Python’s internals. For instance, we can use fetch data from wikipedia and show it on the document. Thus, the following script will replace links like these: [Pandoc](wiki://) With this “Pandoc is a free and open-source software document converter…”.

"""
Panflute filter that embeds wikipedia text

Replaces markdown such as [Stack Overflow](wiki://) with the resulting text.
"""

import requests
import panflute as pf


def action(elem, doc):
    if isinstance(elem, pf.Link) and elem.url.startswith('wiki://'):
        title = pf.stringify(elem).strip()
        baseurl = 'https://en.wikipedia.org/w/api.php'
        query = {'format': 'json', 'action': 'query', 'prop': 'extracts',
            'explaintext': '', 'titles': title}
        r = requests.get(baseurl, params=query)
        data = r.json()
        extract = list(data['query']['pages'].values())[0]['extract']
        extract = extract.split('.', maxsplit=1)[0]
        return pf.RawInline(extract)


def main(doc=None):
    return pf.run_filter(action, doc=doc) 


if __name__ == '__main__':
    main()

Alternatively, we might want to run other programs through the shell. For this, explore the shell function.

List containers and slices

The .content of an element (and other lists of elements, such as the .citations of a Cite) is a list container, a ListContainer: a list-like object that only accepts elements of the right type, and that sets their .parent and .index_in_parent. It supports the usual list operations (append, insert, del, elem.content[i:j] = [...], etc.).

A slice such as para.content[1:3] is a new container that holds the same elements; changing the slice does not change para.content, and reading it does not change the .parent or .index_in_parent of the elements. A slice is an instance of a (private) subclass of ListContainer, so check its type with isinstance(s, ListContainer), or use list(s) to get a plain list.

Raw formats

RawBlock and RawInline hold text in a given output format, which pandoc copies as-is into documents of that format. Any format name is accepted, as in pandoc, for instance 'html', 'latex', 'typst', or 'comment' (which pandoc drops from every output), or the name of a custom writer:

def action(elem, doc):
    if isinstance(elem, RawBlock) and elem.format == 'comment':
        return []  # Delete comments, whatever the output format

page_break = RawBlock('#pagebreak()', format='typst')

(Before panflute 3.0, only a fixed list of formats was accepted, so documents with other raw formats could not be loaded.)

Running filters automatically

If you run panflute as a filter (pandoc ... -F panflute), then panflute will run all filters specified in the metadata field panflute-filters. This is faster and more convenient than typing the precise list and order of filters used every time the document is run.

You can also specify the location of the filters with the panflute-path field, which will take precedence over Pandoc’s search locations (., $datadir/filters, and $path).

Example:

---
title: Some title
panflute-filters: [remove-tables, include]
panflute-path: 'panflute/docs/source'
...

Lorem ipsum

For this to work, the filters need to have a very specific structure, with a main() function of the following form:

"""
Pandoc filter using panflute
"""

import panflute as pf


def prepare(doc):
    pass


def action(elem, doc):
    if isinstance(elem, pf.Element) and doc.format == 'latex':
        pass
        # return None -> element unchanged
        # return [] -> delete element


def finalize(doc):
    pass


def main(doc=None):
    return pf.run_filter(action,
                         prepare=prepare,
                         finalize=finalize,
                         doc=doc) 


if __name__ == '__main__':
    main()

Note

To be able to run filters automatically, the main function needs to be exactly as shown, with an optional argument doc, that gets passed to run_filter, and which is return ed back.

Note

You can add panflute-verbose: true to the metadata to display debugging information, including the folders searched and the filters executed.

Command line tools

Installing panflute also installs two command line programs:

  • panflute is the filter used above (pandoc -F panflute): it runs the filters listed in the metadata. panflute --help describes it, and panflute --version prints panflute’s version.

  • panfl can do the same (pandoc -F panfl). It can also run the filters given on its command line, in a pipeline of pandoc commands; there it needs the output format (-t/--to) and accepts several filters, which it searches for in the directories given with -d (--dir), in pandoc’s data directory (--data-dir), and in Python’s sys.path (unless --no-sys-path):

    pandoc example.md -t json | panfl foo.bar -t markdown | pandoc -f json
    

    Run panfl --help for all the options.

Type hints

Panflute comes with type hints, so a type checker (mypy, pyright, …) or an IDE can check your filters and complete the attributes of each element. Annotate an action like this:

import panflute as pf

def action(elem: pf.Element, doc: pf.Doc) -> pf.Element | list[pf.Element] | None:
    if isinstance(elem, pf.Str):
        return pf.Str(elem.text.upper())  # replace the element
    if isinstance(elem, pf.Emph):
        return []  # delete the element
    return None  # keep the element

def prepare(doc: pf.Doc) -> None:
    doc.count = 0  # filters can store their own attributes in the Doc

def main(doc: pf.Doc | None = None) -> pf.Doc | None:
    return pf.run_filter(action, prepare=prepare, doc=doc)

isinstance() checks narrow the type of elem, so that e.g. elem.text is known to be a str after isinstance(elem, pf.Str). Arguments that only take a few values have Literal types (for instance Quoted(quote_type='SingleQuote') or OrderedList(style='LowerRoman')), so a typo is reported by the type checker.

The types are simplified in a few places, deliberately:

  • The doc passed to an action is typed Doc, although it is None when walk() is called on an element that is not part of a document.

  • .identifier, .classes and .attributes are declared on every element, although only the elements that have attributes (such as Div, Header or Code) have them.

  • Panflute accepts an element class where an element is expected (Para(Str('a'), Space) instead of Para(Str('a'), Space())), but the type hints only accept instances.