Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bpj
Last active October 30, 2016 23:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpj/e6e53cbe679d3ec77e25 to your computer and use it in GitHub Desktop.
Save bpj/e6e53cbe679d3ec77e25 to your computer and use it in GitHub Desktop.
Pandoc filter to insert arbitrary raw output markup as Code/CodeBlocks with an attribute raw=<outputformat>.
#!/usr/bin/env python
r"""Pandoc filter to insert arbitrary raw output markup
as Code/CodeBlocks with an attribute raw=<outputformat>.
Especially useful for inserting LaTeX code which pandoc will
otherwise mangle:
````{raw=latex}
\let\Begin\begin
\let\End\end
````
or for making HTML opaque to pandoc, which will otherwise
show the text between tags in other output formats,
or for allowing Markdown in the arguments of LaTeX commands
or the contents of LaTeX environments
`\textsf{`{raw=latex}<span class=sans>San Seriffe</span>`}`{raw=latex}
````{raw=latex}
\begin{center}
````
This is *centered*!
````{raw=latex}
\end{center}
````
or for header-includes in metadata:
---
header-includes: |
````{raw=latex}
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper]
````
...
See <https://github.com/jgm/pandoc/issues/2139>
"""
from pandocfilters import RawInline, RawBlock, toJSONFilter
from pandocattributes import PandocAttributes
raw4code = { 'Code': RawInline, 'CodeBlock': RawBlock }
def code2raw(key, val, format, meta):
if not key in raw4code:
return None
attrs = PandocAttributes(val[0], format='pandoc')
raw = attrs.kvs.get('raw', None)
if raw:
# if raw != format: # but what if we output markdown?
# return []
return raw4code[key](raw, val[-1])
else:
return None
if __name__ == "__main__":
toJSONFilter(code2raw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment