Skip to main content

Utilities for writing pandoc filters in python

Project description

A python module for writing pandoc filters. Pandoc filters are pipes that read a JSON serialization of the Pandoc AST from stdin, transform it in some way, and write it to stdout. They can be used with pandoc (>= 1.12) either using pipes::

pandoc -t json -s | ./caps.py | pandoc -f json

or using the --filter (or -F) command-line option::

pandoc --filter ./caps.py -s

For more on pandoc filters, see the pandoc documentation under --filter and the tutorial on writing filters.

To install:

python setup.py install

The source repository contains a number of illustrative examples in the examples subdirectory.

The pandocfilters module exports the following functions:

walk(x, action, format, meta)

Walk a tree, applying an action to every object. Returns a modified tree.

toJSONFilter(action)

Converts an action into a filter that reads a JSON-formatted pandoc document from stdin, transforms it by walking the tree with the action, and returns a new JSON-formatted pandoc document to stdout. The argument is a function action(key, value, format, meta), where key is the type of the pandoc object (e.g. ‘Str’, ‘Para’), value is the contents of the object (e.g. a string for ‘Str’, a list of inline elements for ‘Para’), format is the target output format (which will be taken for the first command line argument if present), and meta is the document’s metadata. If the function returns None, the object to which it applies will remain unchanged. If it returns an object, the object will be replaced. If it returns a list, the list will be spliced in to the list to which the target object belongs. (So, returning an empty list deletes the object.)

stringify(x)

Walks the tree x and returns concatenated string content, leaving out all formatting.

attributes(attrs)

Returns an attribute list, constructed from the dictionary attrs.

Most users will only need toJSONFilter. Here is a simple example of its use::

#!/usr/bin/env python

"""
Pandoc filter to convert all regular text to uppercase.
Code, link URLs, etc. are not affected.
"""

from pandocfilters import toJSONFilter

def caps(key, value, format, meta):
  if key == 'Str':
    return {'Str': value.upper()}

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandocfilters-1.1.tar.gz (6.7 kB view details)

Uploaded Source

File details

Details for the file pandocfilters-1.1.tar.gz.

File metadata

  • Download URL: pandocfilters-1.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pandocfilters-1.1.tar.gz
Algorithm Hash digest
SHA256 13af030bac826fc52d440191b6b9cdd00c42d0378ffe2adb7026f8b794d3b5e7
MD5 c1095be10bcbdfe4d12e65e0b78827d3
BLAKE2b-256 594cf0b703eda696967b21d67e937dbc6e0c380e7bfa2ea31b814fa9898a906f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page