-
Notifications
You must be signed in to change notification settings - Fork 0
/
tex2png.py
executable file
·31 lines (26 loc) · 1.18 KB
/
tex2png.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
#!/usr/bin/env python3
#: Pandoc filter to convert code blocks with class "tex2png" into
#: images.
#: Requires tex2png.sh.
import os
import sys
import subprocess
# from subprocess import Popen, PIPE, STDOUT
from pandocfilters import toJSONFilter, Para, Image, get_filename4code, get_caption, get_extension, get_value
def tex2png(key, value, format, _):
if key == 'CodeBlock':
[[ident, classes, keyvals], code] = value
if "tex2png" in classes:
caption, typef, keyvals = get_caption(keyvals)
filetype = get_extension(format, "png", html="png", latex="pdf")
dest = get_filename4code("tex2png", code, filetype)
abs_dest = os.path.abspath(os.path.expanduser(os.path.expandvars(dest)))
if not os.path.isfile(dest):
# sys.stderr.write('DEBUG code="' + code + '"\n')
p = subprocess.run(["tex2png.sh", abs_dest],
text=True,
input=code)
sys.stderr.write('Created image ' + dest + '\n')
return Para([Image([ident, [], keyvals], caption, [dest, typef])])
if __name__ == "__main__":
toJSONFilter(tex2png)