forked from martinpflaum/latex_to_html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
838 lines (669 loc) · 25.6 KB
/
core.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
import antibugs
#\numberwithin{equation}{theorem}
def get_all_allchars_no_abc():
"""
super small subset of - this really makes this programm slow otherwise :D
allchars_no_abc = [chr(k) for k in range(256)]
allchars_no_abc = ''.join(allchars_no_abc)
allchars_no_abc = allchars_no_abc.replace("ABCDEFGHIJKLMNOPQRSTUVWXYZ","")
allchars_no_abc = allchars_no_abc.replace("abcdefghijklmnopqrstuvwxyz","")
return allchars_no_abc
"""
return '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\n ' # is removed
def save_command_split(input,split_on):
if split_on == "$":
return input.split("$")
for appendix in get_all_allchars_no_abc():
#if isinstance(input)
input = input.replace(split_on + appendix,"XXXsplit_meXXX"+appendix)
input = input.split("XXXsplit_meXXX")
return input
def remove_empty_at_begin(input):
out = 0
for k,elem in enumerate(input):
if elem == " " or elem == "\n" or elem =="*":
if elem =="*":
print("unexpected * your numbering is probably wrong :'D")
out = k + 1
else:
break
return input[out:]
def find_nearest_classes(input,all_classes):
"""
returns a list of nearest classes
"""
min_distance = 99999
out = []
for elem in all_classes:
dist = elem.position(input)
if dist == -1:
continue
else:
if dist == min_distance:
out.append(elem)
if dist <= min_distance:
min_distance = dist
out = [elem]
if out != []:
for elem in out:
try:
if hasattr(elem, 'prio_elem'):
#print("priority_element")
return [elem]
except AttributeError:
pass
return out
def first_char_brace(input,begin_brace = "{"):
input = remove_empty_at_begin(input)
if len(input) == 0:
return False
return input[0] == begin_brace
def split_on_first_brace(input,begin_brace = "{",end_brace = "}",error_replacement="brace_error"):
"""
input: string with {Something1} Something2
output: tuple (Something1,Something2)
"""
if error_replacement=="chapter_error":
print(input[:20])
input = remove_empty_at_begin(input)
if len(input) == 0:
#raise RuntimeError("hi")
print("first brace empty string ERROR")
return error_replacement,input
if input[0] != begin_brace:
print("first brace NOT Brace ERROR")
return error_replacement,input
brace_count = 0
out1 = ""
for elem in input:
out1 += elem
if elem == begin_brace:
brace_count = brace_count + 1
if elem == end_brace:
brace_count = brace_count - 1
if brace_count == 0:
break
out2 = input[len(out1):]
out1 = out1[1:-1]
return out1, out2
def split_rename(input):
input = remove_empty_at_begin(input)
if len(input) == 0:
return None
if input[0] == "[":
name,post = split_on_first_brace(input,"[","]")
return name,post
else:
return None
def split_on_next(input,split_on,save_split = True):
#tmp = input.split(split_on)
if save_split:
tmp = save_command_split(input,split_on)
else:
tmp = input.split(split_on)
pre = tmp[0]
post = input[len(pre + split_on):]
return pre, post
def begin_end_split_old(input,beginname,endname,save_split=False):
"""
"""
pre,tmp = split_on_next(input,beginname,save_split)
middle,post = split_on_next(tmp,endname,save_split)
return pre,middle,post
def begin_end_split(input,beginname,endname,save_split=False):
##return begin_end_split_old(input,beginname,endname,save_split)
pre,xanda = split_on_next(input,beginname,save_split)
begin_num = 1
middle = ""
while True:
posbegin = position_of(xanda,beginname,save_split)
posend = position_of(xanda,endname,save_split)
if posbegin!=-1 and posbegin < posend:
#print(f"posbegin {posbegin} < posend {posend}")
ptmp,xtmp = split_on_next(xanda,beginname,save_split)
middle += ptmp + beginname
xanda = xtmp
begin_num = begin_num + 1
else:
ptmp,xtmp = split_on_next(xanda,endname,save_split)
begin_num = begin_num - 1
if begin_num == 0:
middle += ptmp
return pre,middle,xtmp
else:
middle += ptmp + endname
xanda = xtmp
print("one iter")
"""while True:
current = tmp[0]
tmp = tmp[1:]
if endname in current:
begin_num = begin_num - len(current.split(endname))
if begin_num == 0:
xmiddle,post = split_on_next(current,endname,save_split=False)
middle += xmiddle
for elem in tmp:
post += beginname + elem
return pre,middle,post
else:
middle += beginname + current
else:
middle += beginname + current
begin_num = begin_num + 1
print("begin_end_split error")
return begin_end_split_old(input,beginname,endname)
"""
def position_of(input,beginname,save_split = True):
if beginname in input:
if save_split:
tmp = save_command_split(input,beginname)
else:
tmp = input.split(beginname)
# tmp = input.split(beginname)
if len(tmp) == 1:
return -1
return len(tmp[0])
else:
return -1
call_num = 0
class Element():
"""
#NOTE DO NOT EDIT
_children
_modifiable_content
"""
children = None
_modifiable_content = ""
parent = None
def hasattr(self,input):
try:
object.__getattribute__(self,input)
return True
except AttributeError:
return False
def search_attribute_holder(self,input):
try:
object.__getattribute__(self,input)
return self
except AttributeError:
if self.parent is None:
return None
else:
return self.parent.search_attribute_holder(input)
def all_childs(self):
out = [self]
if self.children is None:
return out
else:
for child in self.children:
out.extend(child.all_childs())
return out
def search_on_func(self,function):
if function(self):
return self
else:
if self.parent is None:
return None
else:
return self.parent.search_on_func(function)
def search_class(self,input):
if self.parent is None:
if isinstance(self,input):
return self
else:
return None
else:
if isinstance(self,input):
return self
else:
return self.parent.search_class(input)
def search_up_on_func(self,function):
"""
if you are calling this function while the caller is not a child
of his parent this will throw an error
- during split_and_create the object will never be a child
of his parent until it this function is finished!
"""
if function(self):
return self
root = self.search_class(Document)
all_childs = root.all_childs()
split_on = -1
for k,elem in enumerate(all_childs):
if elem == self:
split_on = k
break
#if split_on == -1:
# raise RuntimeError("FATAL ERROR couldn t find own class in roots all_childs " + str(self.__class__))
all_childs = all_childs[:split_on]
all_childs = all_childs[::-1]
for elem in all_childs:
if function(elem):
return elem
return None
def __getattr__(self, name):
"""if hasattr(self, input):
return self.__getattr__(name)
"""
if self.parent is None:
return None
#raise AttributeError("No parent in the latex tree has the attribute " + name)
else:
return self.parent.__getattr__(name)
def __init__(self,modifiable_content,parent):
"""
#NOTE PLEASE CALL super().__init__(pre_content,modifiable_content)
in your child class
input
"""
self._modifiable_content = modifiable_content
self.parent = parent
def _finish_up(self):
"""
this function will be called at the very end
it puts modifable_content into an RawText wrapper
"""
if self._modifiable_content != "":
if self.children is None:
self.children = []
self.children.append(RawText(self._modifiable_content,self))
self._modifiable_content = ""
if self.children is None:
self.children = [RawText(self._modifiable_content,self)]
#print(type(self))
#raise RuntimeError("something went wrong children is not none but _modifiable_content != \"")
else:
for child in self.children:
child._finish_up()
#def develop(self):
def expand(self,all_classes):
global call_num
while True:
call_num = call_num + 1
if call_num % 100==0:
print(".",end='')
if call_num % 2000==0:
print("\nnumber of expand calls ",call_num," ")
if self._process_children(all_classes) == False:
break
def _process_children(self,all_classes):
"""
#NOTE DO NOT OVERWRITE!!
this function will return True if one child (or grant...grant child) has been updated
"""
if self._modifiable_content != "":
if self.children is None:
self.children = []
while self._modifiable_content != "":
selected_classes = find_nearest_classes(self._modifiable_content,all_classes)
if selected_classes == []:
if self.children == []:
self.children = None
return False
element = Undefined(self._modifiable_content,self)
self.children.append(element)
self._modifiable_content = ""
else:
undefined_string,element,self._modifiable_content = selected_classes[0].split_and_create(self._modifiable_content,self)
self.children.append(Undefined(undefined_string,self))
self.children.append(element)
return True
else:
if self.children is None:
return False
out = False
for child in self.children:
out = out or child._process_children(all_classes)
return out
def to_string(self):
"""
output html-string
"""
raise NotImplementedError("no function to_string found")
class Undefined(Element):
def __init__(self,modifiable_content,parent):
super().__init__(modifiable_content,parent)
def to_string(self):
out = ""
for child in self.children:
out += child.to_string()
return out
class RawText(Element):
def __init__(self,input,parent):
super().__init__("",parent)
self.text = input
def to_string(self):
return self.text
def has_value_equal(instance,attribute_name,value):
if instance.hasattr(attribute_name):
return (object.__getattribute__(instance,attribute_name)==value)
else:
return False
class SectionEnumerate(Element):
"""
SectionEnumerate Searcher need to implement
def theorem_enviroment_name(self):
return "document"
"""
section_count = 0
equation_count = 0
def __init__(self,modifiable_content,parent,theorem_env_name,enum_parent_class):
super().__init__(modifiable_content,parent)
if (not isinstance(enum_parent_class,list)) and (not enum_parent_class is None):
enum_parent_class = [enum_parent_class]
self.enum_parent_class = enum_parent_class
self.theorem_env_name = theorem_env_name
def label_name(self):
return self.get_section_enum()[:-1]
def try_get_section_enum(self,class_name):
if class_name is None:
return str(self.section_number) + "."
else:
search_func = lambda instance : has_value_equal(instance,"theorem_env_name",class_name)
section_enum = self.search_up_on_func(search_func)
if section_enum is None:
return None
else:
out = section_enum.get_section_enum()
out += str(self.section_number) + "."
return out
def get_section_enum(self):
if self.enum_parent_class is None:
return str(self.section_number) + "."
else:
out = None
for elem in self.enum_parent_class:
out = self.try_get_section_enum(elem)
if not out is None:
break
if out is None:
out = "E"
print("couldn't find enumaration parent: --"+self.enum_parent_class +"-- in enviroment: --" + self.theorem_env_name +"--")
return out
def generate_child_equation_number(self):
equation_number = self.equation_count + 1
self.equation_count = self.equation_count + 1
return equation_number
def generate_child_section_number(self):
section_number = self.section_count + 1
self.section_count = self.section_count + 1
return section_number
class BeginEquationElement(Element):
def __init__(self,modifiable_content,parent):
super().__init__(modifiable_content,parent)
def to_string(self):
"""
first children ist name of Section
"""
#\tag{}
out = "<br><br><span class='display'>"
for child in self.children:
out += child.to_string()
out += "</span><br><br>"
return out
class Globals():
pass
class Document(SectionEnumerate):
def __init__(self,modifiable_content,parent):
super().__init__(modifiable_content,parent,"document",None)
self.globals = Globals()
def get_section_enum(self):
return ""
@staticmethod
def position(input):
if "\\begin{document}" in input:
return position_of(input,"\\begin{document}")
else:
return -1
@staticmethod
def split_and_create(input,parent):
pre,content,post = begin_end_split(input,"\\begin{document}","\\end{document}")
return pre,Document(content,parent),post
def to_string(self):
out = ""
for child in self.children:
out += child.to_string()
return out
def create_final_file(file_name,article_header,discription,article,bibliography):
out = None
with open('contentwrapper.html', 'r') as file:
out = file.read()
out = out.split("XXXarticle_headerXXX")
out = out[0] + article_header + out[1]
out = out.split("XXXdiscriptionXXX")
out = out[0] + discription + out[1]
out = out.split("XXXcontentXXX")
out = out[0] + article + out[1]
out = out.split("XXXbibtexXXX")
out = out[0] + bibliography + out[1]
with open(file_name, "w") as file:
file.write(out)
def execute_on_pattern(input,arg_num,command_name,command_pattern):
out = ""
while(True):
pattern_instance = command_pattern
pre,post = split_on_next(input,command_name)
if input == pre:
out += pre
break
for k in range(arg_num):
content = ""
if first_char_brace(post):
content,post = split_on_first_brace(post)
elif first_char_brace(post,"["):
content,post = split_on_first_brace(post,"[","]")
pattern_instance = pattern_instance.replace("#"+str(k+1),content)
input = post
out += pre + pattern_instance
return out
def do_commands(input):
toprocess = input
out = ""
all_commands = []
while True:
#
pre,post = split_on_next(toprocess,"\\newcommand")
out += pre
if toprocess == pre:
break
command_name,post = split_on_first_brace(post)
arg_num = 0
if first_char_brace(post,"["):
arg_num,post = split_on_first_brace(post,"[","]")
arg_num = int(arg_num)
command_pattern,post = split_on_first_brace(post)
toprocess = post
all_commands.append((arg_num,command_name,command_pattern))
while True:
tmp = input
for arg_num,command_name,command_pattern in all_commands:
tmp = execute_on_pattern(tmp,arg_num,command_name,command_pattern)
if tmp == input:
break
input = tmp
return input
def execute_enviroment_on_pattern(input,environment_name,arg_num,begin,end):
out = ""
while(True):
begin_instance = begin
end_instance = end
if not "\\begin{"+environment_name+"}" in input:
out += input
break
pre,content,post = begin_end_split(input,"\\begin{"+environment_name+"}","\\end{"+environment_name+"}")
for k in range(arg_num):
argument = ""
if first_char_brace(content):
argument,content = split_on_first_brace(content)
elif first_char_brace(content,"["):
argument,content = split_on_first_brace(content,"[","]")
end_instance = end_instance.replace("#"+str(k+1),argument)
begin_instance = begin_instance.replace("#"+str(k+1),argument)
input = post
out += pre + begin_instance + content + end_instance
return out
def do_newenvironment(input):
toprocess = input
out = ""
all_env = []
while True:
pre,post = split_on_next(toprocess,"\\newenvironment")
out += pre
if toprocess == pre:
break
environment_name,post = split_on_first_brace(post)
arg_num = 0
if first_char_brace(post,"["):
arg_num,post = split_on_first_brace(post,"[","]")
arg_num = int(arg_num)
post = "{" + split_on_next(post,"{")[1]
begin,post = split_on_first_brace(post)
end,post = split_on_first_brace(post)
toprocess = post
all_env.append((environment_name,arg_num,begin,end))
while True:
tmp = input
for environment_name,arg_num,begin,end in all_env:
print("applying enviroment ",environment_name)
tmp = execute_enviroment_on_pattern(tmp,environment_name,arg_num,begin,end)
if tmp == input:
break
input = tmp
return input
class TheoremElement(SectionEnumerate):
#hsla(206, 90%, 20%, 0.7);
def __init__(self,modifiable_content,section_number,parent,display_name,theorem_env_name,enum_parent_class):
super().__init__(modifiable_content,parent,theorem_env_name,enum_parent_class)
self.section_number = section_number
self.display_name = display_name
def label_name(self):
return self.get_section_enum()[:-1]
def to_string(self):
"""
first children ist name of Section
"""
out = "<br><br><strong>" + self.display_name + " " + self.get_section_enum()[:-1]+"</strong>"
for child in self.children:
out += child.to_string()
out += "<br><br>"
return out
class TheoremSearcher():
def __init__(self,theorem_env_name,enum_parent_class,display_name):
self.display_name = display_name
self.theorem_env_name = theorem_env_name
self.enum_parent_class = enum_parent_class
def position(self,input):
return position_of(input,"\\begin{" + self.theorem_env_name + "}")
def split_and_create(self,input,parent):
pre,content,post = begin_end_split(input,"\\begin{"+self.theorem_env_name+"}","\\end{"+self.theorem_env_name+"}")
search_func = lambda instance : has_value_equal(instance,"theorem_env_name",self.enum_parent_class)
#potential bug - normaly you would have a space or new line
section_enum = parent.children[-1].search_up_on_func(search_func)
section_number = section_enum.generate_child_section_number()
return pre,TheoremElement(content,section_number,parent,self.display_name,self.theorem_env_name,self.enum_parent_class),post
def get_theoremSearchers(input):
need_fix = []
pending_envs = []
shared_parent_fixer = {"section":"document","subsection":"section"}
while True:
pre,post = split_on_next(input,"\\newtheorem")
if input == pre:
break
theorem_env_name,post = split_on_first_brace(post)
display_name = ""
if first_char_brace(post):
enum_parent_class = ""
display_name,post = split_on_first_brace(post)
if first_char_brace(post,"["):
enum_parent_class,post = split_on_first_brace(post,"[","]")
else:
enum_parent_class = None
shared_parent_fixer[theorem_env_name] = enum_parent_class
pending_envs.append((theorem_env_name,enum_parent_class,display_name))
else:
if first_char_brace(post,"["):
shared_parent,post = split_on_first_brace(post,"[","]")
display_name,post = split_on_first_brace(post)
need_fix.append((theorem_env_name,shared_parent,display_name))
else:
print("theoremenv error in ",theorem_env_name)
input = post
for theorem_env_name,shared_parent,display_name in need_fix:
pending_envs.append((theorem_env_name,shared_parent_fixer[shared_parent],display_name))
out = []
for elem in pending_envs:
print(elem)
out.append(TheoremSearcher(*elem))
return out
def get_number_within_equation(input):
input = input.split("\\numberwithin{equation}")
if len(input) == 1:
return "document"
out,_ = split_on_first_brace(input[1])
return out
class JunkSearch():
def __init__(self,junk_name,save_split = True):
self.junk_name = junk_name
self.save_split = save_split
def position(self,input):
return position_of(input,self.junk_name,self.save_split)
def split_and_create(self,input,parent):
pre,post = split_on_next(input,self.junk_name,self.save_split)
return pre,Undefined("",parent),post
def convert_latex(input,all_classes_prio):
if not isinstance(all_classes_prio[0],list):
all_classes_prio = [all_classes_prio]
input = antibugs.no_more_bugs_begin(input)
input = do_commands(input)
input = do_newenvironment(input)
all_classes_prio[0].extend(get_theoremSearchers(input))
number_within_equation = get_number_within_equation(input)
pre_docmuent,document,post_document = Document.split_and_create(input,None)
document.globals.number_within_equation = number_within_equation
for expand_on in all_classes_prio:
document.expand(expand_on)
document.expand([JunkSearch("{",save_split=False),JunkSearch("}",save_split=False)])
document.expand([JunkSearch("\\ ",save_split=False)])
#pre_content are just commands
print("processing finished! now the final file will be created.")
document._finish_up()
out = document.to_string()
out = antibugs.no_more_bugs_end(out)
return out
class ReplaceSearch():
def __init__(self,junk_name,replacement,save_split=True):
self.junk_name = junk_name
self.replacement = replacement
self.save_split = save_split
def position(self,input):
return position_of(input,self.junk_name,self.save_split)
def split_and_create(self,input,parent):
pre,post = split_on_next(input,self.junk_name,self.save_split)
return pre,Undefined(self.replacement,parent),post
class GuardianSearch():
def __init__(self,name,save_split = True):
self.name = name
self.save_split = save_split
def position(self,input):
return position_of(input,self.name,self.save_split)
def split_and_create(self,input,parent):
pre,post = split_on_next(input,self.name,self.save_split)
return pre,RawText(self.name,parent),post
class OneArgumentJunkSearch():
def __init__(self,command_name,begin_brace="{",end_brace="}"):
self.command_name,self.begin,self.end = command_name,begin_brace,end_brace
def position(self,input):
return position_of(input,self.command_name)
def split_and_create(self,input,parent):
pre,post = split_on_next(input,self.command_name)
name,post = split_on_first_brace(post,self.begin,self.end)
return pre,Undefined("",parent),post
class OneArgumentCommandSearch():
def __init__(self,command_name,begin,end):
self.command_name,self.begin,self.end = command_name,begin,end
def position(self,input):
return position_of(input,self.command_name)
def split_and_create(self,input,parent):
pre,post = split_on_next(input,self.command_name)
name,post = split_on_first_brace(post)
return pre,Undefined(self.begin + name + self.end,parent),post