-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_latex.py
More file actions
208 lines (192 loc) · 7.79 KB
/
main_latex.py
File metadata and controls
208 lines (192 loc) · 7.79 KB
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
import argparse
import os
import shutil
from bs4 import BeautifulSoup
import cssutils
import make4ht_utils
import shared
import tex
ap = argparse.ArgumentParser(description="Convert LaTeX to HTML")
ap.add_argument("source_file_path", help="Path to the source .zip file")
ap.add_argument("output_dir", help="Path to output folder (will be created if needed)")
ap.add_argument(
"--mathml",
default=False,
action="store_true",
help="Use MathML conversion in make4ht",
)
ap.add_argument("--timeout-ms", type=int, help="Enforce make4ht execution time (in ms)")
ap.add_argument(
"--skip-compile",
action="store_true",
help="Skip (re)compilation; useful only for quickly debugging postprocessing steps",
)
ap.add_argument(
"--skip-extract",
action="store_true",
help="Skip extraction; useful for modifying Tex sources during debugging",
)
ap.add_argument(
"--main-tex", default="main.tex", help="Name of main .tex file (default main.tex)"
)
ap.add_argument("--texlive", type=int, help="Use specific TeXLive year (version)")
args = ap.parse_args()
print("Creating output folder")
extracted_dir = os.path.join(args.output_dir, "source")
shared.warn.output_filename = os.path.join(args.output_dir, "conversion_warnings.csv")
try:
os.mkdir(args.output_dir)
except FileExistsError:
print("Output folder already exists; contents may be overwritten")
# Clean up old files
if not args.skip_compile and not args.skip_extract:
try:
shutil.rmtree(extracted_dir)
except FileNotFoundError:
pass
# Combine any \input files into 1 (makes postprocessing much easier for line numbers)
print("Reading LaTeX source")
if args.skip_extract:
with open(os.path.join(extracted_dir, "tmp-make4ht.tex")) as infile:
texstr = infile.read()
else:
texstr = make4ht_utils.get_raw_tex_contents(
args.source_file_path, extracted_dir, args.main_tex
)
with open(os.path.join(extracted_dir, "tmp-make4ht.tex"), "w") as ofile:
ofile.write(texstr)
bib_backend = make4ht_utils.get_bib_backend(texstr)
extra_flags = make4ht_utils.detect_extra_flags_needed(texstr)
if extra_flags:
print("Using extra make4ht flags:" + extra_flags)
template_name = "unknown"
if os.path.exists(os.path.join(extracted_dir, "edm_article.cls")):
template_name = "EDM"
make4ht_utils.check_file_hash(
os.path.join(extracted_dir, "edm_article.cls"),
"7efa88c45209f518695575100a433dca2e32f7a02d3d237e9f4c5bd1cb1c3553",
)
elif os.path.exists(os.path.join(extracted_dir, "jedm.cls")):
template_name = "JEDM"
else:
shared.warn("template_not_detected", tex=True)
exit()
print("Detected template:", template_name)
if not args.skip_compile:
print("Converting via make4ht")
scripts_dir = os.path.dirname(os.path.realpath(__file__))
mk4_template = os.path.join(scripts_dir, "make4ht_hardcode_bib.mk4")
if bib_backend:
mk4_template = os.path.join(scripts_dir, "make4ht_template.mk4")
with open(mk4_template) as infile:
with open(os.path.join(extracted_dir, "make4ht_with_bibtex.mk4"), "w") as ofile:
if bib_backend:
ofile.write('Make:add("bibtex", "%s ${input}")\n' % bib_backend)
ofile.write(infile.read())
shutil.copy(os.path.join(scripts_dir, "make4ht_preamble.cfg"), extracted_dir)
mathml = "mathml," if args.mathml else ""
if args.texlive:
bin_path = shutil.which("make4ht").replace("2025", str(args.texlive))[:-8]
os.environ["PATH"] = bin_path + ":" + os.environ["PATH"]
print("Using TeXLive", args.texlive)
assert os.path.exists(os.path.join(bin_path, "make4ht")), "No such TeXLive"
retcode = shared.exec_grouping_subprocesses(
"make4ht" + extra_flags + " --output-dir .. --format html5+common_domfilters "
"--build-file make4ht_with_bibtex.mk4 tmp-make4ht.tex "
'"' + mathml + 'mathjax,svg,fn-in" --config make4ht_preamble',
timeout=args.timeout_ms / 1000 if args.timeout_ms else None,
shell=True,
cwd=extracted_dir,
)
if retcode:
if template_name == "JEDM" and "{natbib}" in texstr:
shared.warn("natbib_jedm", tex=True)
shared.warn("make4ht_warnings", tex=True)
# Load HTML
if not os.path.exists(os.path.join(extracted_dir, "tmp-make4ht.html")):
shared.warn("make4ht_failed", tex=True)
if os.path.exists(os.path.join(extracted_dir, "tmp-make4ht.blg")):
print("\nBibliography log:")
error_count = 0
with open(os.path.join(extracted_dir, "tmp-make4ht.blg")) as blg:
for line in blg.readlines():
if line.startswith("You've used"):
break # End of useful output
print(line.strip())
if line.startswith("I'm skipping"):
error_count += 1
if error_count:
shared.warn("bib_compile_errors", str(error_count) + " error(s)", tex=True)
exit()
print("Loading converted HTML")
with open(os.path.join(extracted_dir, "tmp-make4ht.html")) as infile:
html_str = tex.fix_et_al(infile.read())
if " --lua" in extra_flags:
html_str = tex.lua_font_remap(html_str)
soup = BeautifulSoup(html_str, "html.parser")
texer = tex.TeXHandler(texstr, soup, template_name)
print("Formatting lists")
tex.parse_description_lists(texer)
print("Parsing headings")
tex.add_headings(texer)
print("Parsing authors")
tex.add_authors(texer)
shared.wrap_author_divs(texer.soup)
print("Merging unnecessary elements")
texer.merge_elements("span")
print("Formatting tables")
tex.format_tables(texer)
shared.fix_table_gaps(texer.soup)
print("Formatting figures")
tex.format_listings(texer)
tex.format_figures(texer)
shared.check_alt_text_duplicates(texer.soup, True)
tex.wrap_floating_algorithms(texer)
tex.copy_missing_images(texer, args.output_dir)
print("Formatting equations")
texer.format_equations()
tex.add_macros_for_mathjax(texer)
print("Formatting fonts")
texer.fix_fonts()
print("Parsing references")
texer.fix_references()
shared.mark_up_citations(texer.soup, template_name)
# Inline any styles made by ID
print("Inlining styles selected by ID")
cssutils.log.setLevel("FATAL")
css = cssutils.parseFile(os.path.join(extracted_dir, "tmp-make4ht.css"))
for rule in css:
if rule.type == rule.STYLE_RULE:
if "#" in rule.selectorText:
for elem in soup.select(rule.selectorText):
elem["style"] = rule.style.cssText + ";" + elem.get("style", "")
if elem.name == "col":
if not elem.parent.find_previous_sibling("colgroup"):
elem["style"] += "border-left:none;"
if not elem.parent.find_next_sibling("colgroup"):
elem["style"] += "border-right:none;"
if elem.name == "td":
if not elem.find_previous_sibling("td"):
elem["style"] += "border-left:none;"
if not elem.find_next_sibling("td"):
elem["style"] += "border-right:none;"
print("Removing unused IDs")
texer.remove_unused_ids()
print("Checking styles")
shared.check_styles(soup, args.output_dir, template_name, tex=True)
shared.check_citations_vs_references(
soup, args.output_dir, shared.CONFIG["anystyle_path"], template_name, tex=True
)
# Save result
print("Saving result")
shared.save_soup(soup.body, os.path.join(args.output_dir, "index.html"))
print("Removing unused tmp-* files from result folder")
soup_str = str(soup)
count_removed = 0
for fname in os.listdir(args.output_dir):
if fname.startswith("tmp-") and fname not in soup_str:
os.remove(os.path.join(args.output_dir, fname))
count_removed += 1
elif fname.startswith("tmp-") and fname.endswith(".svg"):
tex.fix_svg_quotes(os.path.join(args.output_dir, fname))
print("Removed", count_removed, "tmp-* file(s)")