-
-
Save jepio/3ecaa6bba2a53ff74f2e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3 | |
''' | |
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) | |
''' | |
from pandocfilters import toJSONFilter, RawBlock | |
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 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))] | |
if __name__ == '__main__': | |
toJSONFilter(mintedify) |
@FilterKaapi how are you using the filter? Starred assignment was new in Python 3, is it possible you are using python2?
@FilterKaapi the proper usage is pandoc --no-highlight --filter ./minted.py ...
. I seem to recall that there used to be a wrong way to pass the filter parameter in older versions of pandoc that would make it use the default python version (which in most cases is python2). How are you running pandoc with this filter?
@FilterKaapi found it! You have to set the executable bit on the script, otherwise it's run using the default python.
chmod +x minted.py
Hi @jepio
I'm currently trying to use your script in my thesis. I've installed Python v3. I've also installed pandocfilters
via pip.
pandoc --no-highlight --filter ./minted.py "$(INPUTDIR)"/*.md \
-o "$(OUTPUTDIR)/thesis.pdf" \
-H "$(STYLEDIR)/preamble.tex" \
--template="$(STYLEDIR)/template.tex" \
--bibliography="$(BIBFILE)" 2>pandoc.log \
--csl="$(STYLEDIR)/ref_format.csl" \
-V fontsize=12pt \
-V papersize=a4paper \
-V documentclass:report \
-N \
--latex-engine-opt='-shell-escape' \
--latex-engine=xelatex \
--verbose
I'm always getting the following error:
! Package minted Error: Missing Pygments output; \inputminted was
probably given a file that does not exist--otherwise, you may need
the outputdir package option, or may be using an incompatible build tool,
or may be using frozencache with a missing file.
In addition to the error message above I have the following in my log file of pandoc:
Error: cannot read infile: [Errno 2] No such file or directory: 'input.pyg'
system returned with code 256
Do you have any idea why this is happening?
Thanks for your help
Phil
Having the same issue as @iphilgood. Any ideas?
I had to change:
if format_ == 'latex' and classes:
language, *pos = classes
to
if format_ == 'latex' and classes:
_, language, *pos = classes
with pandocfilters==1.4.2
. The first element appears to be the string "sourceCode"
.
This simple filter works perfectly fine for me, thanks a lot @jepio!
Glad to hear that. I last used this in 2016 for my master thesis.
To handle beamer output(I thought beamer is latex. 🤔 ), use
format_ in ['latex', 'beamer']
And add {.fragile}
following section title
# section 1 {.fragile}
I wonder if there is way to automatically add it
@iphilgood I figured out it is mint weirdness. If your output directory (contains .aux files) is not the same as where tex src is, you need to explicitly tell minted by,
\usepackage[outputdir=OUTDIR]{minted}
Now, I need to figure out how to pass the outdir from Makefile.
I wonder if there is way to automatically add it
I made a hacked filter for doing this here https://github.com/alexfanqi/pandoc_beamer_solarized/blob/master/fragile.py
@jepio I am getting an error as follows:
I don't know
python
. How do I fix this issue?