forked from Ey321/HTMLyX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layouts.py
287 lines (252 loc) · 9.73 KB
/
layouts.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import insets
BEGIN_DEEPER = "\\begin_deeper"
TITLE_LAYOUT = "Title"
PART_LAYOUT = "Part"
SECTION_LAYOUT = "Section"
SUBSECTION_LAYOUT = "Subsection"
SUBSUBSECTION_LAYOUT = "Subsubsection"
PARAGRAPH_LAYOUT = "Paragraph"
SUBPARAGRAPH_LAYOUT = "Subparagraph"
STANDARD_LAYOUT = "Standard"
AUTHOR_LAYOUT = "Author"
DESCRIPTION = "Description"
UNNUMBERED_TITLE_LAYOUT = "Title*"
UNNUMBERED_PART_LAYOUT = "Part*"
UNNUMBERED_SECTION_LAYOUT = "Section*"
UNNUMBERED_SUBSECTION_LAYOUT = "Subsection*"
UNNUMBERED_SUBSUBSECTION_LAYOUT = "Subsubsection*"
UNNUMBERED_PARAGRAPH_LAYOUT = "Paragraph*"
UNNUMBERED_SUBPARAGRAPH_LAYOUT = "Subparagraph*"
DATE_LAYOUT = "Date"
LYX_CODE_LAYOUT = "LyX-Code"
PLAIN_LAYOUT = "Plain Layout"
TEXT_LAYOUTS = {TITLE_LAYOUT, AUTHOR_LAYOUT, DATE_LAYOUT, PART_LAYOUT,
SECTION_LAYOUT, SUBSECTION_LAYOUT,
SUBSUBSECTION_LAYOUT, PARAGRAPH_LAYOUT, SUBPARAGRAPH_LAYOUT,
UNNUMBERED_TITLE_LAYOUT, UNNUMBERED_PART_LAYOUT,
UNNUMBERED_SECTION_LAYOUT, UNNUMBERED_SUBSECTION_LAYOUT,
UNNUMBERED_SUBSUBSECTION_LAYOUT, UNNUMBERED_PARAGRAPH_LAYOUT,
UNNUMBERED_SUBPARAGRAPH_LAYOUT,
STANDARD_LAYOUT,
PLAIN_LAYOUT,
DESCRIPTION
}
LAYOUT_TAGS = {
TITLE_LAYOUT: 'h1 class="title"',
AUTHOR_LAYOUT: 'h1 class="author"',
PART_LAYOUT: 'h2 class="part"',
UNNUMBERED_PART_LAYOUT: 'h2 class="part"',
SECTION_LAYOUT: 'h3 class="section"',
UNNUMBERED_SECTION_LAYOUT: 'h3 class="section"',
SUBSECTION_LAYOUT: 'h4 class="subsection"',
UNNUMBERED_SUBSECTION_LAYOUT: 'h4 class="subsection"',
SUBSUBSECTION_LAYOUT: 'h5 class="subsubsection"',
UNNUMBERED_SUBSUBSECTION_LAYOUT: 'h5 class="subsubsection"',
PARAGRAPH_LAYOUT: 'h6 class="paragraph"',
UNNUMBERED_PARAGRAPH_LAYOUT: 'h6 class="paragraph"',
SUBPARAGRAPH_LAYOUT: 'h6 class="subparagraph"',
UNNUMBERED_SUBPARAGRAPH_LAYOUT: 'h6 class="subparagraph"',
STANDARD_LAYOUT: 'div class="standard"',
PLAIN_LAYOUT: 'div class="plain"',
DESCRIPTION: 'div class="description" style="font-weight: bold;"'
}
BEGIN_TAGS = {
}
LAYOUT_NUMBERING = {
PART_LAYOUT: r'<span style="display: block">\partname \thepart</span>',
SECTION_LAYOUT: r'<span>\thesection </span>',
SUBSECTION_LAYOUT: r'<span>\thesubsection </span>',
SUBSUBSECTION_LAYOUT: r'<span>\thesubsubsection </span>',
PARAGRAPH_LAYOUT: "",
SUBPARAGRAPH_LAYOUT: "",
}
ENUMERATE_LAYOUT = "Enumerate"
ITEMIZE_LAYOUT = "Itemize"
ITEM_TAG = "li"
LIST_TAG = {
ENUMERATE_LAYOUT: "ol",
ITEMIZE_LAYOUT: "ul"
}
ENUMERATE_TYPES = "1aiAI"
LIST_LAYOUTS = {ENUMERATE_LAYOUT, ITEMIZE_LAYOUT}
styles = {"bar", "uuline", "uwave",
"series",
"color",
"strikeout"}
VALID_COLORS = {
"inherit",
"default",
"black",
"blue",
"brown",
"cyan",
"darkgray",
"gray",
"green",
"lightgray",
"lime",
"magenta",
"olive",
"orange",
"pink",
"purple",
"red",
"teal",
"violet",
"white",
"yellow"
}
RTL_LANGUAGES = {"arabic", "hebrew"}
def parse_begin_layout(parser, outfile):
"""parses a layout, from \\begin layout to \\end_layout."""
assert parser.current_command() == "\\begin_layout"
parameters = parser.current_parameters()
assert len(parameters) >= 1
layout_type = " ".join(parameters)
if layout_type in TEXT_LAYOUTS:
parse_text_layout(parser, outfile)
elif layout_type == LYX_CODE_LAYOUT:
outfile.write('<div class="lyx-code">')
parse_lyx_code(parser, outfile)
outfile.write('</div>')
elif layout_type in LIST_LAYOUTS:
parse_list_layout(parser, outfile)
else:
raise Exception(f"Unsupported layout type in {parser.current_line[:-1]}")
def parse_lyx_code(parser, outfile, indent=0):
""" parses the lyx code"""
while True:
if parser.current_command() != "\\begin_layout" or \
parser.current_parameters()[0] != LYX_CODE_LAYOUT:
break
parser.advance()
if not parser.is_current_command():
outfile.write('<div>')
parse_text(parser, outfile, indent=indent)
outfile.write('</div>')
if parser.current_command() == "\\end_layout" and \
parser.next_command() == "\\begin_deeper":
assert parser.current_command() == "\\end_layout"
parser.advance() # \end_layout
assert parser.current_command() == "\\begin_deeper"
parser.advance() # \begin_deeper
parse_lyx_code(parser, outfile, indent=indent + 1)
assert parser.current_command() == "\\end_deeper"
parser.advance() # \end_deeper
if parser.current_command() == "\\end_layout" and \
parser.next_command() == "\\begin_layout" and \
parser.next_parameters()[0] == LYX_CODE_LAYOUT:
assert parser.current_command() == "\\end_layout"
parser.advance()
assert parser.current_command() == "\\begin_layout"
continue
elif parser.current_command() == "\\end_layout":
parser.advance()
break
else:
pass
def parse_text_layout(parser, outfile):
"""parses a text layout, from \\begin layout to \\end_layout."""
layout_type = " ".join(parser.current_parameters())
outfile.write(f"<{LAYOUT_TAGS[layout_type]}>")
if layout_type in LAYOUT_NUMBERING.keys():
outfile.counter.increase_counter(layout_type.lower())
outfile.write(outfile.counter.evaluate(LAYOUT_NUMBERING[layout_type]))
parser.advance()
parse_text(parser, outfile)
assert parser.current_command() == "\\end_layout"
outfile.write(f"</{LAYOUT_TAGS[layout_type].split()[0]}>\n")
parser.advance()
def parse_list_layout(parser, outfile, level=0):
"""parses itemize and enumerate"""
layout_type = parser.current_parameters()[0]
write_list_begin_tag(outfile, layout_type, level)
while parser.current_command() == "\\begin_layout" \
and parser.current_parameters()[0] in LIST_LAYOUTS:
# list type changed from enumerate to itemize or vice versa
if parser.current_parameters()[0] != layout_type:
outfile.write(f"</{LIST_TAG[layout_type]}>")
layout_type = parser.current_parameters()[0]
write_list_begin_tag(outfile, layout_type, level)
parse_list_item(parser, outfile, level=level)
outfile.write(f"</{LIST_TAG[layout_type]}>")
def write_list_begin_tag(outfile, list_type, level):
if list_type == ENUMERATE_LAYOUT:
outfile.write(f'<{LIST_TAG[ENUMERATE_LAYOUT]} type="{ENUMERATE_TYPES[level]}">')
else:
outfile.write(f"<{LIST_TAG[ITEMIZE_LAYOUT]}>")
def parse_list_item(parser, outfile, level=0):
assert parser.current_command() == "\\begin_layout" \
and parser.current_parameters()[0] in LIST_LAYOUTS
outfile.write(f"<{ITEM_TAG}>")
parser.advance()
parse_text(parser, outfile)
assert parser.current_command() == "\\end_layout"
parser.advance()
if parser.current_command() == "\\begin_deeper":
parser.advance()
parse_list_layout(parser, outfile, level=level+1)
assert parser.current_command() == "\\end_deeper"
parser.advance()
outfile.write(f"</{ITEM_TAG}>")
def parse_text(parser, outfile, indent=0):
"""parses a text and its styles"""
language = parser.default_language
paragraph_styles = {
"bar": "default",
"uuline": "default",
"uwave": "default",
"series": "default",
"color": "default",
"strikeout": "default",
}
outfile.write(f'<span style="{get_style(paragraph_styles)}">')
outfile.write(" "*indent*4)
style_changed = False
while not parser.is_current_command() or \
parser.current_command() != "\\end_layout":
if not parser.is_current_command():
if style_changed:
outfile.write(
f'</span><span style="{get_style(paragraph_styles)}">')
style_changed = False
# The entire line without the newline
text = parser.current()[:-1]
if is_rtl(language): # parentheses should be reversed in rtl languages
translation_table = str.maketrans("()", ")(")
text = text.translate(translation_table)
outfile.write(text)
parser.advance()
else:
if parser.current_command() == "\\begin_inset":
insets.parse_inset(parser, outfile)
elif parser.current_command()[1:] in styles:
param = parser.current_parameters()[0]
paragraph_styles[parser.current_command()[1:]] = param
parser.advance()
style_changed = True
elif parser.current_command() == "\\lang":
language = parser.current_parameters()[0]
parser.advance()
else:
parser.advance()
outfile.write('</span>')
def get_style(style_dict):
out = ""
if style_dict["bar"] == "under":
out += 'text-decoration: underline; '
if style_dict["uuline"] == "on":
out += 'text-decoration-line: underline; text-decoration-style: double; '
if style_dict["uwave"] == "on":
out += 'text-decoration-line: underline; text-decoration-style: wavy; '
if style_dict["series"] == "medium":
out += 'font-weight: normal; '
elif style_dict["series"] == "bold":
out += 'font-weight: bold; '
if style_dict["strikeout"] == "on":
out += 'text-decoration: line-through; '
if style_dict["color"] in VALID_COLORS:
out += f'color: {style_dict["color"]}; '
return out
def is_rtl(language):
return language in RTL_LANGUAGES