Skip to content

Commit

Permalink
feat(minted): add minted filter to project
Browse files Browse the repository at this point in the history
install `pandoc_minted` module as separate script
`pandoc-minted` that can be runned alone using
`pandoc --filter=pandoc-minted ...`

note: `minted` ctan package dependencies must
be respected to render correctly the pdf when
running the filter with pandoc
  • Loading branch information
andros21 committed Nov 1, 2022
1 parent cc96d54 commit 159cafa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
58 changes: 58 additions & 0 deletions pandoc_minted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
"""
Filter to wrap Pandoc's CodeBlocks into minted blocks when using latex.
Pandoc's `fence_code_attributes` can be used to provide:
- the language (first class)
- minted's argumentless options (following classes)
- minted's options with arguments (attributes)
see: https://gist.github.com/jepio/3ecaa6bba2a53ff74f2e
"""

from pandocfilters import RawBlock, toJSONFilter

TEMPLATE = r"""
\begin{{minted}}[{options}]{{{lang}}}
{cont}
\end{{minted}}
""".strip()


def latex(x):
return RawBlock("latex", x)


def join_options(opts):
return ",\n".join(opts)


def process_atts(kws):
"""Preprocess the attributes provided by pandoc - they come as a list of
2-lists, convert to a list of strings"""
return ["%s=%s" % (l, r) for l, r in kws]


def main():
"main entry point"

def mintedify(key, value, format_, meta):
if key == "CodeBlock":
(ident, classes, attributes), contents = value
if format_ == "latex" and classes:
language, *pos = classes
atts = process_atts(attributes)
return [
latex(
TEMPLATE.format(
lang=language,
options=join_options(pos + atts),
cont=contents,
)
)
]

toJSONFilter(mintedify)


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@
],
# Alternatively, if you want to distribute just a my_module.py, uncomment
# this:
py_modules=["pandoc_imagine"],
py_modules=["pandoc_imagine", "pandoc_minted"],
# To provide executable scripts, use entry points in preference to the
# "scripts" keyword. Entry points provide cross-platform support and allow
# pip to create the appropriate form of executable for the target platform.
entry_points={
"console_scripts": [
"pandoc-imagine = pandoc_imagine:main",
"pandoc-minted = pandoc_minted:main",
],
},
# List run-time dependencies here. These will be installed by pip when
Expand Down

0 comments on commit 159cafa

Please sign in to comment.