#!/usr/bin/env python3 """The way from Markdown to the PDF of a company, measured at the PDF. 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 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): """The converter is called by its name, so it has to be executable. The README describes exactly that way — a link in a directory of the PATH — and a file without the bit ends in "Permission denied". Reported on 2026-09-21 from a company that had linked it and called it. """ check.that(os.access(CONVERTER, os.X_OK), "bin/md2pdf.py carries no execute bit, so a link to it on the " "PATH cannot be called by its name") def convert(check): shutil.rmtree(OUT, ignore_errors=True) os.makedirs(OUT, exist_ok=True) result = support.run(["python3", CONVERTER, "--identity", "nordwind", "--out-dir", OUT, "--keep-tex", os.path.join(EXAMPLES, "report.md")]) pdf = os.path.join(OUT, "report.pdf") check.that(os.path.exists(pdf), f"the converter produced no PDF:\n{result.stdout[-1500:]}") return pdf def measured(check): """The line and the character were measured, not taken from the fallback. The numbers the filters compute a column width from belong to the paper, the margins and the face of the company. They live outside TeX, in a Lua filter, so the converter builds a probe with the identity of the company and reads them out of it. Without the probe every company would compute with the numbers of the company this suite came from. """ metrics = os.path.join(OUT, "build", "metrics.txt") check.that(os.path.exists(metrics), "the converter wrote no measurement of the line") if not os.path.exists(metrics): return with open(metrics, encoding="utf-8") as handle: found = dict(re.findall(r"^(\w+) ([0-9.]+)pt$", handle.read(), re.MULTILINE)) check.that("linewidth" in found, "the probe measured no line width") if "linewidth" not in found: return # A4 is 597.5 points wide and the template keeps 1cm on each side, so the # line is 540 points give or take the rounding of the paper. width = float(found["linewidth"]) check.that(530 < width < 545, f"the measured line is {width} points wide, which is not the " "type area of an A4 page with the margins of this template") def inside(check, pdf): """Nothing of the document stands outside the type area. A Markdown table says nothing about its column widths, and without the filter LaTeX writes the last words of a wide table past the edge of the paper. """ page = Page(pdf, 1) factor = 72.0 / page.dpi columns = page.ink_columns() right = columns[-1] * factor left = columns[0] * factor # The paper is 595 points wide and the template keeps 1cm, 28.35 points, on # each side. Half a point of tolerance for the rounding of the raster. check.that(left > 27, f"ink stands {left:.1f} points from the left edge, " "inside the margin") check.that(right < 568, f"ink reaches {right:.1f} points, past the right " "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 `--[-][-]`, 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") check.that("Consetetur sadipscing elitr" in text and "41'820" in text, "the table did not arrive") # The path in the running text is broken at the characters that structure # it, so it stays inside the line. What arrives in the text layer is the # path with its pieces, never a line that runs past the margin. check.that("business-identity.sty" in text.replace("\n", ""), "the path in the running text did not arrive") faces = support.fonts(pdf) check.that(any("DejaVu" in face for face in faces), f"the marks did not reach the page: {faces}") # A formula in the three ways a writer writes one, and a fourth that is none: # inside a fenced code block the dollars are an example of themselves and stay # on the page as text. FORMULA = """--- title: Formula author: Erika Muster --- # Consetetur Inline $E = mc^2$ in a sentence. $$a = \\frac{b}{c}$$ $$ \\text{Ratio} = \\frac{\\text{Nutzen}} {\\text{Kosten}} $$ ```text $$ x = y $$ ``` """ def formula(check): """A formula arrives as a formula, also written over several lines. The grammar of the reader sees a line carrying nothing but `=` as the underline of a heading, so a formula over several lines used to come out as a heading of the first level with the dollars in it, and the build stayed green. Reported on 2026-09-21 from another company of this family, which built a twelve-page analysis with the formula standing as a green heading. """ path = os.path.join(support.BUILD, "formula.md") os.makedirs(support.BUILD, exist_ok=True) with open(path, "w", encoding="utf-8") as handle: handle.write(FORMULA) result = support.run(["python3", CONVERTER, "--identity", "nordwind", "--out-dir", OUT, "--keep-tex", path]) pdf = os.path.join(OUT, "formula.pdf") check.that(os.path.exists(pdf), f"the document with the formula did not build:\n" f"{result.stdout[-1500:]}") if not os.path.exists(pdf): return text = support.text(pdf) # The dollars of a formula never reach the page; the ones in the code block # do, and they are the proof that nothing was repaired in there. body = text.split("$$")[0] if "$$" in text else text check.that(text.count("$$") == 2, f"the page carries {text.count('$$')} rows of dollars, and only " "the two of the code block belong there") check.that("Ratio" in body and "Nutzen" in body, "the formula over several lines did not arrive as a formula") check.that("Kosten" in body, "the second half of the formula is missing") faces = support.fonts(pdf) check.that(any("Math" in face or "math" in face for face in faces), f"no mathematics face on a page with three formulas: {faces}") # The same document twice: once written in Markdown, once in LaTeX. A document # looks the same whichever road it takes, so the two pages carry their ink in # the same places. TWIN_BODY = ( ("Consetetur", "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " "sed diam nonumy eirmod tempor invidunt ut labore et dolore magna " "aliquyam erat."), ("Dolor Sit", "Stet clita kasd gubergren, no sea takimata sanctus est " "Lorem ipsum dolor sit amet."), ("Sadipscing Elitr", "At vero eos et accusam et justo duo dolores et ea " "rebum."), ) TWIN_MD = "---\ntitle: Lorem Ipsum\nauthor: Erika Muster\ndate: 21 September 2026\n---\n\n" \ + "\n".join(f"{'#' * (level + 1)} {head}\n\n{text}\n" for level, (head, text) in enumerate(TWIN_BODY)) TWIN_TEX = r"""\documentclass[a4paper,10pt]{article} \usepackage{nordwind} \title{Lorem Ipsum} \author{Erika Muster} \date{21 September 2026} \begin{document} \maketitle """ + "\n".join( "\\%s{%s}\n\n%s\n" % (("section", "subsection", "subsubsection")[level], head, text) for level, (head, text) in enumerate(TWIN_BODY)) + r""" \end{document} """ def bands(pdf): """The rows of ink of the first page, grouped into blocks, in points.""" page = Page(pdf, 1) factor = 72.0 / page.dpi grouped, current = [], [] for row in page.ink_rows(): if current and row - current[-1] > 1: grouped.append((current[0] * factor, current[-1] * factor)) current = [] current.append(row) if current: grouped.append((current[0] * factor, current[-1] * factor)) return grouped # A picture out of Markdown: the same rules as one written in LaTeX, and the # converter is what says which of them is a raster of pixels. PICTURE_MD = """--- title: Lorem Ipsum author: Erika Muster --- ## Consetetur ![Lorem ipsum dolor sit amet](example-image-16x9.pdf) ## Dolor Sit ![Consetetur sadipscing elitr](example-image-a.png) """ 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 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", "--out-dir", OUT, path]) pdf = os.path.join(OUT, "picture.pdf") check.that(os.path.exists(pdf), f"the document with the pictures did not build:\n" f"{result.stdout[-1000:]}") if not os.path.exists(pdf): return # 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 grouped, current = [], [] for row in page.ink_rows(): if current and row - current[-1] > 1: grouped.append(current) current = [] current.append(row) 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 check.that(width <= 545, 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"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): """A document out of Markdown is the document out of LaTeX. Both roads end in the same template, so the same content has to stand in the same places: same distances under the title block, same three lines over every heading, same line for every paragraph. """ md = support.write("twin.md", TWIN_MD) result = support.run(["python3", CONVERTER, "--identity", "nordwind", "--out-dir", OUT, md]) converted = os.path.join(OUT, "twin.pdf") check.that(os.path.exists(converted), f"the twin out of Markdown did not build:\n" f"{result.stdout[-1000:]}") written, _ = support.build(support.write("twin.tex", TWIN_TEX), EXAMPLES) if not os.path.exists(converted): return out_of_md, out_of_tex = bands(converted), bands(written) check.that(len(out_of_md) == len(out_of_tex), f"the page out of Markdown carries {len(out_of_md)} blocks of " f"ink and the one out of LaTeX {len(out_of_tex)}") if len(out_of_md) != len(out_of_tex): return worst = max(abs(a[0] - b[0]) for a, b in zip(out_of_md, out_of_tex)) check.that(worst < 1, f"a block of the page stands {worst:.1f} points lower out of " "Markdown than out of LaTeX") def missing(check): """A document that does not exist ends in a message and never in a PDF.""" result = support.run(["python3", CONVERTER, "--identity", "nordwind", "--out-dir", OUT, os.path.join(OUT, "gibtesnicht.md")]) check.that(result.returncode != 0, "the converter reported success for a file that does not exist") check.that("gibtesnicht.md" in result.stdout, "the message does not name the file that is missing") check.that(not os.path.exists(os.path.join(OUT, "gibtesnicht.pdf")), "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 = """ Payment service PAT """ LINKED_MD = """# Links [Payment service](#external-payment-service) and [PAT](#term-pat) are defined below. ![Functions](linked.svg) ## External functions
Logical designationDescriptionUKSwitzerland
Payment servicePaymentsStripe
## Terms | Term | Meaning | | --- | --- | | 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('\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 `\n" "\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"]*>", "", LINKED_SVG) STALE_MD = """# Drawing ![Functions](drawing.svg) ## Terms | Term | Meaning | | --- | --- | | Payment service | Payments | | 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) pdf = convert(check) if os.path.exists(pdf): measured(check) inside(check, pdf) content(check, pdf) formula(check) 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() if __name__ == "__main__": sys.exit(main())