1
|
|
|
#!/usr/bin/env python |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
Pandoc filter for adding LaTeX environement on specific div |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
from pandocfilters import toJSONFilters, RawBlock, stringify |
8
|
|
|
|
9
|
|
|
import re |
10
|
|
|
|
11
|
|
|
def environment(key, value, format, meta): |
12
|
|
|
# Is it a div and the right format? |
13
|
|
|
if key == 'Div' and format == 'latex': |
14
|
|
|
|
15
|
|
|
# Get the attributes |
16
|
|
|
[[id, classes, properties], content] = value |
17
|
|
|
|
18
|
|
|
currentClasses = set(classes) |
19
|
|
|
|
20
|
|
|
for environment, definedClasses in getDefined(meta).items(): |
21
|
|
|
# Is the classes correct? |
22
|
|
|
if currentClasses <= definedClasses and bool(currentClasses): |
23
|
|
|
value[1] = [RawBlock('tex', '\\begin{' + environment + '}')] + content + [RawBlock('tex', '\\end{' + environment + '}')] |
24
|
|
|
break |
25
|
|
|
|
26
|
|
|
def getDefined(meta): |
27
|
|
|
# Return the latex-environment defined in the meta |
28
|
|
|
if not hasattr(getDefined, 'value'): |
29
|
|
|
getDefined.value = {} |
30
|
|
|
if 'latex-environment' in meta and meta['latex-environment']['t'] == 'MetaMap': |
31
|
|
|
for environment, classes in meta['latex-environment']['c'].items(): |
32
|
|
|
if classes['t'] == 'MetaList': |
33
|
|
|
getDefined.value[environment] = [] |
34
|
|
|
for klass in classes['c']: |
35
|
|
|
string = stringify(klass) |
36
|
|
|
if re.match('^[a-zA-Z][\w.:-]*$', string): |
37
|
|
|
getDefined.value[environment].append(string) |
38
|
|
|
getDefined.value[environment] = set(getDefined.value[environment]) |
39
|
|
|
return getDefined.value |
40
|
|
|
|
41
|
|
|
def main(): |
42
|
|
|
toJSONFilters([environment]) |
43
|
|
|
|
44
|
|
|
if __name__ == '__main__': |
45
|
|
|
main() |
46
|
|
|
|
47
|
|
|
|