Skip to content

Commit 4c00aba

Browse files
domvwtNicklasTegner
andauthored
Parse lua filters in filters argument (#284)
* Add support for Lua filters * Update README * Formatting * Fix test for Windows * Fix text for Windows (remove trailing whitespace) * Add tests for mixed filters * Update test * Change tests so ordering is explicit * Tidy up test code Co-authored-by: NicklasTegner <[email protected]>
1 parent 0987d3c commit 4c00aba

File tree

5 files changed

+99
-4
lines changed

5 files changed

+99
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ Note that for citeproc tests to pass you'll need to have [pandoc-citeproc](https
305305
* [Kolen Cheung](https://github.com/ickc) - Implement `_get_pandoc_urls` for installing arbitrary version as well as the latest version of pandoc. Minor: README, Travis, setup.py.
306306
* [Rebecca Heineman](https://github.com/burgerbecky) - Added scanning code for finding pandoc in Windows
307307
* [Andrew Barraford](https://github.com/abarrafo) - Download destination.
308+
* [Jesse Widner](https://github.com/jwidner) & [Dominic Thorn](https://github.com/domvwt) - Add support for lua filters
308309
* [Alex Kneisel](https://github.com/hey-thanks/) - Added pathlib.Path support to convert_file.
309-
310+
311+
310312
## License
311313

312314
Pypandoc is available under MIT license. See LICENSE for more details. Pandoc itself is [available under the GPL2 license](https://github.com/jgm/pandoc/blob/master/COPYING.md).

poetry.lock

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pypandoc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def _convert_input(source, format, input_type, to, extra_args=(),
356356
if filters is not None:
357357
if isinstance(filters, string_types):
358358
filters = filters.split()
359-
f = ['--filter=' + x for x in filters]
359+
f = ['--lua-filter=' + x if x.endswith(".lua") else '--filter=' + x for x in filters]
360360
args.extend(f)
361361

362362
# To get access to pandoc-citeproc when we use a included copy of pandoc,

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ packages = [
3434
python = "^3.6"
3535

3636
[tool.poetry.dev-dependencies]
37+
pandocfilters = "^1.5.0"
3738

3839
[build-system]
3940
requires = ["poetry-core>=1.0.0"]

tests.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import subprocess
1111
import sys
1212
import tempfile
13+
import textwrap
1314
import unittest
1415
import warnings
1516
from pathlib import Path
@@ -307,6 +308,87 @@ def test_conversion_with_empty_filter(self):
307308
found = re.search(r'10.1038', written)
308309
self.assertTrue(found is None)
309310

311+
def test_conversion_with_python_filter(self):
312+
markdown_source = "**Here comes the content.**"
313+
python_source = '''\
314+
#!/usr/bin/env python
315+
316+
"""
317+
Pandoc filter to convert all regular text to uppercase.
318+
Code, link URLs, etc. are not affected.
319+
"""
320+
321+
from pandocfilters import toJSONFilter, Str
322+
323+
def caps(key, value, format, meta):
324+
if key == 'Str':
325+
return Str(value.upper())
326+
327+
if __name__ == "__main__":
328+
toJSONFilter(caps)
329+
'''
330+
python_source = textwrap.dedent(python_source)
331+
with closed_tempfile(".py", python_source) as tempfile:
332+
output = pypandoc.convert_text(
333+
markdown_source, to='html', format='md', outputfile=None, filters=tempfile
334+
).strip()
335+
expected = '<p><strong>HERE COMES THE CONTENT.</strong></p>'
336+
self.assertTrue(output == expected)
337+
338+
def test_conversion_with_lua_filter(self):
339+
markdown_source = "**Here comes the content.**"
340+
lua_source = """\
341+
-- taken from: https://pandoc.org/lua-filters.html
342+
function Strong(elem)
343+
return pandoc.SmallCaps(elem.c)
344+
end
345+
"""
346+
lua_source = textwrap.dedent(lua_source)
347+
with closed_tempfile(".lua", lua_source) as tempfile:
348+
output = pypandoc.convert_text(
349+
markdown_source, to='html', format='md', outputfile=None, filters=tempfile
350+
).strip()
351+
expected = '<p><span class="smallcaps">Here comes the content.</span></p>'
352+
self.assertTrue(output == expected)
353+
354+
def test_conversion_with_mixed_filters(self):
355+
markdown_source = "-0-"
356+
357+
lua = """\
358+
function Para(elem)
359+
return pandoc.Para(elem.content .. {{"{0}-"}})
360+
end
361+
"""
362+
lua = textwrap.dedent(lua)
363+
364+
python = """\
365+
#!/usr/bin/env python
366+
367+
from pandocfilters import toJSONFilter, Para, Str
368+
369+
def func(key, value, format, meta):
370+
if key == "Para":
371+
return Para(value + [Str("{0}-")])
372+
373+
if __name__ == "__main__":
374+
toJSONFilter(func)
375+
376+
"""
377+
python = textwrap.dedent(python)
378+
379+
with closed_tempfile(".lua", lua.format(1)) as temp1, closed_tempfile(".py", python.format(2)) as temp2:
380+
with closed_tempfile(".lua", lua.format(3)) as temp3, closed_tempfile(".py", python.format(4)) as temp4:
381+
output = pypandoc.convert_text(
382+
markdown_source, to="html", format="md", outputfile=None, filters=[temp1, temp2, temp3, temp4]
383+
).strip()
384+
expected = "<p>-0-1-2-3-4-</p>"
385+
self.assertTrue(output == expected)
386+
387+
output = pypandoc.convert_text(
388+
markdown_source, to="html", format="md", outputfile=None, filters=[temp3, temp1, temp4, temp2]
389+
).strip()
390+
expected = "<p>-0-3-1-4-2-</p>"
391+
self.assertTrue(output == expected)
310392

311393
def test_classify_pandoc_logging(self):
312394

0 commit comments

Comments
 (0)