-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhyper.py
executable file
·919 lines (898 loc) · 24.1 KB
/
hyper.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
#!/Users/grammarware/opt/anaconda3/bin/python
import latex2mathml.converter
from terminology import *
EXISTENCE = set()
def is_ul_item(line):
return line.startswith('- ') or line.startswith('* ')
def is_ol_item(line):
return len(line) > 3 and line[0].isdigit() and line[1] == '.' and line[2] == ' '
def entitify(s):
return s.replace('&', '&').replace('<', '<').replace('>', '>')
IN_NO_LIST = 0
IN_UL_LIST = 1
IN_OL_LIST = 2
IN_PRE_BLK = 3
def md2html(md_lines):
ret_lines = []
stack = [IN_NO_LIST]
i = 0
while i < len(md_lines):
line = md_lines[i].strip()
if stack[-1] == IN_PRE_BLK:
if line == '```':
ret_lines.append('</pre>')
stack.pop()
else:
ret_lines.append(entitify(md_lines[i]))
i += 1
continue
else:
if line.startswith('```'):
ret_lines.append('<pre>')
stack.append(IN_PRE_BLK)
i += 1
continue
# all the normal list stuff
if stack[-1] == IN_NO_LIST:
if is_ul_item(line):
ret_lines.append('<ul>')
ret_lines.append(li(line[2:].strip()))
stack.append(IN_UL_LIST)
elif is_ol_item(line):
ret_lines.append('<ol>')
ret_lines.append(li(line[2:].strip()))
stack.append(IN_OL_LIST)
else:
ret_lines.append(my_md_converter(line.strip()))
elif stack[-1] == IN_UL_LIST:
if is_ul_item(line):
ret_lines.append(li(line[2:].strip()))
elif is_ol_item(line):
ret_lines.append('<ol>')
ret_lines.append(li(line[2:].strip()))
stack.append(IN_OL_LIST)
else:
ret_lines.append('</ul>')
stack.pop()
continue
elif stack[-1] == IN_OL_LIST:
if is_ol_item(line):
ret_lines.append(li(line[3:].strip()))
elif is_ul_item(line):
ret_lines.append('<ul>')
ret_lines.append(li(line[2:].strip()))
stack.append(IN_UL_LIST)
else:
ret_lines.append('</ol>')
stack.pop()
continue
else:
# cannot happen
pass
i += 1
while len(stack)>1:
if stack[-1] == IN_UL_LIST:
ret_lines.append('</ul>')
stack.pop()
if stack[-1] == IN_OL_LIST:
ret_lines.append('</ol>')
stack.pop()
return '\n'.join(ret_lines)
def my_md_converter(x):
return text2text(clickable(link2link(gentle_latex2mathml(x))))
def get_key(fn):
if fn.endswith('.md'):
fn = fn[:-3]
if fn.find('(') > -1 and fn.find(')') > -1:
before = fn[0:fn.index('(')].strip()
after = fn[fn.index('(')+1 : fn.index(')')].strip()
if len(before) < 2 or len(after) < 2:
fn = before + after
elif len(before) > len(after) > 1:
fn = after
elif len(after) > len(before) > 1:
fn = before
if len(fn) > 20:
fn2 = ''.join(cap for cap in fn if cap.isupper())
if fn2:
fn = fn2
else:
fn2 = ''.join(word[0] for word in fn.replace('-', ' ').split())
if fn2:
fn = fn2
fn = fn.lower()
for c in ' _-':
fn = fn.replace(c, '')
for p in {'τ':'tau', 'µ':'mu', 'é':'e', '+':'plus', '*':'star'}.items():
fn = fn.replace(p[0], p[1])
fn = fn.replace('τ', 'tau').replace('é', 'e')
return fn
def clickable(http):
if http.find('http') > -1:
before = http[:http.index('http')]
if before.endswith('"'):
# we're messing with an already HTML-ified URI!
return http
after = http[http.index('http'):]
link = after.split(' ')[0]
rest = after[len(link):].strip()
if rest.startswith('(') and rest.endswith(')'):
rest = rest[1:-1]
return before + make_link(link, f'<code>{link}</code>', why=rest)
else:
return http
def h3(x):
return f'<h3>{x}</h3>'
def h4(x, c=None):
if c:
return f'<h4 class="{c}">{x}</h4>'
else:
return f'<h4>{x}</h4>'
def li(x):
return f'<li>{my_md_converter(x)}</li>'
def ul(items):
if not items:
return ''
cuis = [i.strip() for i in items if i]
if len(cuis) == 1:
return my_md_converter(cuis[0])
if all([i.startswith('- ') for i in cuis]):
cuis = [i[1:].strip() for i in cuis]
return '<ul>' + '\n'.join([li(i) for i in cuis]) + '</ul>'
def make_link_from_source(where):
if where == DS22_PAPER or where == DS22_ARTEFACT:
src = DS22_CONTAINED
elif where in KNOWN_VENUES:
src = REASON + f'<a class="sic" href="{where}">{KNOWN_VENUES[where]}</a>'
elif where.startswith('https://github.com/Sophietje/Verification-Tool-Overview/pull/'):
no = where.split('/')[-1]
src = REASON + make_link(where, f'pull request №{no}')
else:
# src = make_link(where, f'<code>{where}</code>')
src = REASON + link2link(where)
if src:
return src
else:
return 'OOPS'
def make_link(where, what, hover='', why=''):
in_a = ''
if not where.startswith('http') and where not in EXISTENCE:
in_a += ' class="broken"'
if hover:
in_a += f' title="{hover}"'
s = f'<a href="{where}"{in_a}>{what}</a>'
if why:
s += f' ({why})'
return s
def gentle_latex2mathml(x):
if x.find('$') < 0:
# no need to go through the trouble
return x
if x.startswith('$'):
x = ' ' + x
if x.endswith('$'):
x = x + ' '
xs = x.split('$')
for i in range(0,len(xs)//2):
xs[2*i+1] = latex2mathml.converter.convert(xs[2*i+1])
return ''.join(xs).strip()
LINK_PARSER_NORMAL = 1
LINK_PARSER_PERHAPS_LINK = 2
LINK_PARSER_IMPLICIT = 3
LINK_PARSER_IMPLICIT_PRIME = 4
LINK_PARSER_EXPLICIT = 5
LINK_PARSER_EXPLICIT_PRIME = 6
LINK_PARSER_TARGET = 7
LINK_PARSER_FIG_HOPE = 8
LINK_PARSER_FIG_ALMOST = 9
LINK_PARSER_FIG = 10
LINK_PARSER_FIG_PRIME = 11
def link2link(x):
r = ''
i = 0
state = LINK_PARSER_NORMAL
while i < len(x):
if state == LINK_PARSER_NORMAL:
if x[i] == '[':
state = LINK_PARSER_PERHAPS_LINK
elif x[i] == '!':
state = LINK_PARSER_FIG_HOPE
else:
r += x[i]
elif state == LINK_PARSER_PERHAPS_LINK:
if x[i] == '[':
where = ''
state = LINK_PARSER_IMPLICIT
else:
where = x[i]
state = LINK_PARSER_EXPLICIT
elif state == LINK_PARSER_IMPLICIT:
if x[i] == ']':
state = LINK_PARSER_IMPLICIT_PRIME
elif x[i] == '\\':
# escaping!
where += x[i+1]
i += 1
else:
where += x[i]
elif state == LINK_PARSER_IMPLICIT_PRIME:
if x[i] == ']':
r += make_link(get_key(where)+'.html', where)
state = LINK_PARSER_NORMAL
else:
# ERROR: unmatched [[...], roll back to normal brackets
r += f'[[{where}]{x[i]}'
state = LINK_PARSER_NORMAL
elif state == LINK_PARSER_EXPLICIT:
if x[i] == ']':
state = LINK_PARSER_EXPLICIT_PRIME
elif x[i] == '\\':
# escaping!
where += x[i+1]
i += 1
else:
where += x[i]
elif state == LINK_PARSER_EXPLICIT_PRIME:
if x[i] == '(':
target = ''
state = LINK_PARSER_TARGET
else:
# ERROR: no target follows [...], roll back to normal brackets
r += f'[{where}]{x[i]}'
state = LINK_PARSER_NORMAL
elif state == LINK_PARSER_TARGET:
if x[i] == ')':
if target.startswith('http'):
# external link
link_goal = target
else:
# internal link
link_goal = get_key(target.split('/')[-1].replace('%20', ' ')) + '.html'
r += make_link(link_goal, where)
state = LINK_PARSER_NORMAL
else:
target += x[i]
elif state == LINK_PARSER_FIG_HOPE:
if x[i] == '[':
state = LINK_PARSER_FIG_ALMOST
else:
r += '!' + x[i]
state = LINK_PARSER_NORMAL
elif state == LINK_PARSER_FIG_ALMOST:
if x[i] == '[':
where = ''
state = LINK_PARSER_FIG
else:
r += '![' + x[i]
state = LINK_PARSER_NORMAL
elif state == LINK_PARSER_FIG:
if x[i] == ']':
state = LINK_PARSER_FIG_PRIME
else:
where += x[i]
elif state == LINK_PARSER_FIG_PRIME:
if x[i] == ']':
r += f'<br/><img src="{where}" alt="{where}"/><br/>'
state = LINK_PARSER_NORMAL
else:
r += f'![[{where}]' + x[i]
state = LINK_PARSER_NORMAL
else:
print(f'ERROR IN LINK: {x} --- lost parser state')
state = LINK_PARSER_NORMAL
i += 1
# check if we're in the final state
if state == LINK_PARSER_PERHAPS_LINK:
r += '['
elif state == LINK_PARSER_IMPLICIT:
r += f'[[{where}'
elif state == LINK_PARSER_IMPLICIT_PRIME:
r += f'[[{where}]'
elif state == LINK_PARSER_EXPLICIT:
r += f'[{where}'
elif state == LINK_PARSER_EXPLICIT_PRIME:
r += f'[{where}]'
elif state == LINK_PARSER_TARGET:
r += f'[{where}]({target}'
elif state == LINK_PARSER_FIG_HOPE:
r += f'!'
elif state == LINK_PARSER_FIG_ALMOST:
r += f'!['
elif state == LINK_PARSER_FIG:
r += f'![[{where}'
elif state == LINK_PARSER_FIG_PRIME:
r += f'![[{where}]'
return r
TEXT_PARSER_NORMAL = 100
TEXT_PARSER_TICK = 101
TEXT_PARSER_TICK_T = 102
TEXT_PARSER_U = 103 # _
TEXT_PARSER_US = 104 # _*
TEXT_PARSER_USS = 105 # _**
TEXT_PARSER_USST = 106 # _**a
TEXT_PARSER_USSTS = 107 # _**a*
TEXT_PARSER_UU = 108 # __
TEXT_PARSER_UUS = 109 # __*
TEXT_PARSER_UUSU = 110 # __*_
TEXT_PARSER_UUST = 111 # __*a
TEXT_PARSER_UUSTU = 112 # __*a_
TEXT_PARSER_UUT = 113 # __a
TEXT_PARSER_UUTS = 114 # __a*
TEXT_PARSER_UUTSU = 115 # __a*_
TEXT_PARSER_UUTST = 116 # __a*b
TEXT_PARSER_UUTSTU = 117 # __a*b_
TEXT_PARSER_UUTU = 118 # __a_
TEXT_PARSER_UT = 119 # _a
TEXT_PARSER_UTS = 120 # _a*
TEXT_PARSER_UTSS = 121 # _a**
TEXT_PARSER_UTSST = 122 # _a**b
TEXT_PARSER_UTSSTS = 123 # _a**b*
TEXT_PARSER_S = 124 # *
TEXT_PARSER_SU = 125 # *_
TEXT_PARSER_SUU = 126 # *__
TEXT_PARSER_SUUT = 127 # *__a
TEXT_PARSER_SUUTU = 128 # *__a_
TEXT_PARSER_SS = 129 # **
TEXT_PARSER_SSU = 130 # **_
TEXT_PARSER_SSUS = 131 # **_*
TEXT_PARSER_SSUT = 132 # **_a
TEXT_PARSER_SSUTS = 133 # **_a*
TEXT_PARSER_SST = 134 # **a
TEXT_PARSER_SSTU = 135 # **a_
TEXT_PARSER_SSTUS = 136 # **a_*
TEXT_PARSER_SSTUT = 137 # **a_b
TEXT_PARSER_SSTUTS = 138 # **a_b*
TEXT_PARSER_SSTS = 139 # **a*
TEXT_PARSER_ST = 140 # *a
TEXT_PARSER_STU = 141 # *a_
TEXT_PARSER_STUU = 142 # *a__
TEXT_PARSER_STUUT = 143 # *a__b
TEXT_PARSER_STUUTU = 144 # *a__b_
def text2text(x):
r = ''
i = 0
state = TEXT_PARSER_NORMAL
content = ''
while i < len(x):
# print(f'[+] TEXT_PARSER_{state}: "{x[:i]}•{x[i:]}" -> "{r}"')
if x[i:].startswith('<a href="'):
j = x.find('</a>', i)
if j > i:
r += x[i:j+4]
i = j+4
continue
if state == TEXT_PARSER_NORMAL:
if x[i] == '\\':
# escaping!
r += x[i+1]
i += 2
continue
if x[i] == '`':
state = TEXT_PARSER_TICK
elif x[i] == '_':
state = TEXT_PARSER_U
elif x[i] == '*':
state = TEXT_PARSER_S
else:
r += x[i]
elif state == TEXT_PARSER_TICK:
if x[i] == '`':
r += '``'
state = TEXT_PARSER_NORMAL
elif x[i] == '\\':
# escaping!
if x[i+1] == '<':
content = '<'
elif x[i+1] == '>':
content = '>'
else:
content = x[i+1]
state = TEXT_PARSER_TICK_T
i += 1
else:
content = x[i]
state = TEXT_PARSER_TICK_T
elif state == TEXT_PARSER_TICK_T:
if x[i] == '`':
r += f'<code>{content}</code>'
state = TEXT_PARSER_NORMAL
elif x[i] == '\\':
# escaping inside ticks
if x[i+1] == '<':
content += '<'
elif x[i+1] == '>':
content += '>'
else:
content += x[i+1]
i += 1
else:
# inside ticks everything else is verbatim
content += x[i]
elif state == TEXT_PARSER_U: # _
if x[i] == '_':
state = TEXT_PARSER_UU
elif x[i] == '*':
state = TEXT_PARSER_US
else:
content = x[i]
state = TEXT_PARSER_UT
elif state == TEXT_PARSER_US: # _*
if x[i] == '_':
r += '<em>*</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
state = TEXT_PARSER_USS
else:
content = '*'+x[i]
state = TEXT_PARSER_UT
elif state == TEXT_PARSER_USS: # _**
if x[i] == '_':
r += '<em>**</em>'
elif x[i] == '*':
content = '*'
state = TEXT_PARSER_UTSS
else:
content = x[i]
state = TEXT_PARSER_USST
elif state == TEXT_PARSER_USST: # _**a
if x[i] == '_':
r += f'<em>**{content}</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
state = TEXT_PARSER_USSTS
else:
content += x[i]
elif state == TEXT_PARSER_USSTS: # _**a*
if x[i] == '_':
r += f'<em>**{content}*</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
content = f'<strong>{content}</strong>'
state = TEXT_PARSER_UT
else:
content += '*'+x[i]
state = TEXT_PARSER_USST
elif state == TEXT_PARSER_UU: # __
if x[i] == '_':
r += '_'
elif x[i] == '*':
state = TEXT_PARSER_UUS
else:
content = x[i]
state = TEXT_PARSER_UUT
elif state == TEXT_PARSER_UUS: # __*
if x[i] == '_':
state = TEXT_PARSER_UUSU
elif x[i] == '*':
content = '**'
state = TEXT_PARSER_UUT
else:
content = x[i]
state = TEXT_PARSER_UUST
elif state == TEXT_PARSER_UUSU: # __*_
if x[i] == '_':
r += '<strong>*</strong>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
content = '<em>_</em>'
state = TEXT_PARSER_UUT
else:
content = '_'+x[i]
state = TEXT_PARSER_UUST
elif state == TEXT_PARSER_UUST: # __*a
if x[i] == '_':
state = TEXT_PARSER_UUSTU
elif x[i] == '*':
content = f'<em>{content}</em>'
state = TEXT_PARSER_UUT
else:
content += x[i]
elif state == TEXT_PARSER_UUSTU: # __*a_
if x[i] == '_':
r += f'<strong>*{content}</strong>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
content = f'<em>{content}_</em>'
state = TEXT_PARSER_UUT
else:
content += '_'+x[i]
state = TEXT_PARSER_UUST
elif state == TEXT_PARSER_UUT: # __a
if x[i] == '_':
state = TEXT_PARSER_UUTU
elif x[i] == '*':
state = TEXT_PARSER_UUTS
else:
content += x[i]
elif state == TEXT_PARSER_UUTS: # __a*
if x[i] == '_':
state = TEXT_PARSER_UUTSU
elif x[i] == '*':
content += '**'
state = TEXT_PARSER_UUT
else:
content2 = x[i]
state = TEXT_PARSER_UUTST
elif state == TEXT_PARSER_UUTSU: # __a*_
if x[i] == '_':
r += f'<strong>{content}*</strong>'
elif x[i] == '*':
content += '<em>_</em>'
state = TEXT_PARSER_UUT
else:
content2 = '_'+x[i]
state = TEXT_PARSER_UUTST
elif state == TEXT_PARSER_UUTST: # __a*b
if x[i] == '_':
state = TEXT_PARSER_UUTSTU
elif x[i] == '*':
content += f'<em>{content2}</em>'
state = TEXT_PARSER_UUT
else:
content2 += x[i]
elif state == TEXT_PARSER_UUTSTU: # __a*b_
if x[i] == '_':
r += f'<strong>{content}*{content2}</strong>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
content += f'<em>{content2}_</em>'
else:
content2 ++ '_'+x[i]
state = TEXT_PARSER_UUTST
elif state == TEXT_PARSER_UUTU: # __a_
if x[i] == '_':
r += f'<strong>{content}</strong>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
content += '_'
state = TEXT_PARSER_UUTS
else:
content += '_'+x[i]
state = TEXT_PARSER_UUT
elif state == TEXT_PARSER_UT: # _a
if x[i] == '_':
r += f'<em>{content}</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
state = TEXT_PARSER_UTS
else:
content += x[i]
elif state == TEXT_PARSER_UTS: # _a*
if x[i] == '_':
r += f'<em>{content}*</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
state = TEXT_PARSER_UTSS
else:
content += '*'+x[i]
state = TEXT_PARSER_UT
elif state == TEXT_PARSER_UTSS: # _a**
if x[i] == '_':
r += f'<em>{content}**</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
content += '*'
else:
content2 = x[i]
state = TEXT_PARSER_UTSST
elif state == TEXT_PARSER_UTSST: # _a**b
if x[i] == '_':
r += f'<em>{content}**{content2}</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
state = TEXT_PARSER_UTSSTS
else:
content2 += x[i]
elif state == TEXT_PARSER_UTSSTS: # _a**b*
if x[i] == '_':
r += f'<em>{content}**{content2}*</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '*':
content += f'<strong>{content2}</strong>'
state = TEXT_PARSER_UT
else:
content2 += '*'+x[i]
state = TEXT_PARSER_UTSST
elif state == TEXT_PARSER_S: # *
if x[i] == '*':
state = TEXT_PARSER_SS
elif x[i] == '_':
state = TEXT_PARSER_SU
else:
content = x[i]
state = TEXT_PARSER_ST
elif state == TEXT_PARSER_SU: # *_
if x[i] == '*':
r += '<em>_</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
state = TEXT_PARSER_SUU
else:
content = '_'+x[i]
state = TEXT_PARSER_ST
elif state == TEXT_PARSER_SUU: # *__
if x[i] == '*':
r += '<em>__</em>'
elif x[i] == '_':
content = '_'
state = TEXT_PARSER_STUU
else:
content = x[i]
state = TEXT_PARSER_SUUT
elif state == TEXT_PARSER_SUUT: # *__a
if x[i] == '*':
r += f'<em>__{content}</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
state = TEXT_PARSER_SUUTU
else:
content += x[i]
elif state == TEXT_PARSER_SUUTU: # *__a_
if x[i] == '*':
r += f'<em>__{content}_</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
content = f'<strong>{content}</strong>'
state = TEXT_PARSER_ST
else:
content += '_'+x[i]
state = TEXT_PARSER_SUUT
elif state == TEXT_PARSER_SS: # **
if x[i] == '*':
r += '*'
elif x[i] == '_':
state = TEXT_PARSER_SSU
else:
content = x[i]
state = TEXT_PARSER_SST
elif state == TEXT_PARSER_SSU: # **_
if x[i] == '*':
state = TEXT_PARSER_SSUS
elif x[i] == '_':
content = '__'
state = TEXT_PARSER_SST
else:
content = x[i]
state = TEXT_PARSER_SSUT
elif state == TEXT_PARSER_SSUS: # **_*
if x[i] == '*':
r += '<strong>_</strong>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
content = '<em>*</em>'
state = TEXT_PARSER_SST
else:
content = '*'+x[i]
state = TEXT_PARSER_SSUT
elif state == TEXT_PARSER_SSUT: # **_a
if x[i] == '*':
state = TEXT_PARSER_SSUTS
elif x[i] == '_':
content = f'<em>{content}</em>'
state = TEXT_PARSER_SST
else:
content += x[i]
elif state == TEXT_PARSER_SSUTS: # **_a*
if x[i] == '*':
r += f'<strong>_{content}</strong>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
content = f'<em>{content}*</em>'
state = TEXT_PARSER_SST
else:
content += '*'+x[i]
state = TEXT_PARSER_SSUT
elif state == TEXT_PARSER_SST: # **a
if x[i] == '*':
state = TEXT_PARSER_SSTS
elif x[i] == '_':
state = TEXT_PARSER_SSTU
else:
content += x[i]
elif state == TEXT_PARSER_SSTU: # **a_
if x[i] == '*':
state = TEXT_PARSER_SSTUS
elif x[i] == '_':
content += '__'
state = TEXT_PARSER_SST
else:
content2 = x[i]
state = TEXT_PARSER_SSTUT
elif state == TEXT_PARSER_SSTUS: # **a_*
if x[i] == '*':
r += f'<strong>{content}_</strong>'
elif x[i] == '_':
content += '<em>*</em>'
state = TEXT_PARSER_SST
else:
content2 = '*'+x[i]
state = TEXT_PARSER_SSTUT
elif state == TEXT_PARSER_SSTUT: # **a_b
if x[i] == '*':
state = TEXT_PARSER_SSTUTS
elif x[i] == '_':
content += f'<em>{content2}</em>'
state = TEXT_PARSER_SST
else:
content2 += x[i]
elif state == TEXT_PARSER_SSTUTS: # **a_b*
if x[i] == '*':
r += f'<strong>{content}_{content2}</strong>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
content += f'<em>{content2}*</em>'
else:
content2 ++ '*'+x[i]
state = TEXT_PARSER_SSTUT
elif state == TEXT_PARSER_SSTS: # **a*
if x[i] == '*':
r += f'<strong>{content}</strong>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
content += '*'
state = TEXT_PARSER_SSTU
else:
content += '*'+x[i]
state = TEXT_PARSER_SST
elif state == TEXT_PARSER_ST: # *a
if x[i] == '*':
r += f'<em>{content}</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
state = TEXT_PARSER_STU
else:
content += x[i]
elif state == TEXT_PARSER_STU: # *a_
if x[i] == '*':
r += f'<em>{content}_</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
state = TEXT_PARSER_STUU
else:
content += '_'+x[i]
state = TEXT_PARSER_ST
elif state == TEXT_PARSER_STUU: # *a__
if x[i] == '*':
r += f'<em>{content}__</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
content += '_'
else:
content2 = x[i]
state = TEXT_PARSER_STUUT
elif state == TEXT_PARSER_STUUT: # *a__b
if x[i] == '*':
r += f'<em>{content}__{content2}</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
state = TEXT_PARSER_STUUTU
else:
content2 += x[i]
elif state == TEXT_PARSER_STUUTU: # *a__b_
if x[i] == '*':
r += f'<em>{content}__{content2}_</em>'
state = TEXT_PARSER_NORMAL
elif x[i] == '_':
content += f'<strong>{content2}</strong>'
state = TEXT_PARSER_ST
else:
content2 += '_'+x[i]
state = TEXT_PARSER_STUUT
else:
print('[!] ERROR: Wrong text2text parser state!')
# should be empty
r += x[i]
i += 1
# print(f'[*] TEXT_PARSER_{state}: "{x[:i]}•{x[i:]}"')
# aftermath
if state == TEXT_PARSER_NORMAL:
pass
elif state == TEXT_PARSER_TICK:
r += '`'
elif state == TEXT_PARSER_TICK_T:
r += f'`{content}'
elif state == TEXT_PARSER_U: # _
r += '_'
elif state == TEXT_PARSER_US: # _*
r += '_*'
elif state == TEXT_PARSER_USS: # _**
r += '_**'
elif state == TEXT_PARSER_USST: # _**a
r += f'_**{content}'
elif state == TEXT_PARSER_USSTS: # _**a*
# slightly nontrivial
r += f'_*<em>{content}</em>'
elif state == TEXT_PARSER_UU: # __
r += '__'
elif state == TEXT_PARSER_UUS: # __*
r += '__*'
elif state == TEXT_PARSER_UUSU: # __*_
# slightly nontrivial
r += '_<em>*</em>'
elif state == TEXT_PARSER_UUST: # __*a
r += f'__*{content}'
elif state == TEXT_PARSER_UUSTU: # __*a_
# slightly nontrivial
r += f'_<em>*{content}</em>'
elif state == TEXT_PARSER_UUT: # __a
r += f'__{content}'
elif state == TEXT_PARSER_UUTS: # __a*
r += f'__{content}*'
elif state == TEXT_PARSER_UUTSU: # __a*_
# slightly nontrivial
r += f'_<em>{content}*</em>'
elif state == TEXT_PARSER_UUTST: # __a*b
r += f'__{content}*{content2}'
elif state == TEXT_PARSER_UUTSTU: # __a*b_
# slightly nontrivial
r += f'_<em>{content}*{content2}</em>'
elif state == TEXT_PARSER_UUTU: # __a_
# slightly nontrivial
r += f'_<em>{content}</em>'
elif state == TEXT_PARSER_UT: # _a
r += f'_{content}'
elif state == TEXT_PARSER_UTS: # _a*
r += f'_{content}*'
elif state == TEXT_PARSER_UTSS: # _a**
r += f'_{content}**'
elif state == TEXT_PARSER_UTSST: # _a**b
r += f'_{content}**{content2}'
elif state == TEXT_PARSER_UTSSTS: # _a**b*
# slightly nontrivial
r += f'_{content}*<em>{content2}</em>'
elif state == TEXT_PARSER_S: # *
r += '*'
elif state == TEXT_PARSER_SU: # *_
r += '*_'
elif state == TEXT_PARSER_SUU: # *__
r += '*__'
elif state == TEXT_PARSER_SUUT: # *__a
r += f'*__{content}'
elif state == TEXT_PARSER_SUUTU: # *__a_
# slightly nontrivial
r += f'*_<em>{content}</em>'
elif state == TEXT_PARSER_SS: # **
r += '**'
elif state == TEXT_PARSER_SSU: # **_
r += '**_'
elif state == TEXT_PARSER_SSUS: # **_*
# slightly nontrivial
r += '*<em>_<em>'
elif state == TEXT_PARSER_SSUT: # **_a
r += f'**_{content}'
elif state == TEXT_PARSER_SSUTS: # **_a*
# slightly nontrivial
r += f'*<em>_{content}</em>'
elif state == TEXT_PARSER_SST: # **a
r += f'**{content}'
elif state == TEXT_PARSER_SSTU: # **a_
r += f'**{content}_'
elif state == TEXT_PARSER_SSTUS: # **a_*
# slightly nontrivial
r += f'*<em>{content}_</em>'
elif state == TEXT_PARSER_SSTUT: # **a_b
r += f'**{content}_{content2}'
elif state == TEXT_PARSER_SSTUTS: # **a_b*
# slightly nontrivial
r += f'*<em>{content}_{content2}</em>'
elif state == TEXT_PARSER_SSTS: # **a*
# slightly nontrivial
r += f'*<em>{content}</em>'
elif state == TEXT_PARSER_ST: # *a
r += f'*{content}'
elif state == TEXT_PARSER_STU: # *a_
r += f'*{content}_'
elif state == TEXT_PARSER_STUU: # *a__
r += f'*{content}__'
elif state == TEXT_PARSER_STUUT: # *a__b
r += f'*{content}__{content2}'
elif state == TEXT_PARSER_STUUTU: # *a__b_
# slightly nontrivial
r += f'*{content}_<em>{content2}</em>'
return r.\
replace('</em><em>','').\
replace('<em></em>','').\
replace('</code><code>','').\
replace('<code></code>','').\
replace('</strong><strong>','').\
replace('<strong></strong>','')