pandocfilters


Namepandocfilters JSON
Version 1.5.0 PyPI version JSON
download
home_pagehttp://github.com/jgm/pandocfilters
SummaryUtilities for writing pandoc filters in python
upload_time2021-09-14 03:37:58
maintainer
docs_urlNone
authorJohn MacFarlane
requires_python>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
licenseBSD-3-Clause
keywords pandoc
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            pandocfilters
=============

A python module for writing `pandoc <http://pandoc.org/>`_ filters

What are 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`__.

__ http://johnmacfarlane.net/pandoc/scripting.html

For an alternative library for writing pandoc filters, with
a more "Pythonic" design, see `panflute`__.

__ https://github.com/sergiocorreia/panflute

Compatibility
----------------
Pandoc 1.16 introduced link and image `attributes` to the existing
`caption` and `target` arguments, requiring a change in pandocfilters
that breaks backwards compatibility. Consequently, you should use:

- pandocfilters version <= 1.2.4 for pandoc versions 1.12--1.15, and
- pandocfilters version >= 1.3.0 for pandoc versions >= 1.16.

Pandoc 1.17.3 (pandoc-types 1.17.*) introduced a new JSON format.
pandocfilters 1.4.0 should work with both the old and the new
format.

Installing
--------------
Run this inside the present directory::

    python setup.py install

Or install from PyPI::

    pip install pandocfilters

Available functions
----------------------
The main functions ``pandocfilters`` exports are

-  ``walk(x, action, format, meta)``

   Walk a tree, applying an action to every object. Returns a modified
   tree. An action is a function of the form
   ``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 (as supplied by the
      ``format`` argument of ``walk``)
   -  ``meta`` is the document's metadata

   The return of an action is either:

   -  ``None``: this means that the object should remain unchanged
   -  a pandoc object: this will replace the original object
   -  a list of pandoc objects: these will replace the original object;
      the list is merged with the neighbors of the original objects
      (spliced into the list the original object belongs to); returning
      an empty list deletes the object

-  ``toJSONFilter(action)``

   Like ``toJSONFilters``, but takes a single action as argument.

-  ``toJSONFilters(actions)``

   Generate a JSON-to-JSON filter from stdin to stdout

   The filter:

   -  reads a JSON-formatted pandoc document from stdin
   -  transforms it by walking the tree and performing the actions
   -  returns a new JSON-formatted pandoc document to stdout

   The argument ``actions`` is a list of functions of the form
   ``action(key, value, format, meta)``, as described in more detail
   under ``walk``.

   This function calls ``applyJSONFilters``, with the ``format``
   argument provided by the first command-line argument, if present.
   (Pandoc sets this by default when calling filters.)

-  ``applyJSONFilters(actions, source, format="")``

   Walk through JSON structure and apply filters

   This:

   -  reads a JSON-formatted pandoc document from a source string
   -  transforms it by walking the tree and performing the actions
   -  returns a new JSON-formatted pandoc document as a string

   The ``actions`` argument is a list of functions (see ``walk`` for a
   full description).

   The argument ``source`` is a string encoded JSON object.

   The argument ``format`` is a string describing the output format.

   Returns a new JSON-formatted pandoc document.

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

How to use
----------
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, Str

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

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

Examples
--------

The examples subdirectory in the source repository contains the
following filters. These filters should provide a useful starting point
for developing your own pandocfilters.

``abc.py``
    Pandoc filter to process code blocks with class ``abc`` containing ABC
    notation into images. Assumes that abcm2ps and ImageMagick's convert
    are in the path. Images are put in the abc-images directory.

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

``blockdiag.py``
    Pandoc filter to process code blocks with class "blockdiag" into
    generated images. Needs utils from http://blockdiag.com.

``comments.py``
    Pandoc filter that causes everything between
    ``<!-- BEGIN COMMENT -->`` and ``<!-- END COMMENT -->`` to be ignored.
    The comment lines must appear on lines by themselves, with blank
    lines surrounding

``deemph.py``
    Pandoc filter that causes emphasized text to be displayed in ALL
    CAPS.

``deflists.py``
    Pandoc filter to convert definition lists to bullet lists with the
    defined terms in strong emphasis (for compatibility with standard
    markdown).

``gabc.py``
    Pandoc filter to convert code blocks with class "gabc" to LaTeX
    \\gabcsnippet commands in LaTeX output, and to images in HTML output.

``graphviz.py``
    Pandoc filter to process code blocks with class ``graphviz`` into
    graphviz-generated images.

``lilypond.py``
    Pandoc filter to process code blocks with class "ly" containing
    Lilypond notation.

``metavars.py``
    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``.

``myemph.py``
    Pandoc filter that causes emphasis to be rendered using the custom
    macro ``\myemph{...}`` rather than ``\emph{...}`` in latex. Other output
    formats are unaffected.

``plantuml.py``
    Pandoc filter to process code blocks with class ``plantuml`` to images.
    Needs `plantuml.jar` from http://plantuml.com/.

``ditaa.py``
    Pandoc filter to process code blocks with class ``ditaa`` to images.
    Needs `ditaa.jar` from http://ditaa.sourceforge.net/.

``theorem.py``
    Pandoc filter to convert divs with ``class="theorem"`` to LaTeX theorem
    environments in LaTeX output, and to numbered theorems in HTML
    output.

``tikz.py``
    Pandoc filter to process raw latex tikz environments into images.
    Assumes that pdflatex is in the path, and that the standalone
    package is available. Also assumes that ImageMagick's convert is in
    the path. Images are put in the ``tikz-images`` directory.

API documentation
-----------------

By default most filters use ``get_filename4code`` to
create a directory ``...-images`` to save temporary
files. This directory doesn't get removed as it can be used as a cache so that
later pandoc runs don't have to recreate files if they already exist. The
directory is generated in the current directory.

If you prefer to have a clean directory after running pandoc filters, you
can set an environment variable ``PANDOCFILTER_CLEANUP`` to any non-empty value such as `1`
which forces the code to create a temporary directory that will be removed
by the end of execution.



            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/jgm/pandocfilters",
    "name": "pandocfilters",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
    "maintainer_email": "",
    "keywords": "pandoc",
    "author": "John MacFarlane",
    "author_email": "fiddlosopher@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/62/42/c32476b110a2d25277be875b82b5669f2cdda7897c165bd22b78f366b3cb/pandocfilters-1.5.0.tar.gz",
    "platform": "",
    "description": "pandocfilters\n=============\n\nA python module for writing `pandoc <http://pandoc.org/>`_ filters\n\nWhat are pandoc filters?\n--------------------------\nPandoc filters\nare pipes that read a JSON serialization of the Pandoc AST\nfrom stdin, transform it in some way, and write it to stdout.\nThey can be used with pandoc (>= 1.12) either using pipes ::\n\n    pandoc -t json -s | ./caps.py | pandoc -f json\n\nor using the ``--filter`` (or ``-F``) command-line option. ::\n\n    pandoc --filter ./caps.py -s\n\nFor more on pandoc filters, see the pandoc documentation under ``--filter``\nand `the tutorial on writing filters`__.\n\n__ http://johnmacfarlane.net/pandoc/scripting.html\n\nFor an alternative library for writing pandoc filters, with\na more \"Pythonic\" design, see `panflute`__.\n\n__ https://github.com/sergiocorreia/panflute\n\nCompatibility\n----------------\nPandoc 1.16 introduced link and image `attributes` to the existing\n`caption` and `target` arguments, requiring a change in pandocfilters\nthat breaks backwards compatibility. Consequently, you should use:\n\n- pandocfilters version <= 1.2.4 for pandoc versions 1.12--1.15, and\n- pandocfilters version >= 1.3.0 for pandoc versions >= 1.16.\n\nPandoc 1.17.3 (pandoc-types 1.17.*) introduced a new JSON format.\npandocfilters 1.4.0 should work with both the old and the new\nformat.\n\nInstalling\n--------------\nRun this inside the present directory::\n\n    python setup.py install\n\nOr install from PyPI::\n\n    pip install pandocfilters\n\nAvailable functions\n----------------------\nThe main functions ``pandocfilters`` exports are\n\n-  ``walk(x, action, format, meta)``\n\n   Walk a tree, applying an action to every object. Returns a modified\n   tree. An action is a function of the form\n   ``action(key, value, format, meta)``, where:\n\n   -  ``key`` is the type of the pandoc object (e.g. 'Str', 'Para')\n   -  ``value`` is the contents of the object (e.g. a string for 'Str', a list of\n      inline elements for 'Para')\n   -  ``format`` is the target output format (as supplied by the\n      ``format`` argument of ``walk``)\n   -  ``meta`` is the document's metadata\n\n   The return of an action is either:\n\n   -  ``None``: this means that the object should remain unchanged\n   -  a pandoc object: this will replace the original object\n   -  a list of pandoc objects: these will replace the original object;\n      the list is merged with the neighbors of the original objects\n      (spliced into the list the original object belongs to); returning\n      an empty list deletes the object\n\n-  ``toJSONFilter(action)``\n\n   Like ``toJSONFilters``, but takes a single action as argument.\n\n-  ``toJSONFilters(actions)``\n\n   Generate a JSON-to-JSON filter from stdin to stdout\n\n   The filter:\n\n   -  reads a JSON-formatted pandoc document from stdin\n   -  transforms it by walking the tree and performing the actions\n   -  returns a new JSON-formatted pandoc document to stdout\n\n   The argument ``actions`` is a list of functions of the form\n   ``action(key, value, format, meta)``, as described in more detail\n   under ``walk``.\n\n   This function calls ``applyJSONFilters``, with the ``format``\n   argument provided by the first command-line argument, if present.\n   (Pandoc sets this by default when calling filters.)\n\n-  ``applyJSONFilters(actions, source, format=\"\")``\n\n   Walk through JSON structure and apply filters\n\n   This:\n\n   -  reads a JSON-formatted pandoc document from a source string\n   -  transforms it by walking the tree and performing the actions\n   -  returns a new JSON-formatted pandoc document as a string\n\n   The ``actions`` argument is a list of functions (see ``walk`` for a\n   full description).\n\n   The argument ``source`` is a string encoded JSON object.\n\n   The argument ``format`` is a string describing the output format.\n\n   Returns a new JSON-formatted pandoc document.\n\n-  ``stringify(x)``\n\n   Walks the tree x and returns concatenated string content, leaving out\n   all formatting.\n\n-  ``attributes(attrs)``\n\n   Returns an attribute list, constructed from the dictionary attrs.\n\nHow to use\n----------\nMost users will only need ``toJSONFilter``.  Here is a simple example\nof its use::\n\n    #!/usr/bin/env python\n\n    \"\"\"\n    Pandoc filter to convert all regular text to uppercase.\n    Code, link URLs, etc. are not affected.\n    \"\"\"\n\n    from pandocfilters import toJSONFilter, Str\n\n    def caps(key, value, format, meta):\n      if key == 'Str':\n        return Str(value.upper())\n\n    if __name__ == \"__main__\":\n      toJSONFilter(caps)\n\nExamples\n--------\n\nThe examples subdirectory in the source repository contains the\nfollowing filters. These filters should provide a useful starting point\nfor developing your own pandocfilters.\n\n``abc.py``\n    Pandoc filter to process code blocks with class ``abc`` containing ABC\n    notation into images. Assumes that abcm2ps and ImageMagick's convert\n    are in the path. Images are put in the abc-images directory.\n\n``caps.py``\n    Pandoc filter to convert all regular text to uppercase. Code, link\n    URLs, etc. are not affected.\n\n``blockdiag.py``\n    Pandoc filter to process code blocks with class \"blockdiag\" into\n    generated images. Needs utils from http://blockdiag.com.\n\n``comments.py``\n    Pandoc filter that causes everything between\n    ``<!-- BEGIN COMMENT -->`` and ``<!-- END COMMENT -->`` to be ignored.\n    The comment lines must appear on lines by themselves, with blank\n    lines surrounding\n\n``deemph.py``\n    Pandoc filter that causes emphasized text to be displayed in ALL\n    CAPS.\n\n``deflists.py``\n    Pandoc filter to convert definition lists to bullet lists with the\n    defined terms in strong emphasis (for compatibility with standard\n    markdown).\n\n``gabc.py``\n    Pandoc filter to convert code blocks with class \"gabc\" to LaTeX\n    \\\\gabcsnippet commands in LaTeX output, and to images in HTML output.\n\n``graphviz.py``\n    Pandoc filter to process code blocks with class ``graphviz`` into\n    graphviz-generated images.\n\n``lilypond.py``\n    Pandoc filter to process code blocks with class \"ly\" containing\n    Lilypond notation.\n\n``metavars.py``\n    Pandoc filter to allow interpolation of metadata fields into a\n    document. ``%{fields}`` will be replaced by the field's value, assuming\n    it is of the type ``MetaInlines`` or ``MetaString``.\n\n``myemph.py``\n    Pandoc filter that causes emphasis to be rendered using the custom\n    macro ``\\myemph{...}`` rather than ``\\emph{...}`` in latex. Other output\n    formats are unaffected.\n\n``plantuml.py``\n    Pandoc filter to process code blocks with class ``plantuml`` to images.\n    Needs `plantuml.jar` from http://plantuml.com/.\n\n``ditaa.py``\n    Pandoc filter to process code blocks with class ``ditaa`` to images.\n    Needs `ditaa.jar` from http://ditaa.sourceforge.net/.\n\n``theorem.py``\n    Pandoc filter to convert divs with ``class=\"theorem\"`` to LaTeX theorem\n    environments in LaTeX output, and to numbered theorems in HTML\n    output.\n\n``tikz.py``\n    Pandoc filter to process raw latex tikz environments into images.\n    Assumes that pdflatex is in the path, and that the standalone\n    package is available. Also assumes that ImageMagick's convert is in\n    the path. Images are put in the ``tikz-images`` directory.\n\nAPI documentation\n-----------------\n\nBy default most filters use ``get_filename4code`` to\ncreate a directory ``...-images`` to save temporary\nfiles. This directory doesn't get removed as it can be used as a cache so that\nlater pandoc runs don't have to recreate files if they already exist. The\ndirectory is generated in the current directory.\n\nIf you prefer to have a clean directory after running pandoc filters, you\ncan set an environment variable ``PANDOCFILTER_CLEANUP`` to any non-empty value such as `1`\nwhich forces the code to create a temporary directory that will be removed\nby the end of execution.\n\n\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "Utilities for writing pandoc filters in python",
    "version": "1.5.0",
    "split_keywords": [
        "pandoc"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "f183cea1efb5321287b8b46d9fe6c712",
                "sha256": "33aae3f25fd1a026079f5d27bdd52496f0e0803b3469282162bafdcbdf6ef14f"
            },
            "downloads": -1,
            "filename": "pandocfilters-1.5.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f183cea1efb5321287b8b46d9fe6c712",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
            "size": 8667,
            "upload_time": "2021-09-14T03:37:57",
            "upload_time_iso_8601": "2021-09-14T03:37:57.378830Z",
            "url": "https://files.pythonhosted.org/packages/5e/a8/878258cffd53202a6cc1903c226cf09e58ae3df6b09f8ddfa98033286637/pandocfilters-1.5.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "d625fec43c27f091e465ff28df763a66",
                "sha256": "0b679503337d233b4339a817bfc8c50064e2eff681314376a47cb582305a7a38"
            },
            "downloads": -1,
            "filename": "pandocfilters-1.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d625fec43c27f091e465ff28df763a66",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
            "size": 8405,
            "upload_time": "2021-09-14T03:37:58",
            "upload_time_iso_8601": "2021-09-14T03:37:58.577374Z",
            "url": "https://files.pythonhosted.org/packages/62/42/c32476b110a2d25277be875b82b5669f2cdda7897c165bd22b78f366b3cb/pandocfilters-1.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-09-14 03:37:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "jgm",
    "github_project": "pandocfilters",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pandocfilters"
}
        
Elapsed time: 0.01324s