forked from martinpflaum/latex_to_html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumitem.py
229 lines (174 loc) · 6.76 KB
/
enumitem.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from core import *
#\item[something] has no enumeration
#[TAG] changes arguments to [] type
"""
<ol class="enumeration" type="1">
<li value="(O1)">This is item three.</li>
<li value="(O2)">This is item fifty.</li>
<li value="(O3)">
Something Aweakkjlk
<ol class="enumeration" type="1">
<li value="">This is item three.</li>
<li value="(A5)">This is item fifty.</li>
<li value="(A6)">This is item one hundred.</li>
</ol>
</li>
</ol>
"""
class ItemizeItem(Element):
def __init__(self,modifiable_content,parent,label = "•"):
super().__init__("",parent)
self.label = label
self.children = [Undefined(label,self),Undefined(modifiable_content,self)]
def label_name(self):
return self.label#self.children[0].to_string()
def to_string(self):
out = "<li value='"+self.children[0].to_string()+"'>"
self.children = self.children[1:]
for child in self.children:
out += child.to_string()
out += "</li>"
return out
@staticmethod
def position(input):
return position_of(input,"\\item")
@staticmethod
def split_and_create(input,parent):
pre,content = split_on_next(input,"\\item")
if "\\item" in content:
content,post = split_on_next(content,"\\item")
post = "\\item" + post
else:
post = ""
label = ""
if first_char_brace(content,"["):
label,content = split_on_first_brace(content,"[","]")
elem_out = ItemizeItem(content,parent,label)
return pre,elem_out,post
class Itemize(Element):
current_index = 0
def __init__(self,modifiable_content,parent):
super().__init__(modifiable_content,parent)
def to_string(self):
out = "</p><ol class='enumeration'>"
for child in self.children:
out += child.to_string()
out += "</ol><p>"
return out
@staticmethod
def position(input):
return position_of(input,"\\begin{itemize}")
@staticmethod
def split_and_create(input,parent):
pre,content,post = begin_end_split(input,"\\begin{itemize}","\\end{itemize}")
elem_out = Itemize(content,parent)
elem_out.expand([ItemizeItem])
return pre,elem_out,post
class EnumerationItem(Element):
def __init__(self,modifiable_content,parent,label = None):
super().__init__("",parent)
self.label = ""
if label is None:
enumeration = self.search_class(Enumeration)
self.label = enumeration.generate_item_label()
else:
self.label = label
self.children = [Undefined(self.label,self),Undefined(modifiable_content,self)]
def label_name(self):
return self.label
def to_string(self):
out = "<li value='"+self.children[0].to_string()+"'>"
self.children = self.children[1:]
for child in self.children:
out += child.to_string()
out += "</li>"
return out
@staticmethod
def position(input):
return position_of(input,"\\item")
@staticmethod
def split_and_create(input,parent):
pre,content = split_on_next(input,"\\item")
if "\\item" in content:
content,post = split_on_next(content,"\\item")
post = "\\item" + post
else:
post = ""
label = None
if first_char_brace(content,"["):
label,content = split_on_first_brace(content,"[","]")
elem_out = EnumerationItem(content,parent,label)
return pre,elem_out,post
def enum_style_roman(index):
roman = ["i","ii","iii","iv","v","vi","vii","viii","ix","x"]
return roman[index].upper()
def enum_style_Roman(index):
roman = ["i","ii","iii","iv","v","vi","vii","viii","ix","x"]
return roman[index]
def enum_style_arabic(index):
return str(index + 1)
def enum_style_alph(index):
lowercase = 'abcdefghijklmnopqrstuvwxyz'
return lowercase[index]
def enum_style_Alph(index):
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
return uppercase[index]
def enum_style_empty(index):
return ""
enum_styles = {"\\roman":enum_style_roman,"\\Roman":enum_style_Roman,"\\arabic":enum_style_arabic,"\\alph":enum_style_alph,"\\Alph":enum_style_Alph}
class Enumeration(Element):
current_index = 0
def __init__(self,modifiable_content,parent,style_func,left,right):
super().__init__(modifiable_content,parent)
self.style_func,self.left,self.right = style_func,left,right
def to_string(self):
out = "</p><ol class='enumeration'>"
for child in self.children:
out += child.to_string()
out += "</ol><p>"
return out
@staticmethod
def position(input):
return position_of(input,"\\begin{enumerate}")
@staticmethod
def split_and_create(input,parent):
pre,content,post = begin_end_split(input,"\\begin{enumerate}","\\end{enumerate}")
style_func = None
left = ""
right = ""
if first_char_brace(content,"["):
options,content = split_on_first_brace(content,"[","]")
options_pre,options_post = split_on_next(options,"label")
options_post = remove_empty_at_begin(options_post)
options_post = options_post[1:]#remove =
options_label = ""
#print("options_post",options_post)
if first_char_brace(options_post,"{"):
options_label,options_post = split_on_first_brace(options_post)
tmp = options_post.split(",")[0]
options_label = options_label + tmp
for style in enum_styles.keys():
if style in options_label:
style_func = enum_styles[style]
tmp = options_label.split(style)
left = tmp[0]
_,right = split_on_next(tmp[1],"*")
break
if style_func is None:
style_func = enum_style_empty
left = options_label
else:
style_func = enum_style_arabic
right = "."
left = left.replace("\\sffamily","")
left = left.replace("{","")
left = left.replace("}","")
right = right.replace("\\sffamily","")
right = right.replace("{","")
right = right.replace("}","")
elem_out = Enumeration(content,parent,style_func,left,right)
elem_out.expand([EnumerationItem])
return pre,elem_out,post
def generate_item_label(self):
self.current_index = self.current_index + 1
return self.left + self.style_func(self.current_index-1)+ self.right