-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathu2o.py
executable file
·3231 lines (2874 loc) · 113 KB
/
u2o.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
r"""
Convert usfm bibles to osis.
Notes:
* better handling of osisID's is probably needed.
* no attempt has been made to process any x- attributes in this script
other than x-morph as found in bibles located on the ebible.org website.
* I can think of scenarios where this script may not work properly. However,
it works fine for all of the usfm bibles that I have access to at this
time.
* jmp, xop, and sd# need better handling.
* There may be better ways to handle lh and lf
* table cell column spanning is not implemented
Alternative Book Ordering:
To have the books output in an order different from the built in canonical
book order you will have to create a simple text file.
FIRST, put the OSIS ID's for the books in the order you want, one per line,
in a plain text file. Example:
Gen
Exod
Lev
...
Rev
SECOND, name the file as follows: order-SomeOrderYouWant.txt
THIRD, place that file in the directory where you will be running
the script. This new book order will be automatically detected and
available.
Examples can be provided. Simple send me an email and ask me for them.
NOTE: I should probably change this so that there's a more central
location for these alternative book orderings.
This script is public domain. You may do whatever you want with it.
"""
#
# uFDD0 - used to mark line breaks during processing
# uFDD1 - used to preserve line breaks during wj processing
#
# uFDD2 - used at start of footnotes to help process \fp markers
# uFDD3 - used at end of footnotes to help process \fp markers
#
# uFDD4 - mark end of cl and sp tags
# uFDD5 - mark end of cp tags
#
# uFDDF - used to mark separation between files when read from storage.
#
# uFDE0 - used to mark the start of introductions
# uFDE1 - used to mark the end of introductions
#
# uFDE2 - used to separate attributes in usfm tags
#
# make pylint happier..
# pylint: disable=too-many-lines
# pylint: disable=too-many-statements
# pylint: disable=too-many-branches
# pylint: disable=too-many-locals
# pylint: disable=too-many-arguments
# pylint: disable=consider-using-f-string
import os.path
import re
import logging
import concurrent.futures
from sys import exit as sysexit
from os import getenv
from glob import glob
from unicodedata import normalize
from datetime import datetime
from tempfile import NamedTemporaryFile
from collections import OrderedDict
from codecs import encode, lookup, decode
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
from typing import Any
et: Any
_: Any
# try to import lxml so that we can validate
# our output against the OSIS schema.
try:
import lxml.etree as et # nosec
HAVELXML = True
except ImportError:
et = None
HAVELXML = False
# -------------------------------------------------------------------------- #
META = {
"USFM": "3.0", # Targeted USFM version
"OSIS": "2.1.1", # Targeted OSIS version
"VERSION": "0.7", # THIS SCRIPT version
"DATE": "2025-01-19", # THIS SCRIPT revision date
}
# -------------------------------------------------------------------------- #
OSISHEADER = """<?xml version="1.0" encoding="utf-8"?>
<osis xmlns="http://www.bibletechnologies.net/2003/OSIS/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bibletechnologies.net/2003/OSIS/namespace
http://www.bibletechnologies.net/osisCore.2.1.1.xsd">
<osisText osisIDWork="{}" osisRefWork="Bible" xml:lang="{}">
<header>
<revisionDesc resp="{}">
<date>{}</date>
<p>Converted from USFM source using u2o.py</p>
</revisionDesc>
<work osisWork="{}">
<title>{}</title>
{}
<type type="OSIS">Bible</type>
<identifier type="OSIS">Bible.{}.{}</identifier>
<refSystem>Bible</refSystem>
</work>{}
</header>\n"""
STRONGSWORK = """
<work osisWork="strong">
<refSystem>Dict.Strongs</refSystem>
</work>"""
OSISFOOTER = """
</osisText>
</osis>\n"""
# -------------------------------------------------------------------------- #
CANONICALORDER = [
# Canonical order used by the usfm2osis.py script...
# minus the extra books that aren't part of usfm at this time.
"FRONT",
"INTRODUCTION",
"Gen",
"Exod",
"Lev",
"Num",
"Deut",
"Josh",
"Judg",
"Ruth",
"1Sam",
"2Sam",
"1Kgs",
"2Kgs",
"1Chr",
"2Chr",
"PrMan",
"Jub",
"1En",
"Ezra",
"Neh",
"Tob",
"Jdt",
"Esth",
"EsthGr",
"1Meq",
"2Meq",
"3Meq",
"Job",
"Ps",
"AddPs",
"5ApocSyrPss",
"Odes",
"Prov",
"Reproof",
"Eccl",
"Song",
"Wis",
"Sir",
"PssSol",
"Isa",
"Jer",
"Lam",
"Bar",
"EpJer",
"2Bar",
"EpBar",
"4Bar",
"Ezek",
"Dan",
"DanGr",
"PrAzar",
"Sus",
"Bel",
"Hos",
"Joel",
"Amos",
"Obad",
"Jonah",
"Mic",
"Nah",
"Hab",
"Zeph",
"Hag",
"Zech",
"Mal",
"1Esd",
"2Esd",
"4Ezra",
"5Ezra",
"6Ezra",
"1Macc",
"2Macc",
"3Macc",
"4Macc",
"Matt",
"Mark",
"Luke",
"John",
"Acts",
"Rom",
"1Cor",
"2Cor",
"Gal",
"Eph",
"Phil",
"Col",
"1Thess",
"2Thess",
"1Tim",
"2Tim",
"Titus",
"Phlm",
"Heb",
"Jas",
"1Pet",
"2Pet",
"1John",
"2John",
"3John",
"Jude",
"Rev",
"EpLao",
"XXA",
"XXB",
"XXC",
"XXD",
"XXE",
"XXF",
"XXG",
"BACK",
"CONCORDANCE",
"GLOSSARY",
"INDEX",
"GAZETTEER",
"X-OTHER",
]
# get list of book orders available from external files in the current
# working directory. Each order file has the following naming pattern:
# order-SOMEORDER.txt
BOOKORDERS = sorted(
[_.replace("order-", "").replace(".txt", "") for _ in glob("order-*.txt")]
)
BOOKORDERS.append("none")
BOOKORDERS.insert(0, "canonical")
# -------------------------------------------------------------------------- #
# convert usfm book names
BOOKNAMES = {
# old testament books
"GEN": "Gen",
"EXO": "Exod",
"LEV": "Lev",
"NUM": "Num",
"DEU": "Deut",
"JOS": "Josh",
"JDG": "Judg",
"RUT": "Ruth",
"1SA": "1Sam",
"2SA": "2Sam",
"1KI": "1Kgs",
"2KI": "2Kgs",
"1CH": "1Chr",
"2CH": "2Chr",
"EZR": "Ezra",
"NEH": "Neh",
"EST": "Esth",
"JOB": "Job",
"PSA": "Ps",
"PRO": "Prov",
"ECC": "Eccl",
"SNG": "Song",
"ISA": "Isa",
"JER": "Jer",
"LAM": "Lam",
"EZK": "Ezek",
"DAN": "Dan",
"HOS": "Hos",
"JOL": "Joel",
"AMO": "Amos",
"OBA": "Obad",
"JON": "Jonah",
"MIC": "Mic",
"NAM": "Nah",
"HAB": "Hab",
"ZEP": "Zeph",
"HAG": "Hag",
"ZEC": "Zech",
"MAL": "Mal",
# new testament books
"MAT": "Matt",
"MRK": "Mark",
"LUK": "Luke",
"JHN": "John",
"ACT": "Acts",
"ROM": "Rom",
"1CO": "1Cor",
"2CO": "2Cor",
"GAL": "Gal",
"EPH": "Eph",
"PHP": "Phil",
"COL": "Col",
"1TH": "1Thess",
"2TH": "2Thess",
"1TI": "1Tim",
"2TI": "2Tim",
"TIT": "Titus",
"PHM": "Phlm",
"HEB": "Heb",
"JAS": "Jas",
"1PE": "1Pet",
"2PE": "2Pet",
"1JN": "1John",
"2JN": "2John",
"3JN": "3John",
"JUD": "Jude",
"REV": "Rev",
# other books
"TOB": "Tob",
"JDT": "Jdt",
"ESG": "EsthGr",
"WIS": "Wis",
"SIR": "Sir",
"BAR": "Bar",
"LJE": "EpJer",
"S3Y": "PrAzar",
"SUS": "Sus",
"BEL": "Bel",
"1MA": "1Macc",
"2MA": "2Macc",
"3MA": "3Macc",
"4MA": "4Macc",
"1ES": "1Esd",
"2ES": "2Esd",
"MAN": "PrMan",
"PS2": "AddPs",
"ODA": "Odes",
"PSS": "PssSol",
"EZA": "4Ezra",
"5EZ": "5Ezra",
"6EZ": "6Ezra",
"DAG": "DanGr",
"PS3": "5ApocSyrPss",
"2BA": "2Bar",
"LBA": "EpBar",
"JUB": "Jub",
"ENO": "1En",
"1MQ": "1Meq",
"2MQ": "2Meq",
"3MQ": "3Meq",
"REP": "Reproof",
"4BA": "4Bar",
"LAO": "EpLao",
# private use
"XXA": "XXA",
"XXB": "XXB",
"XXC": "XXC",
"XXD": "XXD",
"XXE": "XXE",
"XXF": "XXF",
"XXG": "XXG",
# Peripheral books
"FRT": "FRONT",
"INT": "INTRODUCTION",
"BAK": "BACK",
"CNC": "CONCORDANCE",
"GLO": "GLOSSARY",
"TDX": "INDEX",
"NDX": "GAZETTEER",
"OTH": "X-OTHER",
}
# noncanonical book id's
NONCANONICAL = {
"FRONT": "front",
"INTRODUCTION": "introduction",
"XXA": "x-other-a",
"XXB": "x-other-b",
"XXC": "x-other-c",
"XXD": "x-other-d",
"XXE": "x-other-e",
"XXF": "x-other-f",
"XXG": "x-other-g",
"BACK": "back",
"CONCORDANCE": "concordance",
"GLOSSARY": "glossary",
"INDEX": "index",
"GAZETTEER": "gazetteer",
"X-OTHER": "x-other",
}
# list of books with one chapter
ONECHAP = ["Obad", "Phlm", "2John", "3John", "Jude"]
# -------------------------------------------------------------------------- #
# TAG MAPPINGS
# identification tags
IDTAGS = {
r"\usfm": ("<!-- usfm - ", " -->"),
r"\sts": ("", '<milestone type="x-usfm-sts" n="{}" />'),
r"\toc1": ("", '<milestone type="x-usfm-toc1" n="{}" />'),
r"\toc2": ("", '<milestone type="x-usfm-toc2" n="{}" />'),
r"\toc3": ("", '<milestone type="x-usfm-toc3" n="{}" />'),
# ebible.org bibles sometimes use a ztoc4 tag. If it's desired to process
# this tag then simply uncomment the ztoc4 line here.
# (No other ztags are even attempted in this converter.)
# r'\ztoc4': ('', '<milestone type=x-usfm-ztoc4 n="{}" />'),
r"\restore": ("<!-- restore - ", " -->"),
# the osis 2.1.1 user manual says the value of h h1 h2 and h3 tags should
# be in the short attribute of a title.
# ************************************************************************
# NOTE: These types of titles seem to be problematic when trying to import
# bibles for The SWORD Project. So alternative conversions have been
# implemented to work around the issue.
# ************************************************************************
# r'\h': ('', '<title type="runningHead" short="{}" />'),
# r'\h1': ('', '<title type="runningHead" n="1" short="{}" />'),
# r'\h2': ('', '<title type="runningHead" n="2" short="{}" />'),
# r'\h3': ('', '<title type="runningHead" n="3" short="{}" />')
# ************************************************************************
r"\h": ("", '<milestone type="x-usfm-h" n="{}" />'),
r"\h1": ("", '<milestone type="x-usfm-h1" n="{}" />'),
r"\h2": ("", '<milestone type="x-usfm-h2" n="{}" />'),
r"\h3": ("", '<milestone type="x-usfm-h3" n="{}" />'),
}
# the osis 2.1.1 user manual says the value of id, ide, and rem should be
# placed in description tags in the header. That's why they are in a separate
# list instead of the dict above.
IDTAGS2 = [r"\id", r"\ide", r"\rem"]
# title tags
TITLETAGS = {
# ---------------------------------------------------------
# ##### SECTION TAGS get special handling elsewhere ##### #
r"\is": ('<title type="x-introduction">', "</title>"),
r"\is1": ('<title type="x-introduction">', "</title>"),
r"\is2": ('<title type="x-introduction">', "</title>"),
# \is3 and \is4 are not currently handled in this script.
# r'\is3': ('<title type="x-introduction">', '</title>'),
# r'\is4': ('<title type="x-introduction">', '</title>'),
#
r"\ms": ("<title>", "</title>"),
r"\ms1": ("<title>", "</title>"),
r"\ms2": ("<title>", "</title>"),
r"\ms3": ("<title>", "</title>"),
# \ms4 is not currently handled by this script.
# r'\ms4': ('<title>', '</title>'),
#
r"\s": ("<title>", "</title>"),
r"\s1": ("<title>", "</title>"),
r"\s2": ("<title>", "</title>"),
r"\s3": ("<title>", "</title>"),
r"\s4": ("<title>", "</title>"),
# ##### Semantic Whitespace ##### #
r"\sd": ('<milestone type="x-usfm-sd" />', ""),
r"\sd1": ('<milestone type="x-usfm-sd1" />', ""),
r"\sd2": ('<milestone type="x-usfm-sd2" />', ""),
r"\sd3": ('<milestone type="x-usfm-sd3" />', ""),
r"\sd4": ('<milestone type="x-usfm-sd4" />', ""),
# ---------------------------------------------------------
# ##### INTRODUCTIONS ##### #
r"\imt": ('<title type="main" subType="x-introduction">', "</title>"),
r"\imt1": (
'<title level="1" type="main" subType="x-introduction">',
"</title>",
),
r"\imt2": (
'<title level="2" type="main" subType="x-introduction">',
"</title>",
),
r"\imt3": (
'<title level="3" type="main" subType="x-introduction">',
"</title>",
),
r"\imt4": (
'<title level="4" type="main" subType="x-introduction">',
"</title>",
),
r"\imt5": (
'<title level="5" type="main" subType="x-introduction">',
"</title>",
),
r"\imte": ('<title type="main" subType="x-introduction">', "</title>"),
r"\imte1": (
'<title level="1" type="main" subType="x-introduction">',
"</title>",
),
r"\imte2": (
'<title level="2" type="main" subType="x-introduction">',
"</title>",
),
r"\imte3": (
'<title level="3" type="main" subType="x-introduction">',
"</title>",
),
r"\imte4": (
'<title level="4" type="main" subType="x-introduction">',
"</title>",
),
r"\imte5": (
'<title level="5" type="main" subType="x-introduction">',
"</title>",
),
# r'\ib': ('', ''),
# ##### Normal Title Section ##### #
r"\mt": ('<title type="main">', "</title>"),
r"\mt1": ('<title level="1" type="main">', "</title>"),
r"\mt2": ('<title level="2" type="main">', "</title>"),
r"\mt3": ('<title level="3" type="main">', "</title>"),
r"\mt4": ('<title level="4" type="main">', "</title>"),
r"\mt5": ('<title level="5" type="main">', "</title>"),
r"\mte": ('<title type="main">', "</title>"),
r"\mte1": ('<title level="1" type="main">', "</title>"),
r"\mte2": ('<title level="2" type="main">', "</title>"),
r"\mte3": ('<title level="3" type="main">', "</title>"),
r"\mte4": ('<title level="4" type="main">', "</title>"),
r"\mte5": ('<title level="5" type="main">', "</title>"),
#
r"\mr": ('<title type="scope"><reference>', "</reference></title>"),
r"\sr": ('<title type="scope"><reference>', "</reference></title>"),
r"\r": (
'<title type="parallel"><reference type="parallel">',
"</reference></title>",
),
r"\d": ('<title type="psalm" canonical="true">', "</title>"),
r"\sp": ("<speaker>", "</speaker>"),
# ##### chapter cl tags ##### #
#
# This is the best way I know how to convert these tags.
#
# The osis user manual says to convert these to titles and use chapterLabel
# for type in the title tag. That type is not allowed according to the osis
# 2.1.1 schema though. So I use x-chapterLabel for the type instead.
# NOTE: titles created in this manner don't work with The SWORD Project
# sofware and create problems with displaying other titles as well.
# So the conversion to titles is disabled for now and milestone
# markers are inserted instead.
# r'\cl': ('<title type="x-chapterLabel" short="', '" />'),
r"\cl": ('<milestone type="x-chapterLabel" n="', '" />'),
# ##### chapter cd tags ##### #
# the osis user manual says cd titles should be in an introduction div.
r"\cd": (
'<div type="introduction">\ufdd0<title type="x-description">',
"</title>\ufdd0</div>",
),
# ##### special features ##### #
# the osis user manual says this should be in an lg tag of type doxology
# with an l tag of type refrain.
r"\lit": (
'<lg type="doxology">\ufdd0<l type="refrain">',
"</l>\ufdd0</lg>",
),
}
# paragraph and poetry/prose tags
PARTAGS = {
# INTRODUCTIONS
r"\iot": (r'<item type="x-head" subType="x-introduction">', r"</item>"),
r"\io": (r'<item type="x-indent-1" subType="x-introduction">', r"</item>"),
r"\io1": (
r'<item type="x-indent-1" subType="x-introduction">',
r"</item>",
),
r"\io2": (
r'<item type="x-indent-2" subType="x-introduction">',
r"</item>",
),
r"\io3": (
r'<item type="x-indent-3" subType="x-introduction">',
r"</item>",
),
r"\io4": (
r'<item type="x-indent-4" subType="x-introduction">',
r"</item>",
),
r"\ip": (r'<p subType="x-introduction">', r" </p>"),
r"\im": (r'<p type="x-noindent" subType="x-introduction">', r" </p>"),
r"\ipq": (r'<p type="x-quote" subType="x-introduction">', r" </p>"),
r"\imq": (
r'<p type="x-noindent-quote" subType="x-introduction">',
r" </p>",
),
r"\ipi": (r'<p type="x-indented" subType="x-introduction">', r" </p>"),
r"\imi": (
r'<p type="x-noindent-indented" subType="x-introduction">',
r" </p>",
),
r"\ili": (
r'<item type="x-indent-1" subType="x-introduction">',
r" </item>",
),
r"\ili1": (
r'<item type="x-indent-1" subType="x-introduction">',
r" </item>",
),
r"\ili2": (
r'<item type="x-indent-2" subType="x-introduction">',
r" </item>",
),
r"\ipr": (r'<p type="x-right" subType="x-introduction">', r" </p>"),
r"\iq": (r'<l level="1" subType="x-introduction">', r" </l>"),
r"\iq1": (r'<l level="1" subType="x-introduction">', r" </l>"),
r"\iq2": (r'<l level="2" subType="x-introduction">', r" </l>"),
r"\iq3": (r'<l level="3" subType="x-introduction">', r" </l>"),
r"\iex": (r'<div type="bridge" subType="x-introduction">', r"</div>"),
r"\ie": (r"<!-- ie -->", r""),
# ##### PARAGRAPH/POETRY
r"\p": (r"<p>", r" </p>"),
r"\m": (r'<p type="x-noindent">', r" </p>"),
r"\po": (r'<p type="x-usfm-po">', r" </p>"),
r"\pmo": (r'<p type="x-embedded-opening">', r" </p>"),
r"\pm": (r'<p type="x-embedded">', r" </p>"),
r"\pmc": (r'<p type="x-embedded-closing">', r" </p>"),
r"\pmr": (r'<p type="x-right">', r" </p>"),
r"\pi": (r'<p type="x-indented">', r" </p>"),
r"\pi1": (r'<p type="x-indented-1">', r" </p>"),
r"\pi2": (r'<p type="x-indented-2">', r" </p>"),
r"\pi3": (r'<p type="x-indented-3">', r" </p>"),
r"\pi4": (r'<p type="x-indented-4">', r" </p>"),
r"\mi": (r'<p type="x-noindent-indented">', r" </p>"),
r"\cls": (r"<closer>", r"</closer>"),
r"\lh": (r'<p type="x-usfm-lh">', r"</p>"),
r"\lf": (r'<p type="x-usfm-lf">', r"</p>"),
r"\li": (r'<item type="x-indent-1">', r" </item>"),
r"\li1": (r'<item type="x-indent-1">', r" </item>"),
r"\li2": (r'<item type="x-indent-2">', r" </item>"),
r"\li3": (r'<item type="x-indent-3">', r" </item>"),
r"\li4": (r'<item type="x-indent-4">', r" </item>"),
r"\lim": (r'<item type="x-usfm-lim">', r" </item>"),
r"\lim1": (r'<item type="x-usfm-lim1">', r" </item>"),
r"\lim2": (r'<item type="x-usfm-lim2">', r" </item>"),
r"\lim3": (r'<item type="x-usfm-lim3">', r" </item>"),
r"\lim4": (r'<item type="x-usfm-lim4">', r" </item>"),
r"\pc": (r'<p type="x-center">', r" </p>"),
r"\pr": (r'<p type="x-right">', r" </p>"),
r"\ph": (r'<item type="x-indent-1">', r" </item>"),
r"\ph1": (r'<item type="x-indent-1">', r" </item>"),
r"\ph2": (r'<item type="x-indent-2">', r" </item>"),
r"\ph3": (r'<item type="x-indent-3">', r" </item>"),
# POETRY Markers
r"\q": (r'<l level="1">', r" </l>"),
r"\q1": (r'<l level="1">', r" </l>"),
r"\q2": (r'<l level="2">', r" </l>"),
r"\q3": (r'<l level="3">', r" </l>"),
r"\q4": (r'<l level="4">', r" </l>"),
r"\qr": (r'<l type="x-right">', r" </l>"),
r"\qc": (r'<l type="x-center">', r" </l>"),
r"\qa": (r'<title type="acrostic">', r"</title>"),
r"\qd": (r'<l type="x-usfm-qd">', r"</l>"),
r"\qm": (r'<l type="x-embedded" level="1">', r" </l>"),
r"\qm1": (r'<l type="x-embedded" level="1">', r" </l>"),
r"\qm2": (r'<l type="x-embedded" level="2">', r" </l>"),
r"\qm3": (r'<l type="x-embedded" level="3">', r" </l>"),
r"\qm4": (r'<l type="x-embedded" level="4">', r" </l>"),
# sidebar markers... FIXED ELSEWHERE
r"\esb": (r"<SIDEBAR>", ""),
r"\esbe": (r"</SIDEBAR>", ""),
}
# other introduction and poetry tags
OTHERTAGS = OrderedDict()
for _ in [
# selah is handled in a special manner…
(r"\qs ", "<selah>"),
(r"\qs*", "</selah>"),
# these tags get special handling…
(r"\ie", "<!-- ie -->"), # handled with partags… may not need here…
(r"\ib ", "<!-- b -->"), # handled exactly like \b
(r"\b ", "<!-- b -->"),
(r"\nb ", "<!-- nb -->"),
(r"\nb", "<!-- nb -->"),
# translators chunk marker…
(r"\ts", "<!-- ts -->"),
]:
OTHERTAGS[_[0]] = _[1]
# table cell tags
CELLTAGS = {
# header cells
r"\th": ('<cell role="label">', "</cell>"),
r"\th1": ('<cell role="label">', "</cell>"),
r"\th2": ('<cell role="label">', "</cell>"),
r"\th3": ('<cell role="label">', "</cell>"),
r"\th4": ('<cell role="label">', "</cell>"),
r"\th5": ('<cell role="label">', "</cell>"),
r"\thr": ('<cell role="label" type="x-right">', "</cell>"),
r"\thr1": ('<cell role="label" type="x-right">', "</cell>"),
r"\thr2": ('<cell role="label" type="x-right">', "</cell>"),
r"\thr3": ('<cell role="label" type="x-right">', "</cell>"),
r"\thr4": ('<cell role="label" type="x-right">', "</cell>"),
r"\thr5": ('<cell role="label" type="x-right">', "</cell>"),
# normal cells
r"\tc": ("<cell>", "</cell>"),
r"\tc1": ("<cell>", "</cell>"),
r"\tc2": ("<cell>", "</cell>"),
r"\tc3": ("<cell>", "</cell>"),
r"\tc4": ("<cell>", "</cell>"),
r"\tc5": ("<cell>", "</cell>"),
r"\tcr": ('<cell type="x-right">', "</cell>"),
r"\tcr1": ('<cell type="x-right">', "</cell>"),
r"\tcr2": ('<cell type="x-right">', "</cell>"),
r"\tcr3": ('<cell type="x-right">', "</cell>"),
r"\tcr4": ('<cell type="x-right">', "</cell>"),
r"\tcr5": ('<cell type="x-right">', "</cell>"),
}
# special text and character style tags.
# \wj tags are handled with a special function. Don't add it here.
SPECIALTEXT = {
# tags for special text
r"\add": ('<transChange type="added">', "</transChange>"),
r"\addpn": (
'<transChange type="added" subType="x-usfm-addpn">',
"</transChange>",
),
r"\nd": ("<divineName>", "</divineName>"),
r"\pn": ("<name>", "</name>"),
r"\qt": ('<seg type="otPassage">', "</seg>"),
r"\sig": ("<signed>", "</signed>"),
r"\ord": ('<hi type="super">', "</hi>"),
r"\tl": ("<foreign>", "</foreign>"),
r"\bk": ('<name type="x-usfm-bk">', "</name>"),
r"\k": ('<seg type="keyword">', "</seg>"),
r"\dc": ('<transChange type="added" editions="dc">', "</transChange>"),
r"\sls": ('<foreign type="x-secondaryLanguage">', "</foreign>"),
r"\+add": (
'<seg type="x-nested"><transChange type="added">',
"</transChange></seg>",
),
r"\+addpn": (
'<seg type="x-nested"><transChange type="added" subType="x-usfm-addpn">',
"</transChange></seg>",
),
r"\+nd": ('<seg type="x-nested"><divineName>', "</divineName></seg>"),
r"\+pn": ('<seg type="x-nested"><name>', "</name></seg>"),
r"\+qt": ('<seg type="x-nested"><seg type="otPassage">', "</seg></seg>"),
r"\+sig": ('<seg type="x-nested"><signed>', "</signed></seg>"),
r"\+ord": ('<seg type="x-nested"><hi type="super">', "</hi></seg>"),
r"\+tl": ('<seg type="x-nested"><foreign>', "</foreign></seg>"),
r"\+bk": ('<seg type="x-nested"><name type="x-usfm-bk">', "</name></seg>"),
r"\+k": ('<seg type="x-nested"><seg type="keyword">', "</seg></seg>"),
r"\+dc": (
'<seg type="x-nested"><transChange type="added" editions="dc">',
"</transChange></seg>",
),
r"\+sls": (
'<seg type="x-nested"><foreign type="x-secondaryLanguage">',
"</foreign></seg>",
),
# tags for character styles
r"\em": ('<hi type="emphasis">', "</hi>"),
r"\bd": ('<hi type="bold">', "</hi>"),
r"\it": ('<hi type="italic">', "</hi>"),
r"\bdit": ('<hi type="bold"><hi type="italic">', "</hi></hi>"),
r"\no": ('<hi type="normal">', "</hi>"),
r"\sc": ('<hi type="small-caps">', "</hi>"),
r"\sup": ('<hi type="super">', "</hi>"),
r"\+em": ('<seg type="x-nested"><hi type="emphasis">', "</hi></seg>"),
r"\+bd": ('<seg type="x-nested"><hi type="bold">', "</hi></seg>"),
r"\+it": ('<seg type="x-nested"><hi type="italic">', "</hi></seg>"),
r"\+bdit": (
'<seg type="x-nested"><hi type="bold"><hi type="italic">',
"</hi></hi></seg>",
),
r"\+no": ('<seg type="x-nested"><hi type="normal">', "</hi></seg>"),
r"\+sc": ('<seg type="x-nested"><hi type="small-caps">', "</hi></seg>"),
r"\+sup": ('<seg type="x-nested"><hi type="super">', "</hi></seg>"),
# a few stray list tags that work well being handled in this section.
r"\lik": ('<seg type="x-usfm-lik">', "</seg>"),
r"\liv": ('<seg type="x-usfm-liv">', "</seg>"),
r"\liv1": ('<seg type="x-usfm-liv1">', "</seg>"),
r"\liv2": ('<seg type="x-usfm-liv2">', "</seg>"),
r"\liv3": ('<seg type="x-usfm-liv3">', "</seg>"),
r"\liv4": ('<seg type="x-usfm-liv4">', "</seg>"),
r"\litl": ('<seg type="x-usfm-litl">', "</seg>"),
# a few stray introduction and poetry tags that
# work well being handled in this section.
r"\ior": ('<reference subType="x-introduction">', "</reference>"),
r"\iqt": ('<q subType="x-introduction">', "</q>"),
r"\rq": ('<reference type="source">', "</reference>"),
r"\qac": ('<hi type="acrostic">', "</hi>"),
r"\+ior": (
'<seg type="x-nested"><reference subType="x-introduction">',
"</reference></seg>",
),
r"\+iqt": (
'<seg type="x-nested"><q subType="x-introduction">',
"</q></seg>",
),
r"\+rq": (
'<seg type="x-nested"><reference type="source">',
"</reference></seg>",
),
r"\+qac": ('<seg type="x-nested"><hi type="acrostic">', "</hi></seg>"),
# ca and va tags... because simpler handling was needed...
r"\ca": ('<milestone type="x-usfm-ca" n="', '" />'),
r"\va": ('<milestone type="x-usfm-va" n="', '" />'),
# cp and vp tags... because simpler handling was needed...
r"\cp": ('<milestone type="x-usfm-cp" n="', '" />'),
r"\vp": ('<milestone type="x-usfm-vp" n="', '" />'),
}
# special features
# do not add \lit here... that is handled with TITLETAGS.
FEATURETAGS = {
r"\pro": ('<milestone type="x-usfm-pro" n="', '" /> '),
r"\rb": ('<milestone type="x-usfm-rb" n="', '" /> '),
r"\rt": ('<milestone type="x-usfm-rt" n="', '" /> '),
r"\ndx": ("", '<index index="Index" level1="{}" /> '),
r"\png": ("", '<index index="Geography" level1="{}" />'),
r"\w": ("", '<index index="Glossary" level1="{}" />'),
r"\wa": ("", '<index index="Aramaic" level1="{}" />'),
r"\wg": ("", '<index index="Greek" level1="{}" />'),
r"\wh": ("", '<index index="Hebrew" level1="{}" />'),
r"\+pro": ('<milestone type="x-usfm-pro" n="', '" /> '),
r"\+rb": ('<milestone type="x-usfm-rb" n="', '" /> '),
r"\+rt": ('<milestone type="x-usfm-rt" n="', '" /> '),
r"\+ndx": ("", '<index index="Index" level1="{}" /> '),
r"\+png": ("", '<index index="Geography" level1="{}" />'),
r"\+w": ("", '<index index="Glossary" level1="{}" />'),
r"\+wa": ("", '<index index="Aramaic" level1="{}" />'),
r"\+wg": ("", '<index index="Greek" level1="{}" />'),
r"\+wh": ("", '<index index="Hebrew" level1="{}" />'),
# stray xt tags are handled here
r"\xt": ("<reference>", "</reference>"),
r"\+xt": ('<seg type="x-nested"><reference>', "</reference></seg>"),
# This should be converted to an 'a' tag. More work needs
# to be done before that can happen though.
r"\jmp": ('<seg type="x-usfm-jmp">', "</seg>"),
r"\+jmp": ('<seg type="x-usfm-jmp" subType="x-nested">', "</seg>"),
}
# special strongs feature tag...
STRONGSTAG = ("<w {}{}>", "</w>")
# footnote and cross reference tags
NOTETAGS = {
r"\f": ('<note placement="foot">\uFDD2', "\uFDD3</note>"),
r"\fe": ('<note placement="end">\uFDD2', "\uFDD3</note>"),
r"\x": ('<note type="crossReference">', "</note>"),
r"\ef": (
'<note placement="foot" subType="x-extended">\uFDD2',
"\uFDD3</note>",
),
r"\ex": ('<note type="crossReference" subType="x-extended">', "</note>"),
}
# tags internal to footnotes and cross references
# * If any of these ever start with anything other than \f or \x *
# * then the NOTEFIXRE regex will need to be modified. *
NOTETAGS2 = {
r"\fm": ('<hi type="super">', "</hi>"),
r"\fdc": ('<seg editions="dc">', "</seg>"),
r"\fr": ('<reference type="annotateRef">', "</reference>"),
r"\fk": ("<catchWord>", "</catchWord>"),
r"\fq": ("<catchWord>", "</catchWord>"),
r"\fqa": ('<rdg type="alternate">', "</rdg>"),
# I think this should be label... but that doesn't validate.
# r'\fl': ('<label>', '</label>'),
r"\fl": ('<seg type="x-usfm-fl">', "</seg>"),
r"\fv": ('<hi type="super">', "</hi>"),
r"\ft": ("", ""),
r"\xot": ('<seg editions="ot">', "</seg>"),
r"\xnt": ('<seg editions="nt">', "</seg>"),
r"\xdc": ('<seg editions="dc">', "</seg>"),
r"\xk": ("<catchWord>", "</catchWord>"),
r"\xq": ("<catchWord>", "</catchWord>"),
# there is no mapping in the osis manual for the xo usfm tag
r"\xo": (
'<reference type="annotateRef" subType="x-origin">',
"</reference>",
),
r"\xop": ('<seg type="x-usfm-xop">', "</seg>"),
r"\xta": ('<seg type="x-usfm-xta">', "</seg>"),
r"\xt": ("<reference>", "</reference>"),
# nested versions of internal footnote tags
r"\+fm": ('<seg type="x-nested"><hi type="super">', "</hi></seg>"),
r"\+fdc": ('<seg type="x-nested"><seg editions="dc">', "</seg></seg>"),
r"\+fr": (
'<seg type="x-nested"><reference type="annotateRef">',
"</reference></seg>",
),
r"\+fk": ('<seg type="x-nested"><catchWord>', "</catchWord></seg>"),
r"\+fq": ('<seg type="x-nested"><catchWord>', "</catchWord></seg>"),
r"\+fqa": ('<seg type="x-nested"><rdg type="alternate">', "</rdg></seg>"),
r"\+fl": ('<seg type="x-nested"><seg type="x-usfm-fl">', "</seg></seg>"),
r"\+fv": ('<seg type="x-nested"><hi type="super">', "</hi></seg>"),
r"\+ft": ("", ""),
r"\+xot": ('<seg type="x-nested"><seg editions="ot">', "</seg></seg>"),
r"\+xnt": ('<seg type="x-nested"><seg editions="nt">', "</seg></seg>"),
r"\+xdc": ('<seg type="x-nested"><seg editions="dc">', "</seg></seg>"),
r"\+xk": ('<seg type="x-nested"><catchWord>', "</catchWord></seg>"),
r"\+xq": ('<seg type="x-nested"><catchWord>', "</catchWord></seg>"),
r"\+xo": (
'<seg type="x-nested"><reference type="annotateRef" subType="x-origin">',
"</reference></seg>",
),
r"\+xop": ('<seg type="x-nested"><seg type="x-usfm-xop">', "</seg></seg>"),
r"\+xta": ('<seg type="x-nested"><seg type="x-usfm-xta">', "</seg></seg>"),
r"\+xt": ('<seg type="x-nested"><reference>', "</reference></seg>"),
}
# -------------------------------------------------------------------------- #
# defined attributes for tags
DEFINEDATTRIBUTES = {
r"\w": ["lemma", "strong", "srcloc"],
r"\+w": ["lemma", "strong", "srcloc"],
r"\xt": ["link-ref"],
r"\+xt": ["link-ref"],
r"\fig": ["alt", "src", "size", "loc", "copy", "ref"],
r"\jmp": ["link-href", "link-title", "link-name"],
r"\+jmp": ["link-href", "link-title", "link-name"],
r"\qt-s": ["id", "who"],
r"\qt-e": ["id"],
r"\periph": ["id"],
}
# defaultattributes for tags
# x-default will be used as the default for undefined default attributes
DEFAULTATTRIBUTES = {
r"\w": "lemma",
r"\+w": "lemma",
r"\xt": "link-ref",
r"\+xt": "link-ref",
}
# -------------------------------------------------------------------------- #
# REGULAR EXPRESSIONS
# squeeze all regular spaces, carriage returns, and newlines
# into a single space.
SQUEEZE = re.compile(r"[ \t\n\r]+", re.U + re.M + re.DOTALL)
# matches special text and character styles
# Automatically build SPECIALTEXTRE regex string from SPECIALTEXT dict.
SPECIALTEXTRE_S = r"""
# put special text tags into a named group called 'tag'
(?P<tag>
# tags always start with a backslash and may have a + symbol which
# indicates that it's a nested character style.
\\\+?
# match the tags we want to match.
(?:{})
)
# there is always at least one space separating the tag and the content
\s+
# put the tag content into a named group called 'osis'
(?P<osis>.*?)
# tag end marker
(?P=tag)\*
""".format(
"|".join([_.replace("\\", "") for _ in SPECIALTEXT if not _.startswith(r"\+")])
)
SPECIALTEXTRE = re.compile(SPECIALTEXTRE_S, re.U + re.VERBOSE)
del SPECIALTEXTRE_S
# match z tags that have both a start and end marker
ZTAGS_S = r"""
# put z tags that have a start and end marker into named group 'tag'
(?P<tag>
# tags always start with a backslash
\\