1.0.2: converter searches no subfolders, safer filters, forms, links and watermark
A document at the root of a checkout of 68377 folders needed 56 minutes, because the converter named the suite, the document folder and the build folder with `//` in TEXINPUTS; it now names them without recursion and the same document converts in 11.5 seconds. A file below the document no longer replaces a package of TeX Live. The security review of the pending changes found four defects, each fixed with a test proven red against the old files and green against the new ones twice: an HTML table read in an empty sandbox, so an iframe opens no address; form labels written through the LaTeX writer, so they are set as text; the form pattern scans a line once instead of quadratically; a kept build folder no longer lays the links of an earlier drawing over a new one. The form tests remove the PDF of an earlier run before each conversion. Also carried: forms out of Markdown, link colours from the company, clickable links in prose, HTML tables and SVG drawings, the watermark, and the further changes of 1.0.1 and 1.0.2 in the CHANGELOG. npm test: 1232 of 1232 checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
adc58e19d7
commit
d7aac52ac7
28 files changed
+3918
-137
No files matched your search
+79
-9
@@ -52,12 +52,35 @@ import tempfile
|
||||
# suite is: the filters beside it would then be looked for in the directory of
|
||||
# the link, and the document would be built without them.
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
||||
# The directory of the packages and classes of the suite, the one directory of
|
||||
# it LaTeX needs: the rest of the repository holds examples and test builds.
|
||||
PACKAGES = os.path.join(ROOT, "texmf", "tex", "latex", "business-suite")
|
||||
FILTERS = [os.path.join(ROOT, "bin", name) for name in
|
||||
("details.lua", "svg.lua", "links.lua",
|
||||
("html.lua", "details.lua", "svg.lua", "links.lua",
|
||||
# Before the table filter: that one wraps a table in a group of its
|
||||
# own, and the sentence above would then no longer stand directly in
|
||||
# front of a table.
|
||||
"leadin.lua", "tables.lua", "code.lua", "figures.lua")]
|
||||
"leadin.lua", "tables.lua", "code.lua", "figures.lua",
|
||||
# Last: a checkbox and a field are raw LaTeX, and a filter that
|
||||
# reads the text of a paragraph would no longer find them.
|
||||
"forms.lua")]
|
||||
|
||||
# A terminal opened by a snap-packaged editor can point a system Inkscape at
|
||||
# the editor's GTK modules and settings schemas. The SVG filter starts Inkscape
|
||||
# through pandoc, so remove paths that belong to the parent application from
|
||||
# every converter subprocess.
|
||||
SNAP_LEAKS = ("GSETTINGS_SCHEMA_DIR", "GTK_PATH", "GIO_MODULE_DIR",
|
||||
"GTK_IM_MODULE_FILE", "XDG_DATA_DIRS")
|
||||
|
||||
# What makes a document a form: a task list item, which every platform renders
|
||||
# as a checkbox, and a paragraph that is a label, a colon and a run of
|
||||
# underscores. A document that carries neither gets no form dictionary, so
|
||||
# nothing about it changes.
|
||||
# The label part is written so that a line is scanned once: `^.*\S.*:` tried
|
||||
# every split of a line between its two `.*`, and a line of 40000 characters took
|
||||
# a second.
|
||||
FORM = re.compile(r"^\s*(?:[-*+]\s+\[[ xX]\]\s)|^[^\S\n]*\S[^\n]*?:\s*_{3,}\s*$",
|
||||
re.MULTILINE)
|
||||
|
||||
# Which language a document is written in decides where LaTeX may break a word,
|
||||
# and the wrong patterns break it in the wrong places: German patterns put
|
||||
@@ -128,8 +151,11 @@ LENGTH = re.compile(r"^(\w+) ([0-9.]+)pt$", re.MULTILINE)
|
||||
|
||||
def run(command, cwd, environment=None):
|
||||
"""One external command; its output comes back as text."""
|
||||
inherited = os.environ.copy()
|
||||
for name in SNAP_LEAKS:
|
||||
inherited.pop(name, None)
|
||||
return subprocess.run(command, cwd=cwd,
|
||||
env=dict(os.environ, **(environment or {})),
|
||||
env=dict(inherited, **(environment or {})),
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||||
encoding="utf-8", errors="replace")
|
||||
|
||||
@@ -154,8 +180,30 @@ def language(source, given):
|
||||
|
||||
|
||||
def searchpath(source_dir, build):
|
||||
"""Where LaTeX looks: the suite, the document, the build directory."""
|
||||
return os.pathsep.join([ROOT + "//", source_dir + "//", build + "//", ""])
|
||||
"""Where LaTeX looks: the package directory of the suite, what the caller
|
||||
searches, the document, the build directory, and the TeX tree.
|
||||
|
||||
What the caller searches comes BEFORE the document. A company whose identity
|
||||
lies outside the TeX tree names its directory in `TEXINPUTS`, and a file
|
||||
beside a document — a picture called `logo.pdf`, a package of the same name
|
||||
— must not stand in for the identity of the company. Until 2026-10-05 the
|
||||
converter replaced the search path of its caller, and such a company could
|
||||
not be built at all.
|
||||
|
||||
The converter's own entries name their directories without `//`, so no
|
||||
subfolder of them is searched. kpathsea expands `//` into every folder below
|
||||
the entry and tries each file name in all of them before it reaches the TeX
|
||||
tree, which comes last: for a document at the root of a checkout of 68377
|
||||
folders, one lookup of `fontspec.sty` took 257 seconds instead of 0.15, and
|
||||
the probe of one PDF ran for twelve and a half minutes, measured on
|
||||
2026-11-14. A file of the name of a TeX Live package anywhere below the
|
||||
document also replaced that package. A picture in a subfolder is named by
|
||||
its relative path from the document, which latexmk resolves in the directory
|
||||
of the document, so it needs no search.
|
||||
"""
|
||||
inherited = [entry for entry in
|
||||
os.environ.get("TEXINPUTS", "").split(os.pathsep) if entry]
|
||||
return os.pathsep.join([PACKAGES] + inherited + [source_dir, build, ""])
|
||||
|
||||
|
||||
def metrics(options, build, source_dir):
|
||||
@@ -171,8 +219,18 @@ def metrics(options, build, source_dir):
|
||||
"fontsize": options.fontsize,
|
||||
"identity": options.identity,
|
||||
"reference": REFERENCE})
|
||||
run(["xelatex", "-no-pdf", "-interaction=nonstopmode", "-halt-on-error",
|
||||
"metrics.tex"], cwd=build,
|
||||
# -no-shell-escape: the probe loads the identity from the search path, and
|
||||
# the directory of the document is on it, so a package lying there could
|
||||
# otherwise call the programs TeX Live allows in restricted mode.
|
||||
#
|
||||
# The probe is named by its full path. A bare file name is looked up through
|
||||
# the search path like a package, the suite and the directories of the
|
||||
# caller come before the build directory, and a `metrics.tex` an earlier
|
||||
# conversion left in the suite measured the example company for every
|
||||
# company: measured on 2026-10-05 in a Pacta document, whose head cells
|
||||
# stood past the right edge of the page.
|
||||
run(["xelatex", "-no-shell-escape", "-no-pdf", "-interaction=nonstopmode",
|
||||
"-halt-on-error", probe], cwd=build,
|
||||
environment={"TEXINPUTS": searchpath(source_dir, build)})
|
||||
written = os.path.join(build, "metrics.txt")
|
||||
if not os.path.exists(written):
|
||||
@@ -261,6 +319,11 @@ def to_latex(source, tex, build, options, measured):
|
||||
"-V", "header-includes=\\usepackage{" + options.identity + "}",
|
||||
"-V", "header-includes=\\usepackage{business-suite}",
|
||||
"-o", tex]
|
||||
with open(read, encoding="utf-8", errors="replace") as handle:
|
||||
if FORM.search(handle.read()):
|
||||
command[-2:-2] = ["-V", "header-includes=\\businessform"]
|
||||
print(f"{os.path.basename(source)}: a form, filled in and saved in "
|
||||
"the reader")
|
||||
if tag:
|
||||
command += ["-M", "lang=" + tag]
|
||||
print(f"{os.path.basename(source)}: {tag}")
|
||||
@@ -299,8 +362,15 @@ def row_rules(tex):
|
||||
|
||||
|
||||
def to_pdf(tex, build, source_dir):
|
||||
"""xelatex, through latexmk, with as many runs as the document needs."""
|
||||
return run(["latexmk", "-xelatex", "-interaction=nonstopmode",
|
||||
"""xelatex, through latexmk, with as many runs as the document needs.
|
||||
|
||||
-norc: latexmk would otherwise run the Perl code of a `latexmkrc` lying in
|
||||
the directory of the document, which is where it runs. -no-shell-escape:
|
||||
a package lying beside the document is found on the search path, and it
|
||||
could otherwise call the programs TeX Live allows in restricted mode.
|
||||
"""
|
||||
return run(["latexmk", "-norc", "-xelatex",
|
||||
"-latexoption=-no-shell-escape", "-interaction=nonstopmode",
|
||||
"-file-line-error", "-emulate-aux-dir",
|
||||
"-auxdir=" + build, "-outdir=" + build, tex],
|
||||
cwd=source_dir,
|
||||
|
||||
Reference in new issue
Block a user