-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtei_to_html.py
executable file
·84 lines (67 loc) · 2.56 KB
/
tei_to_html.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
#!/usr/bin/env python3
import argparse
from lxml import etree
import logging
import os
from src import SPELLCHOICE_ORIG, SPELLCHOICE_SPANISH, ABBRCHOICE_ABBR, TEXT_PARAMS
from src import TEXT_PREVIEW_TEMPLATE, OUTLINE_PREVIEW_TEMPLATE
from src import parse_xml_file, generate_html, generate_outline
def make_all_files(path, text, xslt_path, flex_path):
versions_to_generate = [
# (suffix, regularized?)
('orig', False),
('reg', True)
]
tei_root = parse_xml_file(path)
# Generate HTML for each option from the XML
for suffix, regularized in versions_to_generate:
html_root = generate_html(
tei_root,
xslt_path=xslt_path,
flex_path=flex_path,
text=text,
spellchoice=SPELLCHOICE_SPANISH if regularized else SPELLCHOICE_ORIG,
abbrchoice=ABBRCHOICE_ABBR,
)
# Write output
htmlstr = etree.tostring(html_root, method="xml", encoding="unicode")
with open(f'{text}_{suffix}.html', "w", encoding="utf-8") as f:
f.write(htmlstr)
# Write preview
preview_htmlstr = TEXT_PREVIEW_TEMPLATE.format(htmlstr)
with open(f'previews/{suffix}_preview.html', "w", encoding="utf-8") as f:
f.write(preview_htmlstr)
# Generate outline
generate_outline(path, f'{text}_outline.html', text=text, preview=False)
# Generate preview outline
generate_outline(path, 'previews/outline_preview.html', text=text, preview=True)
parser = argparse.ArgumentParser(description="Convert a TEI-encoded text to HTML.")
parser.add_argument("infile", help="path to a TEI file to convert")
parser.add_argument(
"-t",
"--text",
required=True,
choices=list(TEXT_PARAMS.keys()),
help="text ID indicating what formatting to use when converting"
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
default=False,
help="print debugging output"
)
if __name__ == "__main__":
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
logging.info(f'Starting! converting TEI in {args.infile} to Ticha HTML')
if args.text in TEXT_PARAMS:
xslt_path = TEXT_PARAMS[args.text]["xslt_path"]
flex_path = TEXT_PARAMS[args.text]["flex_path"]
else:
raise RuntimeError("unknown text name: " + args.text)
make_all_files(args.infile, args.text, xslt_path, flex_path)
logging.info(f'Finished! Wrote 3 files to {os.getcwd()} and 3 preview files to {os.getcwd()}/preview')