-
Notifications
You must be signed in to change notification settings - Fork 4
/
pandoc_filter.py
executable file
·46 lines (34 loc) · 1.21 KB
/
pandoc_filter.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
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import yaml
import sys
import re
from typing import NamedTuple, List, Any
from latex_filters.filters import *
from latex_filters import PandocState
from pandocfilters import toJSONFilters
from pandocfilters import Math, Str, RawInline, Para, Header
class NumberedEnvHintFilter(CustomNumberedEnvFilter):
"""
Convert a custom numbered environment (created with \\newtheorem in LaTeX) but
add hint tags
"""
def custom_numbered_env(self, code:str, env):
"""
Process a custom numbered environment
"""
header, output = super().custom_numbered_env(code, env)
hint_tag = Para([Str("{% hint style=\"info\" %}")])
end_hint_tag = Para([Str("{% endhint %}")])
return [hint_tag, header, output, end_hint_tag]
class TextDegreeFilter(PandocFilter):
def __call__(self, key:str, value:Any, fmt:str, meta:Any):
if key == "RawInline":
[fmt, code] = value
if fmt == "latex" and re.match("\\\\textdegree", code):
return Str("˚")
if __name__ == '__main__':
with open("pandoc.yaml", 'r') as f:
config = yaml.safe_load(f)
state = PandocState()
filters = [eval(conf['class_name'])(conf['config'], state) for conf in config]
toJSONFilters(filters)