Python filter for pandoc to replace the original pandoc-include-code that is no longer supported (https://github.com/owickstrom/pandoc-include-code)
from .main import action, main

src/py_pandoc_include_code/__init__.py
veneres/py-pandoc-include-code
#!/usr/bin/env python

from panflute import *


def action(elem, _):
    if not isinstance(elem, CodeBlock):
        return
    path = elem.attributes.get("include", None)

    if path is None:
        return

    snippet = elem.attributes.get("snippet", None)

    with open(path, "r") as f:
        lines = f.readlines()

    code_lines = "".join(lines)

    if snippet is not None:
        # if snippet is not none include only the lines between
        # f"// start snippet {snippet}"
        # and
        # f"// end snippet {snippet}"
        code_lines = ""
        read = False
        for line in lines:
            line = line.strip()

            if read is False and line == f"// start snippet {snippet}":
                read = True
                continue

            if read is True and line == f"// end snippet {snippet}":
                break

            if read:
                code_lines += f"{line}\n"

    return CodeBlock(code_lines, elem.identifier, elem.classes, {})


def main(doc=None):
    return run_filter(action, doc=doc)


if __name__ == '__main__':
    main()

src/py_pandoc_include_code/main.py
veneres/py-pandoc-include-code
import unittest
from panflute import run_filter
from py_pandoc_include_code import action
import io


class TestSum(unittest.TestCase):

    def test_snippet_filet(self):
        test_file = open("resources/test.json", "r")
        output_res = io.StringIO()

        run_filter(action, input_stream=test_file, output_stream=output_res)

        res_lines = output_res.getvalue()
        output_res.close()
        test_file.close()

        with open("resources/expected_res.json", "r") as f:
            oracle = "".join(f.readlines())

        self.assertEqual(res_lines, oracle)

if __name__ == '__main__':
    unittest.main()
test/main.py
veneres/py-pandoc-include-code