forked from blei-lab/edward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandoc-code2raw.py
62 lines (47 loc) · 1.46 KB
/
pandoc-code2raw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
"""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 key not 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)