-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
348 lines (313 loc) · 13.2 KB
/
converter.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import re
import database_host
import tkinter as tk
import tkinter.font as tkfont
color1 = 'gray30'
color2 = 'gray15'
color3 = 'white'
if database_host.get_setting('theme') == 'light':
color1 = 'gray85'
color2 = 'gray70'
color3 = 'black'
def update_convtheme():
global color1, color2, color3
color1 = 'gray30'
color2 = 'gray15'
color3 = 'white'
if database_host.get_setting('theme') == 'light':
color1 = 'gray85'
color2 = 'gray70'
color3 = 'black'
def parsed_to_readable(parsed_text, escape_positions, textbox):
split_text = parsed_text.split("(newline)")
ordered_list_num = 0
line_num = 0
skip = False
codeblock = False
for line in split_text:
count = 0
line_escape_pos = escape_positions[line_num]
font_size = None
# Headings
heading_level = None
for i in range(1, 7):
if line.startswith(f"(h{i})"):
heading_level = i
break
match heading_level:
case 1:
font_size = 22
case 2:
font_size = 18
case 3:
font_size = 16
case 4:
font_size = 14
case 5:
font_size = 12
case 6:
font_size = 11
case None:
font_size = 14
# Calculate the number of characters that can fit on a line
pixel_width = textbox.winfo_width()
font_obj = tkfont.Font(font=("Arial", font_size))
space_width = font_obj.measure(' ')
textbox_width = pixel_width / space_width
count_length = int(textbox_width)
if heading_level:
heading = "h"
bold = "bold"
line = line[4:]
for j in range(len(line_escape_pos)):
line_escape_pos[j - 1] -= 4
else:
heading = "r"
font_size = 14
bold = ""
# Code Block
if line.startswith("(cb)"):
line = ""
skip = True
if codeblock == False:
codeblock = True
else:
codeblock = False
# Ordered list
textbox.tag_configure("ordered", font=("Arial", 14))
if line.startswith("(ol)"):
ordered_list_num += 1
list_prefix = str(ordered_list_num) + ". "
line = list_prefix + line[4:] # Prepend the number to the line instead of separate insert
space_width = font_obj.measure(' ')
char_width = font_obj.measure(list_prefix)
indent = char_width / space_width
count_length -= indent
count -= 2
for j in range(len(line_escape_pos)):
line_escape_pos[j - 1] -= 4
else:
ordered_list_num = 0
# Unordered list
textbox.tag_configure("bullet", font=("Arial", 14, "bold"))
if line.startswith("(ul)"):
space_width = font_obj.measure(' ')
char_width = font_obj.measure(" " + u"\u2022" + " ")
indent = char_width / space_width
count_length -= indent
count += 1
textbox.insert(tk.END, " " + u"\u2022" + " ", "bullet")
line = line[4:]
for j in range(len(line_escape_pos)):
line_escape_pos[j - 1] -= 4
# Blockquote
blockquote_tag = "blockquote" + str(font_size)
textbox.tag_configure(blockquote_tag, font=("Arial", font_size), background = "gray20")
if line.startswith("(bq)"):
space_width = font_obj.measure(' ')
char_width = font_obj.measure(" ")
indent = char_width / space_width
count_length -= indent
textbox.insert(tk.END, " ", blockquote_tag)
line = " " + line[4:]
for j in range(len(line_escape_pos)):
line_escape_pos[j - 1] -= 3
# Unchecked box
textbox.tag_configure("unchecked", font=("Arial", 14))
if line.startswith("(unchecked)"):
space_width = font_obj.measure(' ')
char_width = font_obj.measure(u"\u2610" + " ")
indent = char_width / space_width
count_length -= indent
count += 2
textbox.insert(tk.END, u"\u2610" + " ", "unchecked")
line = line[11:]
for j in range(len(line_escape_pos)):
line_escape_pos[j - 1] -= 11
# Checked box
textbox.tag_configure("checked", font=("Arial", 14))
if line.startswith("(checked)"):
space_width = font_obj.measure(' ')
char_width = font_obj.measure(u"\u2611" + " ")
indent = char_width / space_width
count_length -= indent
count += 2
textbox.insert(tk.END, u"\u2611" + " ", "unchecked")
line = line[9:]
for j in range(len(line_escape_pos)):
line_escape_pos[j - 1] -= 9
# Links
# NOTE: Links do not actually link to any website yet
link_addresses = []
link_characters = []
while True:
match = re.search(r"(\(l\))(\(name\))(.+?)(\(address\))(.+?)(\(l\))", line)
if not match:
break
link_addresses.append(match.group(5))
start, end = match.span()
name = match.group(3)
line = line[:start] + name + line[end:]
start, end = match.span(3)
link_characters.extend(i for i in range(start - 8, end - 8))
# Bold
bold_indices = []
bold_characters = []
matches = re.finditer(r"(\(b\))(.+?)(\(b\))", line)
end_subtract = 6
start_subtract = 0
sr = 0
er = 0
for match in matches:
start, end = match.span()
bold_indices.append(start - start_subtract)
bold_indices.append(end - end_subtract)
for i in range(len(link_characters)):
if link_characters[i - 1] > start - start_subtract:
link_characters[i - 1] -= 3
if link_characters[i - 1] > end - end_subtract:
link_characters[i - 1] -= 3
for j in range(len(line_escape_pos)):
if line_escape_pos[j - 1] > end:
line_escape_pos[j - 1] -= 6
elif line_escape_pos[j - 1] > start:
line_escape_pos[j - 1] -= 3
bold_characters.extend([i for i in range(start + 1 - start_subtract, end + 1 - end_subtract)])
start_subtract = start_subtract + 6
end_subtract = end_subtract + 6
line = line[:start - sr] + line[start + 3 - sr:end - 3 - er] + line[end - er:]
er += 6
sr += 6
# Italic
italic_indices = []
italic_characters = []
matches = re.finditer(r"(\(i\))(.+?)(\(i\))", line)
end_subtract = 6
start_subtract = 0
sr = 0
er = 0
for match in matches:
start, end = match.span()
italic_indices.append(start - start_subtract)
italic_indices.append(end - end_subtract)
for i in range(len(bold_characters)):
if bold_characters[i - 1] > start - start_subtract:
bold_characters[i - 1] -= 3
if bold_characters[i - 1] > end - end_subtract:
bold_characters[i - 1] -= 3
for i in range(len(link_characters)):
if link_characters[i - 1] > start - start_subtract:
link_characters[i - 1] -= 3
if link_characters[i - 1] > end - end_subtract:
link_characters[i - 1] -= 3
for j in range(len(line_escape_pos)):
if line_escape_pos[j - 1] > end:
line_escape_pos[j - 1] -= 6
elif line_escape_pos[j - 1] > start:
line_escape_pos[j - 1] -= 3
italic_characters.extend([i for i in range(start + 1 - start_subtract, end + 1 - end_subtract)])
start_subtract = start_subtract + 6
end_subtract = end_subtract + 6
line = line[:start - sr] + line[start + 3 - sr:end - 3 - er] + line[end - er:]
er += 6
sr += 6
# Inline Code Block
inlinecode_indices = []
inlinecode_characters = []
matches = re.finditer(r"(\(ic\))(.+?)(\(ic\))", line)
end_subtract = 8
start_subtract = 0
sr = 0
er = 0
for match in matches:
start, end = match.span()
inlinecode_indices.append(start - start_subtract)
inlinecode_indices.append(end - end_subtract)
for i in range(len(bold_characters)):
if bold_characters[i - 1] > start - start_subtract:
bold_characters[i - 1] -= 4
if bold_characters[i - 1] > end - end_subtract:
bold_characters[i - 1] -= 4
for i in range(len(italic_characters)):
if italic_characters[i - 1] > start - start_subtract:
italic_characters[i - 1] -= 4
if italic_characters[i - 1] > end - end_subtract:
italic_characters[i - 1] -= 4
for i in range(len(link_characters)):
if link_characters[i - 1] > start - start_subtract:
link_characters[i - 1] -= 4
if link_characters[i - 1] > end - end_subtract:
link_characters[i - 1] -= 4
for j in range(len(line_escape_pos)):
if line_escape_pos[j - 1] > end:
line_escape_pos[j - 1] -= 8
elif line_escape_pos[j - 1] > start:
line_escape_pos[j - 1] -= 4
inlinecode_characters.extend([i for i in range(start + 1 - start_subtract, end + 1 - end_subtract)])
start_subtract = start_subtract + 8
end_subtract = end_subtract + 8
line = line[:start - sr] + line[start + 4 - sr:end - 4 - er] + line[end - er:]
er += 8
sr += 8
# Adding characters to textbox
char_num = 0 # Create a variable to store what number character on the line it is up to
for char in line:
space_width = font_obj.measure(' ')
char_width = font_obj.measure(char)
count += char_width / space_width
if count >= count_length:
beginning_tag = f"{font_size}"
textbox.tag_configure(beginning_tag, font=("Arial", font_size))
textbox.insert(tk.END, "\n")
textbox.insert(tk.END, " " * int(indent), beginning_tag)
count = (char_width / space_width) * 2
char_num = char_num + 1
if char_num in bold_characters or heading == "h":
bold = ",bold"
else:
bold = ","
if char_num in italic_characters:
italic = ",italic"
else:
italic = ","
if char_num in inlinecode_characters:
backgrounds = "gray45"
if heading == "r":
bold = ","
else:
bold = ",bold"
italic = ","
else:
backgrounds = color1
if char_num in link_characters:
foregrounds = "#0291E3"
underlines = True
tag = "true"
else:
foregrounds = color3
underlines = False
tag = "false"
if codeblock == True:
backgrounds = "lightgray"
foregrounds = "black"
bold = ","
italic = ","
underlines = False
tag = "false"
# Tag name is a combination of all changes
tag_name = heading + str(font_size) + bold + italic + "," + backgrounds + "," + foregrounds + "," + tag
if bold == ",bold" and italic == ",italic":
textbox.tag_configure(tag_name, font=("Arial", font_size, "bold", "italic"), background = backgrounds, foreground = foregrounds, underline = underlines)
elif bold == ",bold":
textbox.tag_configure(tag_name, font=("Arial", font_size, "bold"), background = backgrounds, foreground = foregrounds, underline = underlines)
elif italic == ",italic":
textbox.tag_configure(tag_name, font=("Arial", font_size, "italic"), background = backgrounds, foreground = foregrounds, underline = underlines)
else:
textbox.tag_configure(tag_name, font=("Arial", font_size), background = backgrounds, foreground = foregrounds, underline = underlines)
if char_num - 1 not in line_escape_pos:
textbox.insert(tk.END, char, tag_name)
if skip == False:
textbox.insert(tk.END, "\n")
else:
skip = False
line_num += 1