Skip to content

Commit

Permalink
Add support for a Jinja2 pandoc filter (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsyesika committed Aug 16, 2020
1 parent ec001cd commit fcc291a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ matrix:
install: pip install tox
script: tox -e $TOX_ENV

before_install:
- sudo apt update
- sudo apt -y install pandoc

install:
- pip install tox-travis codecov

Expand Down
11 changes: 11 additions & 0 deletions docs/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ keyword arguments.

Please view the `Markdown documentation <https://python-markdown.github.io/>`_ for details.

Pandoc
^^^^^^

``pandoc`` is provided as a filter and can be configured by the
``pandoc_config`` meta variable, which is passed to the convert_text function
as keyword arguments.

Please refer `pypandoc project <https://github.com/bebraw/pypandoc>`_ for details.

Note, pypandoc requires pandoc to be installed. It will error without it.

Typogrify
^^^^^^^^^

Expand Down
19 changes: 19 additions & 0 deletions exhibition/filters/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from jinja2.ext import Extension
from jinja2.nodes import CallBlock, Const, ContextReference
from markdown import markdown as md_func
from pypandoc import convert_text as pandoc_func
from typogrify.templatetags import jinja_filters as typogrify_filters

EXTENDS_TEMPLATE_TEMPLATE = """{%% extends "%s" %%}
Expand All @@ -53,6 +54,10 @@
"output_format": "html5",
}

DEFAULT_PANDOC_KWARGS = {
"to": "html",
}


def metasort(nodes, key=None, reverse=False):
"""
Expand Down Expand Up @@ -86,6 +91,19 @@ def markdown(ctx, text):
return md_func(text, **kwargs)


@contextfilter
def pandoc(ctx, text, fmt=None):
kwargs = DEFAULT_PANDOC_KWARGS.copy()
node = ctx[NODE_TMPL_VAR]

kwargs.update(node.meta.get("pandoc_config", {}))

if fmt is not None:
kwargs["format"] = fmt

return pandoc_func(text, **kwargs)


class RaiseError(Extension):
"""
Raise an exception during template rendering:
Expand Down Expand Up @@ -157,6 +175,7 @@ def content_filter(node, content):
extensions=[RaiseError, Mark],
autoescape=True,
)
env.filters["pandoc"] = pandoc
env.filters["markdown"] = markdown
env.filters["metasort"] = metasort
env.filters["metaselect"] = metaselect
Expand Down
44 changes: 44 additions & 0 deletions exhibition/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@
{% endfilter %}
""".strip()


PANDOC_TEMPLATE = """
{% filter pandoc("org") %}
* Hello
This /is/ *text*
{% endfilter %}
"""

PANDOC_TEMPLATE_WITHOUT_ARG = """
{% filter pandoc %}
* Hello
This /is/ *text*
{% endfilter %}
"""

TYPOG_TEMPLATE = """
{% filter typogrify %}
Hello -- how are you?
Expand Down Expand Up @@ -243,6 +260,33 @@ def test_markdown_filter(self):
result = jinja_filter(node, MD_TEMPLATE)
self.assertEqual(result, "<h2>Hello</h2>\n<p>This is <em>text</em></p>")

def test_pandoc_filter(self):
node = Node(mock.Mock(), None, meta={"templates": []})
node.is_leaf = False
result = jinja_filter(node, PANDOC_TEMPLATE)
self.assertEqual(
result,
"\n<h1 id=\"hello\">Hello</h1>\n"
"<p>This <em>is</em> <strong>text</strong></p>\n"
)

def test_pandoc_filter_no_argument(self):
meta = {"templates": []}
node = Node(mock.Mock(), None, meta=meta)
node.is_leaf = False

# First test with no arguments at all.
self.assertRaises(
TypeError,
jinja_filter,
node,
PANDOC_TEMPLATE_WITHOUT_ARG
)

node.meta["pandoc_config"] = {"format": "org", "to": "markdown"}
result = jinja_filter(node, PANDOC_TEMPLATE_WITHOUT_ARG)
self.assertEqual(result, "\nHello\n=====\n\nThis *is* **text**\n")

def test_typogrify_filter(self):
node = Node(mock.Mock(), None, meta={"templates": []})
node.is_leaf = False
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"markdown",
"ruamel.yaml",
"typogrify",
"pypandoc",
],
extras_require={
"docs": [
Expand Down

0 comments on commit fcc291a

Please sign in to comment.