|
10 | 10 | import subprocess |
11 | 11 | import sys |
12 | 12 | import tempfile |
| 13 | +import textwrap |
13 | 14 | import unittest |
14 | 15 | import warnings |
15 | 16 | from pathlib import Path |
@@ -307,6 +308,87 @@ def test_conversion_with_empty_filter(self): |
307 | 308 | found = re.search(r'10.1038', written) |
308 | 309 | self.assertTrue(found is None) |
309 | 310 |
|
| 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) |
310 | 392 |
|
311 | 393 | def test_classify_pandoc_logging(self): |
312 | 394 |
|
|
0 commit comments