Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/prose-sage-pxd028735/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sha256 text eol=lf
543 changes: 543 additions & 0 deletions docs/prose-sage-pxd028735/EXPERIMENT_REPORT.md

Large diffs are not rendered by default.

457 changes: 457 additions & 0 deletions docs/prose-sage-pxd028735/EXPERIMENT_REPORT.template.md

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions docs/prose-sage-pxd028735/build_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
"""Splice the generated tables into the report template.

usage: build_report.py TEMPLATE TABLES OUT

Markers are addressed by each generated table's "### Table N" heading:

@@TABLES@@ every table, in order
@@TABLES_UPTO_1@@ tables 1..1
@@TABLES_FROM_2@@ tables 2..N

The ranged form lets the template keep prose and figures between two tables.
Every generated table must be emitted exactly once: a table covered twice, or
not at all, is an error rather than a silently missing section.
"""
import re
import sys

MARKER = re.compile(r"@@TABLES(?:_UPTO_(\d+)|_FROM_(\d+))?@@")
HEADING = re.compile(r"^### Table (\d+)", re.M)


def split_tables(text):
"""Split the generated tables on their '### Table N' headings."""
starts = [(int(m.group(1)), m.start()) for m in HEADING.finditer(text)]
if not starts:
raise SystemExit("generated tables contain no '### Table N' heading")
chunks = []
for i, (num, pos) in enumerate(starts):
end = starts[i + 1][1] if i + 1 < len(starts) else len(text)
chunks.append((num, text[pos:end].rstrip("\n")))
return chunks


def main():
if len(sys.argv) != 4:
raise SystemExit("usage: build_report.py TEMPLATE TABLES OUT")
tmpl_path, tables_path, out_path = sys.argv[1:4]
tmpl = open(tmpl_path, encoding="utf-8").read()
chunks = split_tables(open(tables_path, encoding="utf-8").read())
numbers = [n for n, _ in chunks]

if not MARKER.search(tmpl):
raise SystemExit("template contains no @@TABLES@@ marker")

covered = []

def replace(match):
lo, hi = 1, float("inf")
if match.group(1):
hi = int(match.group(1))
elif match.group(2):
lo = int(match.group(2))
picked = [(n, t) for n, t in chunks if lo <= n <= hi]
if not picked:
raise SystemExit("marker %s selects no table" % match.group(0))
covered.extend(n for n, _ in picked)
return "\n\n".join(t for _, t in picked)

report = MARKER.sub(replace, tmpl)

missing = [n for n in numbers if n not in covered]
repeated = sorted({n for n in covered if covered.count(n) > 1})
if missing or repeated:
raise SystemExit("table coverage is not exactly once: missing=%s repeated=%s"
% (missing, repeated))

with open(out_path, "w", encoding="utf-8", newline="\n") as fh:
fh.write(report)
print("wrote %s (%d bytes)" % (out_path, len(report)))


if __name__ == "__main__":
main()
Loading
Loading