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 in ['latex', 'beamer']: |
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: |
23
|
|
|
if id != '': |
24
|
|
|
label = ' \\label{' + id + '}' |
25
|
|
|
else: |
26
|
|
|
label = '' |
27
|
|
|
|
28
|
|
|
currentProperties = dict(properties) |
29
|
|
|
if 'title' in currentProperties: |
30
|
|
|
title = '[' + currentProperties['title'] + ']' |
31
|
|
|
else: |
32
|
|
|
title = '' |
33
|
|
|
|
34
|
|
|
value[1] = [RawBlock('tex', '\\begin{' + environment + '}' + title + label)] + content + [RawBlock('tex', '\\end{' + environment + '}')] |
35
|
|
|
break |
36
|
|
|
|
37
|
|
|
def getDefined(meta): |
38
|
|
|
# Return the latex-environment defined in the meta |
39
|
|
|
if not hasattr(getDefined, 'value'): |
40
|
|
|
getDefined.value = {} |
41
|
|
|
if 'pandoc-latex-environment' in meta and meta['pandoc-latex-environment']['t'] == 'MetaMap': |
42
|
|
|
for environment, classes in meta['pandoc-latex-environment']['c'].items(): |
43
|
|
|
if classes['t'] == 'MetaList': |
44
|
|
|
getDefined.value[environment] = [] |
45
|
|
|
for klass in classes['c']: |
46
|
|
|
string = stringify(klass) |
47
|
|
|
if re.match('^[a-zA-Z][\w.:-]*$', string): |
48
|
|
|
getDefined.value[environment].append(string) |
49
|
|
|
getDefined.value[environment] = set(getDefined.value[environment]) |
50
|
|
|
return getDefined.value |
51
|
|
|
|
52
|
|
|
def main(): |
53
|
|
|
toJSONFilters([environment]) |
54
|
|
|
|
55
|
|
|
if __name__ == '__main__': |
56
|
|
|
main() |
57
|
|
|
|