-
Notifications
You must be signed in to change notification settings - Fork 59
/
theorem.py
33 lines (26 loc) · 1020 Bytes
/
theorem.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
#!/usr/bin/env python
"""
Pandoc filter to convert divs with class="theorem" to LaTeX
theorem environments in LaTeX output, and to numbered theorems
in HTML output.
"""
from panflute import Div, RawBlock, toJSONFilter
def prepare(doc):
doc.theoremcount = 0
def theorems(e, doc):
if type(e) == Div and 'theorem' in e.classes:
doc.theoremcount += 1
if doc.format == 'latex':
label = '\\label{' + e.identifier + '}' if e.identifier else ''
left = RawBlock('\\begin{theorem}' + label, format='latex')
right = RawBlock('\\end{theorem}', format='latex')
elif doc.format in ('html', 'html5'):
label = '<dt>Theorem {}</dt>\n<dd>'.format(doc.theoremcount)
left = RawBlock(label, format='html')
right = RawBlock('</dd>\n</dl>', format='html')
else:
return
e.content = [left] + list(e.content) + [right]
return e
if __name__ == "__main__":
toJSONFilter(theorems, prepare=prepare)