This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomp_pp.py
executable file
·1111 lines (848 loc) · 38.3 KB
/
comp_pp.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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# comp_pp - compare 2 files
# Copyright (C) 2012-2013 bibimbop at pgdp
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import re
import sys
import argparse
import tempfile
import subprocess
import os
from lxml import etree
import tinycss
import cssselect
from functools import partial
from helpers import sourcefile
def clear_element(element):
"""In an XHTML tree, remove all sub-elements of a given element.
We can't properly remove an XML element while traversing the
tree. But we can clean it. Remove its text and children. However
the tail must be preserved because it belong to the next element,
so re-attach."""
tail = element.tail
element.clear()
element.tail = tail
class pgdp_file(object):
"""Stores and process a DP/text/html file.
"""
def __init__(self):
self.text = None
self.words = None
self.myfile = sourcefile.SourceFile()
# œ ligature - has_oe_ligature and has_oe_dp are mutually
# exclusive
self.has_oe_ligature = False # the real thing
self.has_oe_dp = False # DP type: [oe]
# Conversion to latin1 ?
self.convert_to_latin1 = False
self.transform_func = []
# Footnotes, if extracted
self.footnotes = []
# First line of the text. This is where <body> is for html.
self.start_line = 0
def load(self, filename):
pass
def process_args(self, args):
"""Process command line arguments"""
pass
def analyze(self):
pass
def extract_footnotes(self):
"""Extract the footnotes."""
pass
def transform(self):
"""Final transformation pass."""
pass
class pgdp_file_text(pgdp_file):
def load(self, filename):
"""Load the file"""
self.myfile.load_text(filename)
self.from_pgdp_rounds = self.myfile.basename.startswith('projectID')
def analyze(self):
"""Clean then analyse the content of a file. Decides if it is PP version,
a DP version, ..."""
# Remember which line the text started
self.start_line = self.myfile.start
# Unsplit lines
self.text = '\n'.join(self.myfile.text)
# Keep a copy to search for characters
self.char_text = self.text
# Check for œ, or [oe]
if self.text.find('œ') != -1 or self.text.find('Œ') != -1:
self.has_oe_ligature = True
elif self.text.find('[oe]') != -1 or self.text.find('[OE]') != -1:
self.has_oe_dp = True
def convert(self):
"""Remove markers from the text."""
if self.from_pgdp_rounds:
# Px or Fx From PGDP
self.text = re.sub(r"-----File: \w+.png.*", '', self.text)
self.text = self.text.replace("\n/*\n", '\n\n')
self.text = self.text.replace("\n*/\n", '\n\n')
self.text = self.text.replace("\n/#\n", '\n\n')
self.text = self.text.replace("\n#/\n", '\n\n')
self.text = self.text.replace("\n/P\n", '\n\n')
self.text = self.text.replace("\nP/\n", '\n\n')
if args.ignore_format:
tmp = ''
else:
tmp = '_'
self.text = self.text.replace("<i>", tmp)
self.text = self.text.replace("</i>", tmp)
self.text = re.sub("<.*?>", '', self.text)
self.text = re.sub("</.*?>", '', self.text)
self.text = re.sub("\[Blank Page\]", '', self.text)
if args.suppress_footnote_tags:
self.text = re.sub(r"\[Footnote (\d+): ", r'\1 ', self.text)
self.text = re.sub(r"\*\[Footnote: ", '', self.text)
if args.suppress_illustration_tags:
self.text = re.sub(r"\[Illustrations?:([^]]*?)\]", r'\1', self.text, flags=re.MULTILINE)
self.text = re.sub(r"\[Illustration\]", '', self.text)
if args.suppress_proofers_notes:
self.text = re.sub(r"\[\*\*[^]]*?\]", '', self.text)
if args.regroup_split_words:
self.text = re.sub(r"(\w+)-\*(\n+)\*", r'\2\1', self.text)
else:
# Horizontal separation
self.text = self.text.replace("* * * * *", "")
self.text = self.text.replace("* * * * *", "")
pass
# Temp - ndash between numbers
#self.text = re.sub("\d\-\-\d", '-', self.text)
#self.text = self.text.replace("_", ' ')
# Replace -- with real mdash
self.text = self.text.replace("--", "—")
# temp
#self.text = self.text.replace("–", "-")
transtab = str.maketrans("ª₁²₂₃₄₅₆₇",
"a12234567")
self.text = self.text.translate(transtab)
def extract_footnotes_pgdp(self):
# Extract the footnotes from an F round
# Start with [Footnote ... and finish with ] at the end of a line
in_fnote = False # currently processing a footnote
cur_fnote = [] # keeping current footnote
text = [] # new text without footnotes
blanc_cur = False # current line is empty
indent_fnote = 0 # indentation of current footnote
for lineno, line in enumerate(self.text.splitlines()):
# New footnote
if "[Footnote" in line:
if in_fnote:
print("Error in text -- manual cleanup is needed around line " + str(lineno), file=sys.stderr)
sys.exit()
if "*[Footnote" in line:
# Join to previous - Remove the last from the existing
# footnotes.
line = line.replace("*[Footnote: ", "")
cur_fnote, self.footnotes = self.footnotes[-1], self.footnotes[:-1]
else:
cur_fnote = [-1, ""]
in_fnote = True
if in_fnote:
cur_fnote[1] = "\n".join([cur_fnote[1], line])
# Footnote continuation: ] or ]*
# We don't try to regroup yet
if line.endswith((']', "]*")):
self.footnotes.append(cur_fnote)
in_fnote = False
else:
text.append(line)
# Rebuild text, now without footnotes
self.text = '\n'.join(text)
def extract_footnotes_pp(self):
# Extract the footnotes from a PP text version
# Convert to lines and back
in_fnote = False # currently processing a footnote
cur_fnote = [] # keeping current footnote
text = [] # new text without footnotes
blanc_cur = False # current line is empty
indent_fnote = 0 # indentation of current footnote
for line in self.text.splitlines():
blanc_prev = blanc_cur
blanc_cur = len(line) == 0
if line.startswith("——-File:") and in_fnote:
self.footnotes.append(cur_fnote)
text.append(line)
in_fnote = False
continue
if line.endswith((".]", "]]", "»]", " ]", ")]", "_]", "-]", "—]")) and in_fnote:
cur_fnote[1] += "\n" + line
self.footnotes.append(cur_fnote)
in_fnote = False
continue
# Check for new footnote
m = re.match("(\s*)\[([\w-]+)\](.*)", line)
# m = re.match("(\s*)\[Footnote (\d+): (.*)", line)
if not m:
m = re.match("(\s*)\[Note (\d+): (.*)", line)
#m = re.match("(\s*)\[(\d+): (.*)", line)
if m and (m.group(2) == 'Illustration' or m.group(2) == "Décoration"):
# An illustration, possibly inside a footnote. Treat
# as part of text or footnote.
m = None
if m and blanc_prev:
# It matches, and the previous line was empty
if in_fnote:
# New footnote - Append current one to footnotes
self.footnotes.append(cur_fnote)
in_fnote = True
cur_fnote = [ m.group(2), m.group(3) ]
indent_fnote = len(m.group(1))
if line.endswith((".]", "]]", "»]", " ]", ")]", "_]", "-]", "—]")) and in_fnote:
self.footnotes.append(cur_fnote)
in_fnote = False
continue
continue
if in_fnote:
if ((blanc_prev and blanc_cur) or
(not blanc_cur and not line.startswith(' '*indent_fnote))):
# Two empty lines
# or
# Not a blank line, but indentation is less than
# when the footnote was open
#
# -> end of footnote
in_fnote = False
# Append to footnotes
self.footnotes.append(cur_fnote)
else:
# still in footnote
cur_fnote[1] += "\n" + line
continue
# Not a footnote - regular text
text.append(line)
# Last footnote
if in_fnote:
self.footnotes.append(cur_fnote)
# Rebuild text, now without footnotes
self.text = '\n'.join(text)
def extract_footnotes(self):
if self.from_pgdp_rounds:
self.extract_footnotes_pgdp()
else:
self.extract_footnotes_pp()
def transform(self):
"""Final cleanup."""
for func in self.transform_func:
self.text = func(self.text)
# Apply transform function to the footnotes
for fn in self.footnotes:
for func in self.transform_func:
fn[1] = func(fn[1])
class pgdp_file_html(pgdp_file):
def __init__(self):
super().__init__()
self.mycss = ""
def load(self, filename):
"""Load the file"""
self.myfile.load_xhtml(filename)
def process_args(self, args):
# Load default CSS for transformations
if args.css_no_default == False:
self.mycss = '''
i:before, cite:before, em:before, abbr:before, dfn:before,
i:after, cite:after, em:after, abbr:after, dfn:after { content: "_"; }
/* Small caps */
.smcap { text-transform:uppercase; }"
/* line breaks with <br /> will be ignored by
* normalize-space(). Add a space in all of them to work
* around. */
br:before { content: " "; }
/* Remove nbsp within numbers. eg 20 000 becomes 20000.
body * { _replace_regex: "(\\d)\\u00A0(\\d)" "\\1\\2" }*/
/* Add spaces around td tags. */
td:after, td:after { content: " "; }
/* Remove page numbers. It seems every PP has a different way. */
span[class^="pagenum"] { display: none }
p[class^="pagenum"] { display: none }
p[class^="page"] { display: none }
span[class^="pgnum"] { display: none }
div[id^="Page_"] { display: none }
div[class^="pagenum"] { display: none }
'''
"""Process command line arguments"""
if args.css_smcap == 'U':
self.mycss += ".smcap { text-transform:uppercase; }"
elif args.css_smcap == 'L':
self.mycss += ".smcap { text-transform:lowercase; }"
elif args.css_smcap == 'T':
self.mycss += ".smcap { text-transform:capitalize; }"
if args.css_bold:
self.mycss += "b:before, b:after { content: " + args.css_bold + "; }"
if args.css_greek_title_plus:
# greek: if there is a title, use it to replace the greek. */
self.mycss += 'body *[lang=grc] { _replace_with_attr: "title"; }'
self.mycss += 'body *[lang=grc]:before, body *[lang=grc]:after { content: "+"; }'
if args.css_add_illustration:
for figclass in [ 'figcenter', 'figleft', 'figright' ]:
self.mycss += '.' + figclass + ':before { content: "[Illustration: "; }'
self.mycss += '.' + figclass + ':after { content: "]"; }'
# --css can be present multiple times, so it's a list.
for css in args.css:
self.mycss += css
def analyze(self):
"""Clean then analyse the content of a file."""
# Empty the head - we only want the body
self.myfile.tree.find('head').clear()
# Remember which line <body> was.
self.start_line = self.myfile.tree.find('body').sourceline - 2
# Remove PG footer, 1st method
clear_after = False
for element in self.myfile.tree.find('body').iter():
if clear_after:
element.text = ""
element.tail = ""
elif element.tag == "p" and element.text and element.text.startswith("***END OF THE PROJECT GUTENBERG EBOOK"):
element.text = ""
element.tail = ""
clear_after = True
# Remove PG header and footer, 2nd method
find = etree.XPath("//pre")
for element in find(self.myfile.tree):
if element.text is None:
continue
text = element.text.strip()
# Header - Remove everything until start of book.
m = re.match(".*?\*\*\* START OF THIS PROJECT GUTENBERG EBOOK.*?\*\*\*(.*)", text, flags=re.MULTILINE | re.DOTALL)
if m:
# Found the header. Keep only the text after the
# start tag (usually the credits)
element.text = m.group(1)
continue
if text.startswith("End of the Project Gutenberg") or text.startswith("End of Project Gutenberg"):
clear_element(element)
# Remove PG footer, 3rd method -- header and footer are normal
# html, not text in <pre> tag.
try:
# Look for one element
(element,) = etree.XPath("//p[@id='pg-end-line']")(self.myfile.tree)
while element is not None:
clear_element(element)
element = element.getnext()
except ValueError:
pass
# Cleaning is done.
# Transform html into text for character search.
self.char_text = etree.XPath("normalize-space(/)")(self.myfile.tree)
# HTML doc should have oelig by default.
self.has_oe_ligature = True
def text_apply(self, element, func):
"""Apply a function to every sub element's .text and .tail,
and element's .text."""
if element.text:
element.text = func(element.text)
for el in element.iter():
if el == element:
continue
if el.text:
el.text = func(el.text)
if el.tail:
el.tail = func(el.tail)
def convert(self):
"""Remove HTML and PGDP marker from the text."""
# Process each rule from our transformation CSS
stylesheet = tinycss.make_parser().parse_stylesheet(self.mycss)
for rule in stylesheet.rules:
# Extract values we care about
v_content = None
f_transform = None
f_replace_with_attr = None
f_replace_regex = None
f_text_replace = None
f_element_func = None
for val in rule.declarations:
if val.name == 'content':
v_content = val.value[0].value
elif val.name == "text-transform":
v = val.value[0].value
if v == "uppercase":
f_transform = lambda x: x.upper()
elif v == "lowercase":
f_transform = lambda x: x.lower()
elif v == "capitalize":
f_transform = lambda x: x.title()
elif val.name == "_replace_with_attr":
f_replace_with_attr = lambda el: el.attrib[val.value[0].value]
elif val.name == "text-replace":
v1 = val.value[0].value
v2 = val.value[2].value
f_text_replace = lambda x: x.replace(v1, v2)
elif val.name == "display":
# Support display none only. So ignore "none" argument.
f_element_func = clear_element
# elif val.name == "_replace_regex":
# f_replace_regex = partial(re.sub, r"(\d)\u00A0(\d)", r"\1\2")
# f_replace_regex = partial(re.sub, val.value[0].value, val.value[1].value)
# Iterate through each selectors in the rule
for selector in cssselect.parse(rule.selector.as_css()):
pseudo_element = selector.pseudo_element
xpath = cssselect.HTMLTranslator().selector_to_xpath(selector)
find = etree.XPath(xpath)
# Find each matching element in the HTML/XHTML document
for element in find(self.myfile.tree):
# Replace text with content of an attribute.
if f_replace_with_attr:
element.text = f_replace_with_attr(element)
if pseudo_element == "before":
element.text = v_content + (element.text or '') # opening tag
elif pseudo_element == "after":
element.tail = v_content + (element.tail or '') # closing tag
if f_transform:
self.text_apply(element, f_transform)
if f_text_replace:
self.text_apply(element, f_text_replace)
if f_element_func:
f_element_func(element)
# if f_replace_regex and element.text:
# element.text = f_replace_regex(element.text)
return
# Transform footnote anchors to [..]
find = etree.XPath("//a")
for element in find(self.myfile.tree):
href = element.attrib.get('href', None)
if not href or not href.startswith("#Footnote_"):
continue
if element.text and not element.text.startswith('['):
# Some PP have [xx], other have just xx for a page
# number. Do not add [ ] if they are already there.
element.text = '[' + (element.text or '') # opening tag
element.tail = ']' + (element.tail or '') # closing tag
# Add illustration tag, wherever we find it
for figclass in [ 'figcenter', 'figleft', 'figright', 'caption' ]:
find = etree.XPath("//div[contains(concat(' ', normalize-space(@class), ' '), ' " + figclass + " ')]")
for element in find(self.myfile.tree):
if element.text and len(element.text) > 1:
element.text = '[Illustration:' + element.text # opening tag
else:
element.text = '[Illustration' + (element.text or '') # opening tag
element.tail = ']' + (element.tail or '') # closing tag
# for figclass in [ 'caption' ]:
# find = etree.XPath("//p[contains(concat(' ', normalize-space(@class), ' '), ' " + figclass + " ')]")
# for element in find(self.myfile.tree):
# element.text = '[Illustration:' + (element.text or '') # opening tag
# element.tail = ']' + (element.tail or '') # closing tag
# Add sidenote tag
if args.with_sidenote_tags:
for sntag in [ 'sidenote' ]:
for find in [ "//p[contains(concat(' ', normalize-space(@class), ' '), ' " + sntag + " ')]",
"//div[starts-with(@class, 'sidenote')]" ]:
for element in etree.XPath(find)(self.myfile.tree):
element.text = '[Sidenote:' + (element.text or '') # opening tag
element.tail = ']' + (element.tail or '') # closing tag
def extract_footnotes(self):
# Find footnotes, then remove them
if args.extract_footnotes:
for find in [ "//p[a[@id[starts-with(.,'Footnote_')]]]",
"//div[@id[starts-with(.,'FN_')]]",
"//div[p/a[@id[starts-with(.,'Footnote_')]]]",
"//div[p/a[@id[starts-with(.,'Footnote_')]]]",
"//div/p[span/a[@id[starts-with(.,'Footnote_')]]]",
#"//p[a[@id[not(starts-with(.,'footnotetag')) and starts-with(.,'footnote')]]]",
#"//p[a[@id[starts-with(.,'footnote')]]]",
]:
for element in etree.XPath(find)(self.myfile.tree):
# Grab the text and remove the footnote number
m = re.match("\s*\[([\w-]+)\](.*)", element.xpath("string()"), re.DOTALL)
if not m:
m = re.match("\s*([\d]+)\s+(.*)", element.xpath("string()"), re.DOTALL)
if not m:
m = re.match("\s*([\d]+):(.*)", element.xpath("string()"), re.DOTALL)
if not m:
m = re.match("\s*Note ([\d]+):\s+(.*)", element.xpath("string()"), re.DOTALL)
if not m:
# That's probably bad
print ("BAD? " , element.sourceline)
print("*" + element.xpath("string()") + "*")
#continue
raise
#print("*" + element.xpath("string()") + "*")
self.footnotes.append([ m.group(1), m.group(2) ])
clear_element(element)
if len(self.footnotes):
# Found them. Stop now.
break
#for fn in self.footnotes:
# print(fn[0])
#raise
def transform(self):
"""Transform html into text. Do a final cleanup."""
self.text = etree.XPath("string(/)")(self.myfile.tree)
# ff=open("compfilehtml.txt", "w")
# ff.write(self.text)
# ff.close()
# Apply transform function to the main text
for func in self.transform_func:
self.text = func(self.text)
# Apply transform function to the footnotes
for fn in self.footnotes:
for func in self.transform_func:
fn[1] = func(fn[1])
# zero width space
if args.ignore_0_space:
self.text = self.text.replace(chr(0x200b), "")
def oelig_convert(convert_oelig, text):
# Do the required oelig conversion
if convert_oelig == 1:
text = text.replace(r"[oe]", "oe")
text = text.replace(r"[OE]", "OE")
elif convert_oelig == 2:
text = text.replace(r"[oe]", "œ")
text = text.replace(r"[OE]", "Œ")
elif convert_oelig == 3:
text = text.replace("œ", "oe")
text = text.replace("Œ", "OE")
return text
def latin1_convert(text):
# Convert some UTF8 characters to latin1
text = text.replace("’", "'")
text = text.replace("‘", "'")
text = text.replace('“', '"')
text = text.replace('”', '"')
# text = text.replace('in-4º', 'in-4o')
# text = text.replace('in-8º', 'in-8o')
# text = text.replace('in-fº', 'in-fo')
text = text.replace('º', 'o')
return text
def convert_to_words(text):
"""Split the text into a list of words from the text."""
# Split into list of words
words = []
for line in re.findall(r'([\w-]+|\W)', text):
line = line.strip()
if line != '':
words.append(line)
return words
def compare_texts(text1, text2):
# Compare two sources
# We could have used the difflib module, but it's too slow:
# for line in difflib.unified_diff(f1.words, f2.words):
# print(line)
# Use diff instead.
# Some debug code
#f = open("/tmp/text1", "w")
#f.write(text1)
#f.close()
#f = open("/tmp/text2", "w")
#f.write(text2)
#f.close()
with tempfile.NamedTemporaryFile(mode='w') as t1, tempfile.NamedTemporaryFile(mode='w') as t2:
t1.write(text1)
t2.write(text2)
t1.flush()
t2.flush()
cmd = ["dwdiff",
"-P",
"-R",
"-C 2",
"-L",
"-w ]COMPPP_START_DEL[",
"-x ]COMPPP_STOP_DEL[",
"-y ]COMPPP_START_INS[",
"-z ]COMPPP_STOP_INS["]
if args.ignore_case:
cmd += [ "--ignore-case" ]
cmd += [ t1.name, t2.name ]
p = subprocess.Popen( cmd,
stdout=subprocess.PIPE)
# The output is raw, so we have to decode it to UTF-8, which
# is the default under Ubuntu.
return p.stdout.read().decode('utf-8')
def create_html(files, text, footnotes, footnotes_errors):
def massage_input(text, start0, start1):
# Massage the input
text = text.replace("&", "&")
text = text.replace("<", "<")
text = text.replace(">", ">")
text = text.replace("]COMPPP_START_DEL[", "<span class='start_del'>")
text = text.replace("]COMPPP_STOP_DEL[", "</span>")
text = text.replace("]COMPPP_START_INS[", "<span class='start_ins'>")
text = text.replace("]COMPPP_STOP_INS[", "</span>")
if text:
text = "<hr /><pre>\n" + text
text = text.replace("\n--\n", "\n</pre><hr /><pre>\n")
text = re.sub(r"^\s*(\d+):(\d+)",
lambda m: "<span class='lineno'>{0} : {1}</span>".format(int(m.group(1)) + start0,
int(m.group(2)) + start1),
text, flags=re.MULTILINE)
return text
# Find the number of diff sections
nb_diffs_text = 0
if text:
nb_diffs_text = len(re.findall("\n--\n", text)) + 1
nb_diffs_footnotes = 0
if footnotes:
nb_diffs_footnotes = len(re.findall("\n--\n", footnotes or "")) + 1
# Text, with correct (?) line numbers
text = massage_input(text, files[0].start_line, files[1].start_line)
# Footnotes - line numbers are meaningless right now. We could fix
# that.
footnotes = massage_input(footnotes, 0, 0)
# Print header and CSS
print("""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>
Compare """ + files[0].myfile.basename + " and " + files[1].myfile.basename + """
</title>
<style type="text/css">
body {
margin-left: 5%;
margin-right: 5%;
}
.start_del {
border: 1px solid black;
color: #700000 ;
background-color: #f4f4f4;
font-size: larger;
}
.start_ins {
border: 1px solid black;
color: green;
background-color: #f4f4f4;
font-size: larger;
}
.lineno { margin-right: 3em; }
.sep4 { margin-top: 4em; }
.bbox { margin-left: auto;
margin-right: auto;
border: 1px dashed;
padding: 0em 1em 0em 1em;
background-color: #F0FFFF;
width: 90%;
max-width: 50em;
}
.center { text-align:center; }
/* Use a CSS counter to number each diff. */
body {
counter-reset: diff; /* set diff counter to 0 */
}
hr:before {
counter-increment: diff; /* inc the diff counter ... */
content: "Diff " counter(diff) ": "; /* ... and display it */
}
</style>
</head>
<body>
""")
print("<h1>" + files[0].myfile.basename + " and " + files[1].myfile.basename + "</h1>")
print("""
<div class="bbox">
<p class="center">— Usage —</p>
<p>
The first number is the line number in the first file (""" + files[0].myfile.basename + """)<br />
The second number is the line number in the second file (""" + files[1].myfile.basename + """)<br />
</p>
<p>
Deleted words that were in the first file but not in the second will appear <span class='start_del'>like this</span>.<br />
Inserted words that were in the second file but not in the first will appear <span class='start_ins'>like this</span>.<br />
</p>
</div>
<p>Custom CSS added on command line: """ + " ".join(args.css) + """</p>
""")
print("<p>There is " + str(nb_diffs_text) + " diff sections in the main text</p>")
if footnotes:
print("<p>Footnotes are diff'ed separately <a href='#footnotes'>here</a></p>")
print("<p>There is " + str(nb_diffs_footnotes) + " diff sections in the footnotes</p>")
if footnotes_errors:
print("<p>Error with footnotes numbering:</p>")
print("<ul>")
for err in footnotes_errors:
print("<li>" + err + "</li>")
print("</ul>")
print("<h2 class='sep4'>Main text</h2>")
print("<pre class='sep4'>")
print(text)
print("</pre>")
if footnotes:
print("<h2 id='footnotes' class='sep4'>Footnotes</h2>")
print("<pre class='sep4'>")
print(footnotes)
print("</pre>")
print("""
</body>
</html>
""")
def check_char(files, char_best, char_other):
"""Check whether each file has a the best character. If not, add a
conversion request.
This is used for instance if one version uses ’ while the other
uses '. In that case, we need to convert one into the other, to
get a smaller diff.
"""
in_0 = files[0].char_text.find(char_best)
in_1 = files[1].char_text.find(char_best)
if in_0 >= 0 and in_1 >= 0:
# Both have it
return
if in_0 == -1 and in_1 == -1:
# None have it
return
# Downgrade one version
if in_0 > 0:
files[0].transform_func.append(lambda text: text.replace(char_best, char_other))
else:
files[1].transform_func.append(lambda text: text.replace(char_best, char_other))
def check_oelig(files):
"""Similar to check_char, but for oe ligatures."""
if files[0].has_oe_ligature and files[1].has_oe_ligature:
pass
elif files[0].has_oe_dp and files[1].has_oe_dp:
pass
elif files[0].has_oe_ligature and files[1].has_oe_dp:
files[1].transform_func.append(lambda text: text.replace("[oe]", "œ"))
files[1].transform_func.append(lambda text: text.replace("[OE]", "Œ"))
elif files[1].has_oe_ligature and files[0].has_oe_dp:
files[0].transform_func.append(lambda text: text.replace("[oe]", "œ"))
files[0].transform_func.append(lambda text: text.replace("[OE]", "Œ"))
else:
if files[0].has_oe_ligature:
files[0].transform_func.append(lambda text: text.replace("œ", "oe"))
files[0].transform_func.append(lambda text: text.replace("Œ", "OE"))
elif files[1].has_oe_ligature:
files[1].transform_func.append(lambda text: text.replace("œ", "oe"))
files[1].transform_func.append(lambda text: text.replace("Œ", "OE"))
if files[0].has_oe_dp:
files[0].transform_func.append(lambda text: text.replace("[oe]", "oe"))
files[0].transform_func.append(lambda text: text.replace("[OE]", "OE"))
elif files[1].has_oe_dp:
files[1].transform_func.append(lambda text: text.replace("[oe]", "oe"))
files[1].transform_func.append(lambda text: text.replace("[OE]", "OE"))
def main():
files = [ None, None ]
# Load files
for i, fname in enumerate(args.filename):
# Look for file type.
if fname.endswith(('html', 'htm')):
files[i] = pgdp_file_html()
else:
files[i] = pgdp_file_text()
f = files[i]
f.load(fname)
if f.myfile is None:
print("Couldn't load file:", fname)
return
f.process_args(args)
f.analyze()
# How to process oe ligature
check_oelig(files)
# How to process punctuation
check_char(files, "’", "'") # curly quote to straight
check_char(files, "‘", "'") # curly quote to straight
check_char(files, "º", "o") # ordinal to letter o
check_char(files, "–", "-") # ndash to regular dash
check_char(files, "½", "-1/2")
check_char(files, "¼", "-1/4")
check_char(files, "¾", "-3/4")
check_char(files, '”', '"')
check_char(files, '“', '"')
check_char(files, '⁄', '/') # fraction
check_char(files, "′", "'") # prime
check_char(files, "″", "''") # double prime
check_char(files, "‴", "'''") # triple prime