File: theorem.py

package info (click to toggle)
python-pandocfilters 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 232 kB
  • ctags: 90
  • sloc: python: 658; makefile: 17
file content (41 lines) | stat: -rwxr-xr-x 1,199 bytes parent folder | download | duplicates (5)
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
#!/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 pandocfilters import toJSONFilter, RawBlock, Div

theoremcount = 0


def latex(x):
    return RawBlock('latex', x)


def html(x):
    return RawBlock('html', x)


def theorems(key, value, format, meta):
    if key == 'Div':
        [[ident, classes, kvs], contents] = value
        if "theorem" in classes:
            if format == "latex":
                if ident == "":
                    label = ""
                else:
                    label = '\\label{' + ident + '}'
                return([latex('\\begin{theorem}' + label)] + contents +
                       [latex('\\end{theorem}')])
            elif format == "html" or format == "html5":
                global theoremcount
                theoremcount = theoremcount + 1
                newcontents = [html('<dt>Theorem ' + str(theoremcount) + '</dt>'),
                               html('<dd>')] + contents + [html('</dd>\n</dl>')]
                return Div([ident, classes, kvs], newcontents)

if __name__ == "__main__":
    toJSONFilter(theorems)