-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex2utf.py
46 lines (39 loc) · 904 Bytes
/
latex2utf.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
46
from latexparser import *
import json
num_args = 'num args'
mapsto = 'mapsto'
NaN = 'NaN'
def jsonToDict(filename):
mydict = json.load( open( filename + ".json" ) )
return mydict
substitutions = jsonToDict("LateX substitutions")
def latex2utf(command):
parser = LatexParser(command)
parser.dfa()
utf = ""
t_list = parser.token_list
i = 0
while i < len(t_list):
token = t_list[i]
if token.token_type == LatexParser.OPERATION:
item = substitutions.get(token.token, 0)
try:
if item != 0:
num = item[num_args]
if num == 1:
arg = t_list[i + 1].token
utf = utf + item[mapsto][arg]
i = i + 2
elif num == 0:
utf = utf + item[mapsto][NaN]
i = i + 1
else:
utf = utf + token.token
i = i + 1
except:
utf = utf + token.token
i = i + 1
else:
utf = utf + token.token
i = i + 1
return utf