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:
Marc WäckerlinandClaude Opus 5.5 committed 2026-11-14 12:14:55 +01:00
1 parent adc58e19d7
commit d7aac52ac7
28 files changed
+3918 -137

No files matched your search

+825 -17
View File
@@ -5,10 +5,15 @@ The converter is the second road into the same design, and the two have to end
in the same page: the same font, the same logo, the same table that fits the
line. What is measured here is the result and not the call.
"""
import http.server
import importlib.util
import os
import re
import shutil
import sys
import tempfile
import threading
import time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import support
@@ -17,6 +22,7 @@ from support import Check, Page
EXAMPLES = os.path.join(support.ROOT, "examples")
CONVERTER = os.path.join(support.ROOT, "bin", "md2pdf.py")
OUT = os.path.join(support.BUILD, "md2pdf")
support.name_example_company()
def runnable(check):
@@ -91,6 +97,104 @@ def inside(check, pdf):
"margin of the page")
# A paragraph of many short code spans in a row, the shape a style guide writes
# when it lists the names of a convention. Each span is shorter than a path and
# carries its boundaries inside itself, and between two of them stands nothing a
# line may break at but one space.
CODELINE_MD = """---
title: Inline code
---
## Variables
- **Name CSS custom properties consistently.** Use `--<property>[-<role>][-<context>]`, omitting unnecessary segments and placing the context last. Examples of Base Variables: `--font-size-base`, `--font-size-large`, `--color-dark`, `--size-medium`. Examples of Semantic Variables: `--color-fg`, `--color-bg-popup`, `--color-fg-error`, `--padding-box`, `--margin-box`, `--border-box`, `--gap-heading`. Base names identify a palette entry, scale step or universal HTML default; Semantic names identify a purpose or context.
"""
# What a break may never fall into: the hyphens a name begins with. `--` alone
# at the end of a line, or `-` with the rest of the name on the next, leaves a
# reader who copies the name with two pieces, neither of which is the name.
def convert_file(check, name, markdown):
"""One document of our own through the converter, as a reader receives it."""
source = support.write(name + ".md", markdown)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", "--out-dir", OUT, source])
pdf = os.path.join(OUT, name + ".pdf")
check.that(os.path.exists(pdf),
f"{name}.md did not build:\n{result.stdout[-1000:]}")
return pdf if os.path.exists(pdf) else None
def leading_hyphens(check):
"""A name that begins with hyphens keeps them, whatever the line does.
A custom property is written `--size-medium`, and the break the converter
offers after every structuring character fell between the two hyphens and
behind them: one line ended with `--` and the next began with `size-medium`,
another ended with `-` and the next began with `-margin-box`. The break
after an inner hyphen stays, which is where a reader reads a boundary
anyway.
"""
# Measured on the PAGE of the document whose lines really break, the same
# sentence the overflow was found in: the break this forbids is one TeX
# takes by itself after a hyphen, so a file carrying the right break points
# can still break in the wrong place. The first version of this check read
# the LaTeX, passed, and the page still ended a line with `--`.
pdf = convert_file(check, "codeline", CODELINE_MD)
if not pdf:
return
lines = [line.strip() for line
in support.run(["pdftotext", "-layout", pdf, "-"]).stdout
.splitlines() if line.strip()]
# A line that BEGINS with a name is right: the whole name moved to the next
# line. What is wrong is a line that ends with the hyphens alone and leaves
# the rest of the name behind.
ends = [line for line in lines if re.search(r"(^|\s)-+$", line)]
check.that(not ends,
"a line ends with the hyphens a name begins with: "
+ "; ".join(ends))
text = " ".join(lines)
for name in ("--size-medium", "--margin-box", "--color-fg-error"):
check.that(name.replace("-", "") in text.replace("-", "")
.replace(" ", ""),
f"the name {name} did not arrive on the page")
def code_line(check):
"""A line of inline code spans stays inside the type area.
A span of a typewriter face is one word to LaTeX: it carries no space and
hyphenation patterns do not reach it, so a line that ends in one runs past
the right edge. The converter offers a break at the characters that
structure such a name, and it did so only from sixteen characters up: a
paragraph that lists `--color-dark` and `--size-medium`, twelve and thirteen
characters, then had two lines over the edge, 3.5 and 7.9 points, while
`--color-fg-error` beside them broke after its hyphen. Measured on the
rendered page and in the build log.
"""
path = support.write("codeline.md", CODELINE_MD)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", "--out-dir", OUT, path])
pdf = os.path.join(OUT, "codeline.pdf")
check.that(os.path.exists(pdf),
f"the document of code spans did not build:\n"
f"{result.stdout[-1000:]}")
if not os.path.exists(pdf):
return
page = Page(pdf, 1)
factor = 72.0 / page.dpi
right = page.ink_columns()[-1] * factor
check.that(right < 568,
f"a line of code spans reaches {right:.1f} points, past the "
"right margin of the page")
log = os.path.join(OUT, "build", "codeline.log")
with open(log, encoding="utf-8", errors="replace") as handle:
over = [line for line in handle.read().splitlines()
if line.startswith("Overfull \\hbox")]
check.that(not over,
"the build reports a line over the edge: " + "; ".join(over[:2]))
def content(check, pdf):
text = support.text(pdf)
check.that("Lorem Ipsum" in text, "the title block did not arrive")
@@ -239,9 +343,9 @@ def pictures(check):
"""A picture out of Markdown takes the size of the family.
The converter hands every picture to pandoc's own bound, and that is where
the rules of the template hang: the lying picture on the first page takes
the whole line, the raster one on the second is the one the converter marks
as pixels, and both stand centred.
the rules of the template hang: the lying picture takes the whole line, the
raster one is the one the converter marks as pixels, both stand centred, and
both carry the caption the document gave them.
"""
path = support.write("picture.md", PICTURE_MD)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
@@ -252,31 +356,128 @@ def pictures(check):
f"{result.stdout[-1000:]}")
if not os.path.exists(pdf):
return
for number, name in ((1, "lying"), (2, "raster")):
# Both captions reach the page. A raster picture carries the mark of its
# kind in front of it, and the paragraph was then two things and no longer
# a picture alone, so it lost its caption: measured on 2026-10-05.
text = support.run(["pdftotext", pdf, "-"]).stdout
for caption in ("Lorem ipsum dolor sit amet", "Consetetur sadipscing elitr"):
check.that(caption in text,
f"the caption «{caption}» did not reach the page")
# A picture that moves to the next page leaves the room it did not take
# EMPTY, at the foot of the page. The page used to be stretched over that
# room instead: the author stood 240 points under the title, and the heading
# over the first picture ended up inside it. Measured on 2026-10-05.
first = support.words(pdf, 1)
title = [word for word in first if word[0] == "Lorem" and word[2] < 120]
author = [word for word in first if word[0] == "Erika"]
if title and author:
gap = author[0][2] - max(word[4] for word in title)
check.that(gap < 30,
f"the author stands {gap:.0f} points under the title: the "
"page was stretched over the room a picture left")
# And the raster picture with its caption fits into what the first page has
# left, which is more than the quarter the template may leave empty: it is
# made smaller and stays. Inside a figure the caption under the picture was
# not counted, so the picture made to fit the room did not fit with its
# caption and moved after all.
check.that(support.pages(pdf) == 1,
f"the raster picture moved to page {support.pages(pdf)} although "
"the first page had more than a quarter of its height left for it")
heading = [word for word in first if word[0] == "Consetetur"
and word[2] > 100]
figure = [word for word in first if word[0] == "16×9"]
if heading and figure:
check.that(heading[0][4] < figure[0][2] - 60,
"the heading over the first picture stands inside it")
# The pictures are the two tallest bands of ink, on whatever page each one
# stands. The page was once assumed: the raster picture was read off page
# 2 of a document of one page, which the old reader of the rendered page
# answered with a file an earlier test had left behind.
bands_found = []
for number in range(1, support.pages(pdf) + 1):
page = Page(pdf, number)
factor = 72.0 / page.dpi
rows = page.ink_rows()
check.that(rows, f"the {name} picture is not on page {number}")
if not rows:
continue
grouped, current = [], []
for row in rows:
for row in page.ink_rows():
if current and row - current[-1] > 1:
grouped.append(current)
current = []
current.append(row)
grouped.append(current)
tallest = max(grouped, key=lambda band: band[-1] - band[0])
columns = page.ink_columns(tallest[0], tallest[-1] + 1)
if current:
grouped.append(current)
for band in grouped:
columns = page.ink_columns(band[0], band[-1] + 1)
bands_found.append((band[-1] - band[0], number, columns, page.width,
factor))
pictures_found = sorted(bands_found, reverse=True)[:2]
check.that(len(pictures_found) == 2, "the two pictures did not reach the PDF")
for height, number, columns, page_width, factor in pictures_found:
width = (columns[-1] - columns[0] + 1) * factor
left = columns[0] * factor
right = page.width * factor - columns[-1] * factor
right = page_width * factor - columns[-1] * factor
check.that(width <= 545,
f"the {name} picture is {width:.1f} points wide, more than "
"the 540.6 of the line")
f"a picture on page {number} is {width:.1f} points wide, "
"more than the 540.6 of the line")
check.that(abs(left - right) < 4,
f"the {name} picture stands {left:.1f} points from the left "
f"edge and {right:.1f} from the right, so it is not centred")
f"a picture on page {number} stands {left:.1f} points from "
f"the left edge and {right:.1f} from the right, so it is not "
"centred")
# A page whose content has fixed heights, so that what is left for the picture
# is known: the title block, one sentence, a block of 62 percent of the text
# height, and then a heading with a lying picture that needs more than the
# quarter of a page that remains. The picture stands under a heading, which
# holds it, so the two move together and the page ends after the block; the
# spaces of the page are few, so their play cannot cover the room left.
SHORT_TEX = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\title{Lorem Ipsum}
\author{Erika Muster}
\begin{document}
\maketitle
Lorem ipsum dolor sit amet.
\noindent\rule{0.5\linewidth}{0.62\textheight}
\subsection{Dolor Sit}
\businessgraphic{example-image-16x9}
Consetetur sadipscing elitr.
\end{document}
"""
def short_page(check):
"""A page that a picture leaves early keeps the rest empty at its foot.
A picture that does not fit into the last quarter of a page moves to the
next one, and the room it leaves stays empty. The page used to be stretched
over that room instead, every space past the half line it may grow by: the
author then stood 240 points under the title. The distance between the
title and the author is read off the first page.
And the picture does move: a lying picture used to take whatever height was
left, so with a strip of 60 points it came out 60 points high on the first
page, and the quarter the template may leave empty never applied.
"""
pdf, _ = support.build(support.write("shortpage.tex", SHORT_TEX), EXAMPLES)
# The lying picture moves to the next page and does not shrink into the
# strip that is left: less than a quarter of the page remains for it.
first = support.run(["pdftotext", "-f", "1", "-l", "1", pdf, "-"]).stdout
check.that("16×9" not in first and support.pages(pdf) == 2,
"the lying picture shrank into the last strip of the first page "
"instead of moving to the next one")
found = support.words(pdf, 1)
title = [word for word in found if word[0] == "Lorem" and word[2] < 120]
author = [word for word in found if word[0] == "Erika"]
check.that(title and author, "the title block did not reach the page")
if not title or not author:
return
gap = author[0][2] - max(word[4] for word in title)
check.that(gap < 30,
f"the author stands {gap:.0f} points under the title: the page "
"was stretched over the room the picture left")
def twins(check):
@@ -321,6 +522,599 @@ def missing(check):
"the converter produced a PDF for a file that does not exist")
def foreign_rc(check):
"""A latexmkrc beside a document is not executed by the converter.
latexmk runs the Perl code of a `latexmkrc` in its working directory, and
the converter builds inside the directory of the document, so a document
that arrives with such a file would run its code on the machine that
converts it. The rc file here writes a marker file; the marker must not
appear, and the document must still come out.
"""
folder = os.path.join(support.BUILD, "foreign-rc")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
with open(os.path.join(folder, "doc.md"), "w", encoding="utf-8") as handle:
handle.write("# Lorem Ipsum\n\nConsetetur sadipscing elitr.\n")
with open(os.path.join(folder, "latexmkrc"), "w", encoding="utf-8") as handle:
handle.write("open(my $f, '>', 'rc-ran.txt'); close($f);\n")
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
os.path.join(folder, "doc.md")], cwd=folder)
check.that(os.path.exists(os.path.join(folder, "doc.pdf")),
f"the document beside a latexmkrc did not build:\n"
f"{result.stdout[-1000:]}")
check.that(not os.path.exists(os.path.join(folder, "rc-ran.txt")),
"latexmk executed the latexmkrc lying beside the document")
SHELL_MARKER = "shell-ran.ind"
# An identity that lies beside the document and starts a program as it loads.
# The converter puts the directory of the document on the search path, so the
# probe and the build both read it from there.
SHELL_IDENTITY = (r"\ProvidesPackage{shellcompany}" "\n"
r"\immediate\write18{makeindex -o " + SHELL_MARKER
+ r" entries.idx}" "\n"
r"\RequirePackage{nordwind}" "\n")
SHELL_TEX = (r"\documentclass[a4paper,10pt]{article}" "\n"
r"\usepackage{shellcompany}" "\n"
r"\begin{document}Consetetur.\end{document}" "\n")
def shell_escape(check):
"""A document cannot start a program through the converter.
A raw LaTeX block out of the Markdown comes out as verbatim, a header field
and the label of a form field as escaped text; math reaches LaTeX as it is
written, which is why a document is converted only when it is trusted. What
reaches
LaTeX is every package file in the directory of the document, because the
converter searches there, and TeX Live allows a restricted set of programs
by default, `makeindex` among them. The package here asks for `makeindex`
through `\\write18` to write a marker file, and the document is converted
with that package as its identity: the marker must not appear. A control
run builds a document with the same package without the converter: the
marker must appear there, which proves that the program is allowed and
that this check can see it.
"""
folder = os.path.join(support.BUILD, "shell-escape")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
for name, text in (("shellcompany.sty", SHELL_IDENTITY),
("doc.md", "# Lorem Ipsum\n\nConsetetur sadipscing.\n"),
("entries.idx", "\\indexentry{a}{1}\n"),
("control.tex", SHELL_TEX)):
with open(os.path.join(folder, name), "w", encoding="utf-8") as handle:
handle.write(text)
result = support.run(["python3", CONVERTER, "--identity", "shellcompany",
os.path.join(folder, "doc.md")], cwd=folder)
check.that(os.path.exists(os.path.join(folder, "doc.pdf")),
f"the document with a package of its own did not build:\n"
f"{result.stdout[-1000:]}")
check.that(not os.path.exists(os.path.join(folder, SHELL_MARKER)),
"xelatex started a program for a package beside the document")
support.run(["latexmk", "-norc", "-xelatex", "-interaction=nonstopmode",
"-outdir=" + os.path.join(folder, "control"), "control.tex"],
cwd=folder,
environment={"TEXINPUTS": support.searchpath(EXAMPLES, folder)})
check.that(os.path.exists(os.path.join(folder, SHELL_MARKER)),
"the control run without the converter did not start the "
"program, so this check proves nothing")
# A company whose identity lies outside the TeX tree, and a file of the same
# name beside the document that claims to be it.
ELSEWHERE_IDENTITY = (r"\ProvidesPackage{elsewhere}" "\n"
r"\RequirePackage{nordwind}" "\n"
r"\businessidentity{name=Fernwald}" "\n")
DECOY_IDENTITY = (r"\ProvidesPackage{elsewhere}" "\n"
r"\RequirePackage{nordwind}" "\n"
r"\businessidentity{name=Attrappe}" "\n")
def inherited_search(check):
"""A company names the directory of its identity, and that one is used.
A company whose identity lies outside the TeX tree calls the converter with
its directory in `TEXINPUTS`. The converter replaced that search path with
its own until 2026-10-05, so such a company did not build at all. The
directory of the caller comes before the folder of the document: a file
beside a document must not stand in for the identity of the company, so the
fixture puts one there that carries another name.
The fixture lies OUTSIDE this repository. Until 2026-11-14 the converter
searched the suite first and through every folder of it, so a fixture under
`tests/build` was found there before anything else, and the check measured
the order of two files in one tree instead of the order of the search path.
"""
folder = tempfile.mkdtemp(prefix="business-inherited-")
try:
identity = os.path.join(folder, "identity")
document = os.path.join(folder, "document")
os.makedirs(identity)
os.makedirs(document)
for path, text in (
(os.path.join(identity, "elsewhere.sty"), ELSEWHERE_IDENTITY),
(os.path.join(document, "elsewhere.sty"), DECOY_IDENTITY),
(os.path.join(document, "doc.md"),
"# Lorem Ipsum\n\nConsetetur sadipscing elitr.\n")):
with open(path, "w", encoding="utf-8") as handle:
handle.write(text)
result = support.run(["python3", CONVERTER, "--identity", "elsewhere",
os.path.join(document, "doc.md")], cwd=document,
environment={"TEXINPUTS": os.pathsep.join(
[identity + "//", EXAMPLES, ""])})
pdf = os.path.join(document, "doc.pdf")
check.that(os.path.exists(pdf),
f"a company named in TEXINPUTS did not build:\n"
f"{result.stdout[-1000:]}")
if not os.path.exists(pdf):
return
text = support.run(["pdftotext", pdf, "-"]).stdout
check.that("Fernwald" in text and "Attrappe" not in text,
"the identity came from the folder of the document and not "
"from the directory the caller named")
finally:
shutil.rmtree(folder, ignore_errors=True)
# A probe of the same name as the one the converter writes, on the search path
# of the caller, which measures a line one point wide.
DECOY_PROBE = (r"\documentclass{article}\begin{document}" "\n"
r"\newwrite\businessmetrics" "\n"
r"\immediate\openout\businessmetrics=metrics.txt" "\n"
r"\immediate\write\businessmetrics{linewidth 1.0pt}" "\n"
r"\immediate\write\businessmetrics{reference 1.0pt}" "\n"
r"\immediate\closeout\businessmetrics" "\n"
r"\end{document}" "\n")
def own_probe(check):
"""The line is measured in the probe the converter wrote, and in no other.
The probe is a LaTeX file in the build directory, and xelatex looks up a
file name without a directory through the search path, where the suite and
the directories of the caller come first. A `metrics.tex` left in the suite
by an earlier conversion was found there and measured the face of the
example company for every company: Pacta built its tables with the width of
a character of TeX Gyre Pagella, 4.55 points instead of 5.24, and a word of
a head cell stood 7 points past the right edge of the page. Measured on
2026-10-05. The decoy here lies outside this repository, on the search path
the caller hands over.
"""
folder = tempfile.mkdtemp(prefix="business-probe-")
try:
decoy = os.path.join(folder, "decoy")
document = os.path.join(folder, "document")
os.makedirs(decoy)
os.makedirs(document)
for path, text in ((os.path.join(decoy, "metrics.tex"), DECOY_PROBE),
(os.path.join(document, "doc.md"),
"# Lorem Ipsum\n\nConsetetur sadipscing elitr.\n")):
with open(path, "w", encoding="utf-8") as handle:
handle.write(text)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", os.path.join(document, "doc.md")],
cwd=document,
environment={"TEXINPUTS": os.pathsep.join(
[decoy + "//", EXAMPLES, ""])})
metrics = os.path.join(document, "build", "metrics.txt")
check.that(os.path.exists(metrics),
f"the converter wrote no measurement of the line:\n"
f"{result.stdout[-1000:]}")
if not os.path.exists(metrics):
return
# Which file xelatex really ran, out of its log. The width alone does
# not tell: a probe of the example company left in this repository
# measures the same line as the right one, and it is found before the
# decoy. The log breaks its lines at 79 characters, so they are joined.
log = os.path.join(document, "build", "metrics.log")
ran = ""
if os.path.exists(log):
with open(log, encoding="utf-8", errors="replace") as handle:
ran = handle.read().replace("\n", "")
own = os.path.join(document, "build", "metrics.tex")
check.that("(" + own in ran,
"xelatex did not run the probe the converter wrote into its "
"build directory, but a file of the same name from the "
"search path")
with open(metrics, encoding="utf-8") as handle:
found = dict(re.findall(r"^(\w+) ([0-9.]+)pt$", handle.read(),
re.MULTILINE))
width = float(found.get("linewidth", 0))
check.that(530 < width < 545,
f"the measured line is {width} points wide: the converter "
"ran a file called metrics.tex from the search path instead "
"of its own probe")
finally:
shutil.rmtree(folder, ignore_errors=True)
# A package named like one of the TeX tree, which stops the build where it is
# read, and an identity of a name nothing else of the suite carries.
DECOY_PACKAGE = (r"\ProvidesPackage{fontspec}" "\n"
r"\errmessage{a package from a subfolder of the document "
r"was loaded}" "\n")
SUBFOLDER_IDENTITY = (r"\ProvidesPackage{businesssubfolder}" "\n"
r"\RequirePackage{nordwind}" "\n")
def subfolders(check):
"""The folder of the document and the suite are searched, no folder below.
kpathsea expands a search path entry that ends in `//` into every folder
below it, and the TeX tree comes last: a document at the root of a checkout
of 68377 folders took 257 seconds for one lookup of `fontspec.sty` instead
of 0.15, and its probe ran for twelve and a half minutes, measured on
2026-11-14. A file named like a package of the TeX tree anywhere below the
document replaced that package, and a package left in a test build of this
repository could serve as the identity of a company.
The folder of the first document carries a `fontspec.sty` two folders down
that stops LaTeX: the document must build. A package of a name only this
check uses lies two folders down in the test builds of the suite: asked for
as the identity of the second document, it must not be found.
"""
folder = tempfile.mkdtemp(prefix="business-subfolders-")
suite = os.path.join(support.BUILD, "subfolders")
shutil.rmtree(suite, ignore_errors=True)
try:
decoyed = os.path.join(folder, "decoyed")
plain = os.path.join(folder, "plain")
hidden = os.path.join(suite, "notes", "drafts")
os.makedirs(os.path.join(decoyed, "notes", "drafts"))
os.makedirs(plain)
os.makedirs(hidden)
text = "# Lorem Ipsum\n\nConsetetur sadipscing elitr.\n"
for path, content in (
(os.path.join(decoyed, "notes", "drafts", "fontspec.sty"),
DECOY_PACKAGE),
(os.path.join(hidden, "businesssubfolder.sty"),
SUBFOLDER_IDENTITY),
(os.path.join(decoyed, "doc.md"), text),
(os.path.join(plain, "doc.md"), text)):
with open(path, "w", encoding="utf-8") as handle:
handle.write(content)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", os.path.join(decoyed, "doc.md")],
cwd=decoyed)
check.that(os.path.exists(os.path.join(decoyed, "doc.pdf")),
"a fontspec.sty in a subfolder of the document replaced "
f"the package of the TeX tree:\n{result.stdout[-1000:]}")
result = support.run(["python3", CONVERTER, "--identity",
"businesssubfolder", "--keep-tex",
os.path.join(plain, "doc.md")], cwd=plain)
# The log breaks its lines at 79 characters, so they are joined.
log = os.path.join(plain, "build", "doc.log")
written = ""
if os.path.exists(log):
with open(log, encoding="utf-8", errors="replace") as handle:
written = handle.read().replace("\n", "")
check.that(result.returncode != 0
and "File `businesssubfolder.sty' not found" in written,
"a package in a subfolder of the suite was found as the "
f"identity of a company:\n{result.stdout[-1000:]}")
finally:
shutil.rmtree(folder, ignore_errors=True)
shutil.rmtree(suite, ignore_errors=True)
# A document with a title whose first section runs over the first page, so that
# the second page begins inside it; the second section opens further down.
HEAD_PARAGRAPH = ("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed "
"diam nonumy eirmod tempor invidunt ut labore et dolore magna "
"aliquyam erat, sed diam voluptua. ") * 5
HEAD_MD = ("---\ntitle: Nordwind Quartal\nauthor: Erika Muster\n---\n\n"
"# Consetetur\n\n" + "\n\n".join([HEAD_PARAGRAPH] * 9)
+ "\n\n# Dolor Sit\n\n" + HEAD_PARAGRAPH + "\n")
# The same title, and a section that the document opens on a page of its own.
HEAD_TEX = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\title{Nordwind Quartal}
\author{Erika Muster}
\begin{document}
\maketitle
Lorem ipsum dolor sit amet.
\newpage
\section{Dolor Sit}
Consetetur sadipscing elitr.
\end{document}
"""
def head_words(pdf, page):
"""The words of the running head: the upper band of the left half."""
return [word for word in support.words(pdf, page)
if word[4] < 60 and word[1] < 298]
def running_head(check):
"""The head of a page after the title names the section, not the title.
The title stands in the head of the page with the title block only, the page
that begins before any section. Whether a page begins before any section
was asked of the marks a heading leaves at the top of a page, and a section
that begins in the middle of a page leaves none there: every page of a
document out of Markdown with a title then carried the title in its head.
Measured on 2026-10-05 on the second page of a Pacta document that began
inside its first section. A section that opens a page of its own is the
other half of the same question.
"""
md = support.write("head.md", HEAD_MD)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--out-dir", OUT, md])
pdf = os.path.join(OUT, "head.pdf")
check.that(os.path.exists(pdf),
f"the document with two sections did not build:\n"
f"{result.stdout[-1000:]}")
if os.path.exists(pdf):
check.that(support.pages(pdf) >= 2,
"the first section does not reach the second page, so the "
"head of a page inside it cannot be measured")
first = [word[0] for word in head_words(pdf, 1)]
second = [word[0] for word in head_words(pdf, 2)]
check.that("Quartal" in first,
f"the head of the page with the title block says {first}, "
"without the title")
check.that("Consetetur" in second and "Quartal" not in second,
f"the second page begins inside «Consetetur» and its head "
f"says {second}")
written, _ = support.build(support.write("headpage.tex", HEAD_TEX), EXAMPLES)
second = [word[0] for word in head_words(written, 2)]
check.that("Dolor" in second and "Quartal" not in second,
f"the second page opens with «Dolor Sit» and its head says "
f"{second}")
SAME_NAME_MD = """# Lorem Ipsum
![Consetetur](a/mark.svg)
![Sadipscing](b/mark.svg)
![Consetetur again](a/mark.svg)
"""
LINKED_SVG = """<svg xmlns="http://www.w3.org/2000/svg" width="200" height="80"
viewBox="0 0 200 80">
<a href="#external-payment-service"><text x="10" y="25" font-size="12"
textLength="100">Payment service</text></a>
<a href="#term-pat"><text x="10" y="55" font-size="12"
textLength="24">PAT</text></a>
</svg>
"""
LINKED_MD = """# Links
[Payment service](#external-payment-service) and [PAT](#term-pat) are defined below.
![Functions](linked.svg)
## External functions
<table>
<tr><th>Logical designation</th><th>Description</th><th>UK</th><th>Switzerland</th></tr>
<tr><td><a id="external-payment-service"></a>Payment service</td><td>Payments</td><td colspan="2">Stripe</td></tr>
</table>
## Terms
| Term | Meaning |
| --- | --- |
| <a id="term-pat"></a>PAT | Pre-approval estimate |
"""
def annotations(pdf, page=1):
"""The link annotations a reader can operate on one finished PDF page."""
shown = support.run(["mutool", "show", pdf, f"pages/{page}"]).stdout
found = re.search(r"/Annots (\d+) 0 R", shown)
if not found:
return []
array = support.run(["mutool", "show", pdf, found.group(1)]).stdout
return [support.run(["mutool", "show", pdf, number]).stdout
for number in re.findall(r"(\d+) 0 R", array)]
def linked_content(check):
"""Links in prose and in an SVG reach definitions in the finished PDF.
Markdown links became coloured text without an annotation because the raw
HTML anchors of tables were dropped by the LaTeX writer. SVG links existed
in the drawing and disappeared when the drawing was converted and embedded.
This fixture uses both paths and reads the final PDF objects a reader clicks.
"""
folder = os.path.join(support.BUILD, "linked-content")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
for name, content in (("linked.svg", LINKED_SVG), ("linked.md", LINKED_MD)):
with open(os.path.join(folder, name), "w", encoding="utf-8") as handle:
handle.write(content)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", os.path.join(folder, "linked.md")],
cwd=folder)
pdf = os.path.join(folder, "linked.pdf")
check.that(os.path.exists(pdf),
"the linked document did not build:\n" + result.stdout[-1000:])
if not os.path.exists(pdf):
return
text = support.text(pdf)
check.that("Payment service" in text and "Stripe" in text
and "Pre-approval estimate" in text,
"the HTML provider table or Markdown glossary did not reach "
"the finished PDF")
destinations = support.run(["pdfinfo", "-dests", pdf]).stdout
for target in ("external-payment-service", "term-pat"):
check.that(f'"{target}"' in destinations,
f"the definition {target} is not a destination in the PDF")
links = "\n".join(annotations(pdf))
for target in ("external-payment-service", "term-pat"):
count = links.count(f"/D ({target})")
check.that(count == 2,
f"{target} has {count} clickable links instead of one in "
"prose and one in the drawing")
def same_name_drawings(check):
"""Two drawings of one file name in two folders stay two drawings.
The drawings are converted to PDF before LaTeX runs, into one build
directory. Named after the file alone, `b/mark.svg` overwrote `a/mark.svg`
and the document showed the second drawing twice. A drawing shown twice is
converted once.
"""
folder = os.path.join(support.BUILD, "same-name")
shutil.rmtree(folder, ignore_errors=True)
for name, colour in (("a", "red"), ("b", "blue")):
os.makedirs(os.path.join(folder, name))
with open(os.path.join(folder, name, "mark.svg"), "w",
encoding="utf-8") as handle:
handle.write('<svg xmlns="http://www.w3.org/2000/svg" width="20" '
'height="20"><rect width="20" height="20" fill="'
+ colour + '"/></svg>\n')
with open(os.path.join(folder, "doc.md"), "w", encoding="utf-8") as handle:
handle.write(SAME_NAME_MD)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", os.path.join(folder, "doc.md")],
cwd=folder)
build_dir = os.path.join(folder, "build")
made = sorted(name for name in os.listdir(build_dir)
if name.endswith("mark.pdf")) \
if os.path.isdir(build_dir) else []
check.that(len(made) == 2,
f"three pictures of two drawings called mark.svg gave "
f"{len(made)} converted files instead of 2:\n"
f"{result.stdout[-1000:]}")
if len(made) != 2:
return
contents = []
for name in made:
with open(os.path.join(build_dir, name), "rb") as handle:
contents.append(handle.read())
check.that(contents[0] != contents[1],
"the two converted drawings are identical, so one drawing "
"replaced the other")
def html_sandbox(check):
"""A raw HTML table opens no address while it is read.
The HTML reader of pandoc fetches the page an `<iframe>` names and pastes it
into the document. The table is read in an empty sandbox, so a page lying
beside the document, an internal service or a tracking address never reaches
the PDF and is never asked.
"""
folder = os.path.join(support.BUILD, "html-sandbox")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
asked = []
class Page(http.server.BaseHTTPRequestHandler):
def do_GET(self):
asked.append(self.path)
body = b"<p>Zebra marker of the fetched page</p>"
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *arguments):
pass
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Page)
threading.Thread(target=server.serve_forever, daemon=True).start()
try:
with open(os.path.join(folder, "doc.md"), "w", encoding="utf-8") as handle:
handle.write("# Sandbox\n\n<table>\n<tr><th>Cell</th></tr>\n"
"<tr><td>Kept text <iframe src=\"http://127.0.0.1:"
f"{server.server_port}/page.html\"></iframe></td></tr>\n"
"</table>\n")
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
os.path.join(folder, "doc.md")], cwd=folder)
finally:
server.shutdown()
server.server_close()
check.that(not asked, f"reading the HTML table asked {asked} of a server")
pdf = os.path.join(folder, "doc.pdf")
check.that(os.path.exists(pdf),
"the document with an HTML table did not build:\n"
+ result.stdout[-1000:])
if not os.path.exists(pdf):
return
text = support.text(pdf)
check.that("Kept text" in text, "the HTML table did not reach the PDF")
check.that("Zebra marker" not in text,
"the page an iframe of the HTML table names was fetched into the PDF")
def form_pattern(check):
"""The pattern that recognises a form reads a line once.
`^.*\\S.*:` tried every split of a line between its two `.*`: 0.29 seconds
for a line of 20000 characters and 1.01 for 40000, measured on 2026-11-14,
so a megabyte of inline data on one line held the build for minutes. The
limit lies far below the 7 seconds the old pattern needs for 100000.
"""
spec = importlib.util.spec_from_file_location("converter_under_test", CONVERTER)
converter = importlib.util.module_from_spec(spec)
spec.loader.exec_module(converter)
for line, form in (("Name: ________", True), (" Remarks: ____ ", True),
("- [ ] An item", True), (": ________", False),
("No label ________", False), ("Two: colons: ____", True)):
check.that(bool(converter.FORM.search(line)) == form,
f"«{line}» is {'not ' if form else ''}read as a form")
start = time.monotonic()
converter.FORM.search("a" + "b" * 100000 + "\n")
seconds = time.monotonic() - start
check.that(seconds < 1, f"a line of 100000 characters took {seconds:.2f} s")
UNLINKED_SVG = re.sub(r"</?a[^>]*>", "", LINKED_SVG)
STALE_MD = """# Drawing
![Functions](drawing.svg)
## Terms
| Term | Meaning |
| --- | --- |
| <a id="external-payment-service"></a>Payment service | Payments |
| <a id="term-pat"></a>PAT | Pre-approval estimate |
"""
def stale_links(check):
"""A kept build folder lays no links of an earlier drawing over a new one.
The drawings are numbered from one in every run, and a drawing without links
wrote no link file, so with `--keep-tex` the file of the earlier drawing of
that number stayed and the template laid its links over the new picture.
"""
folder = os.path.join(support.BUILD, "stale-links")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
source = os.path.join(folder, "doc.md")
with open(source, "w", encoding="utf-8") as handle:
handle.write(STALE_MD)
pdf = os.path.join(folder, "doc.pdf")
for drawing in (LINKED_SVG, UNLINKED_SVG):
with open(os.path.join(folder, "drawing.svg"), "w", encoding="utf-8") as handle:
handle.write(drawing)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", source], cwd=folder)
check.that(os.path.exists(pdf), "the document with a drawing did not "
"build:\n" + result.stdout[-1000:])
if not os.path.exists(pdf):
return
links = "\n".join(annotations(pdf)).count("/D (term-pat)")
if drawing is LINKED_SVG:
check.that(links == 1, f"the drawing with links carries {links} "
"links to its term, so this check proves nothing")
check.that(links == 0, f"the drawing without links carries {links} links "
"of the drawing that stood there before")
def main():
check = Check("md2pdf")
runnable(check)
@@ -333,6 +1127,20 @@ def main():
pictures(check)
twins(check)
missing(check)
foreign_rc(check)
shell_escape(check)
linked_content(check)
html_sandbox(check)
form_pattern(check)
stale_links(check)
same_name_drawings(check)
inherited_search(check)
own_probe(check)
subfolders(check)
running_head(check)
code_line(check)
leading_hyphens(check)
short_page(check)
return check.done()