lordspline's picture
Publish Revision 2 dataset and TasteCore plan
47a747f verified
Raw
History Blame Contribute Delete
7.21 kB
#!/usr/bin/env python3
from __future__ import annotations
import html
import re
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SOURCE = ROOT / "paper.md"
ARCHITECTURE_FIGURE = r"""
\begin{figure}[!htbp]
\centering
\includegraphics[width=\linewidth]{assets/architecture.pdf}
\caption{Canvas-4B system architecture. A Qwen3.5-4B-class native
multimodal backbone receives a mode-routed visual representation. A learned
global compressor preserves page structure while protected high-resolution
regions retain typography and small controls. Shared training builds the
common visual--code substrate; lightweight mode adapters separate literal
reconstruction from redesign and open-ended generation. Render feedback is
used during training and optional quality-mode inference, while the public
response remains a single HTML document.}
\label{fig:architecture}
\end{figure}
"""
DATA_FIGURE = r"""
\begin{figure}[!htbp]
\centering
\includegraphics[width=\linewidth]{assets/data-flow.pdf}
\caption{Experiment-first, provenance-preserving data engine. Public research
corpora, current front ends, curated design galleries, and controlled
synthesis enter through deterministic build-and-render workers, multi-view
deduplication, and executable quality gates. The same canonical page can yield
reconstruction, redesign, prompt-generation, preference-distilled, and
visual-repair records. Human designers calibrate the uncertain boundary
rather than label the bulk corpus.}
\label{fig:data-flow}
\end{figure}
"""
# One entry per Markdown table, in document order. Widths sum to at most 0.94
# to leave room for inter-column padding.
TABLE_WIDTHS = [
[0.12, 0.24, 0.27, 0.27],
[0.13, 0.20, 0.17, 0.18, 0.22],
[0.20, 0.09, 0.11, 0.15, 0.25, 0.10],
[0.18, 0.21, 0.23, 0.28],
[0.15, 0.17, 0.17, 0.18, 0.23],
[0.19, 0.16, 0.23, 0.32],
[0.18, 0.22, 0.23, 0.27],
[0.13, 0.18, 0.19, 0.19, 0.21],
[0.16, 0.22, 0.22, 0.30],
[0.22, 0.13, 0.16, 0.17, 0.22],
[0.16, 0.21, 0.17, 0.18, 0.18],
[0.20, 0.12, 0.28, 0.30],
[0.20, 0.16, 0.16, 0.16, 0.22],
[0.18, 0.18, 0.18, 0.18, 0.18],
[0.19, 0.30, 0.18, 0.23],
[0.22, 0.11, 0.11, 0.11, 0.35],
[0.25, 0.12, 0.13, 0.40],
[0.16, 0.18, 0.18, 0.18, 0.20],
[0.20, 0.30, 0.40],
[0.16, 0.18, 0.18, 0.18, 0.20],
[0.18, 0.24, 0.20, 0.28],
[0.24, 0.14, 0.18, 0.34],
[0.24, 0.20, 0.20, 0.26],
]
def clean_markdown(text: str) -> str:
text = html.unescape(text)
text = re.sub(
r'<figure class="figure-wide">\s*<img[^>]*architecture\.svg[^>]*>'
r"\s*<figcaption>.*?</figcaption>\s*</figure>",
"\nCAPYARCHFIGUREMARKER\n",
text,
flags=re.S,
)
text = re.sub(
r'<figure class="figure-wide">\s*<img[^>]*data-flow\.svg[^>]*>'
r"\s*<figcaption>.*?</figcaption>\s*</figure>",
"\nCAPYDATAFIGUREMARKER\n",
text,
flags=re.S,
)
text = re.sub(r'<div class="page-break"></div>', "", text)
text = re.sub(
r'<div class="small">(.*?)</div>',
lambda match: "\n> " + re.sub(r"<[^>]+>", "", match.group(1)).strip() + "\n",
text,
flags=re.S,
)
text = re.sub(r"^##\s+\d+\.\s+", "# ", text, flags=re.M)
text = re.sub(r"^###\s+\d+\.\d+\s+", "## ", text, flags=re.M)
text = re.sub(r"^####\s+\d+\.\d+\.\d+\s+", "### ", text, flags=re.M)
text = re.sub(r"^##\s+Appendix\s+[A-Z]\.\s+", "# ", text, flags=re.M)
return text.strip() + "\n"
def pandoc(source: Path, destination: Path) -> None:
subprocess.run(
[
"pandoc",
str(source),
"--from=markdown+pipe_tables+fenced_code_blocks+raw_html+raw_tex+citations",
"--to=latex",
"--top-level-division=section",
"--listings",
"--natbib",
"--wrap=none",
"-o",
str(destination),
],
check=True,
)
def column_spec(widths: list[float]) -> str:
return "@{}" + "".join(
rf">{{\raggedright\arraybackslash}}p{{{width:.3f}\linewidth}}"
for width in widths
) + "@{}"
def format_tables(text: str) -> str:
pattern = re.compile(
r"\\begin\{longtable\}\[\]\{.*?@\{\}\}",
re.S,
)
matches = list(pattern.finditer(text))
if len(matches) != len(TABLE_WIDTHS):
raise RuntimeError(
f"expected {len(TABLE_WIDTHS)} tables, found {len(matches)}"
)
pieces: list[str] = []
cursor = 0
for match, widths in zip(matches, TABLE_WIDTHS, strict=True):
pieces.append(text[cursor : match.start()])
pieces.append("\\begin{longtable}[]{" + column_spec(widths) + "}")
cursor = match.end()
pieces.append(text[cursor:])
return "".join(pieces)
def normalize_longtable_headers(text: str) -> str:
pattern = re.compile(
r"(\\toprule\\noalign\{\}\n.*?\\midrule\\noalign\{\}\n)"
r"\\endhead",
re.S,
)
def replace(match: re.Match[str]) -> str:
header = match.group(1).replace(
r"\toprule\noalign{}" + "\n",
r"\toprule\noalign{}" + "\n" + r"\rowcolor{capywash}" + "\n",
1,
)
return header + "\\endfirsthead\n" + header + "\\endhead"
return pattern.sub(replace, text)
def normalize_symbols(text: str) -> str:
replacements = {
"≤": r"\(\leq\)",
"≥": r"\(\geq\)",
"×": r"\(\times\)",
"±": r"\(\pm\)",
"α": r"\(\alpha\)",
"κ": r"\(\kappa\)",
"τ": r"\(\tau\)",
"ρ": r"\(\rho\)",
"→": r"\(\rightarrow\)",
"–": "--",
"—": "---",
"≈": r"\(\approx\)",
}
for source, target in replacements.items():
text = text.replace(source, target)
return text
def prepare() -> None:
source = SOURCE.read_text()
body_start = source.index("## 1. Scope and recommendation")
references_start = source.index("## References")
appendix_start = source.index("## Appendix A.")
body_markdown = clean_markdown(source[body_start:references_start])
appendix_markdown = clean_markdown(source[appendix_start:])
body_md = ROOT / "body.md"
appendix_md = ROOT / "appendix.md"
body_md.write_text(body_markdown)
appendix_md.write_text(appendix_markdown)
pandoc(body_md, ROOT / "body.raw.tex")
pandoc(appendix_md, ROOT / "appendix.raw.tex")
body = (ROOT / "body.raw.tex").read_text()
body = body.replace("CAPYARCHFIGUREMARKER", ARCHITECTURE_FIGURE)
body = body.replace("CAPYDATAFIGUREMARKER", DATA_FIGURE)
body = format_tables(body)
body = normalize_longtable_headers(body)
body = normalize_symbols(body)
section_two = body.index("\\hypertarget{literature-review-2025}")
(ROOT / "introduction.tex").write_text(body[:section_two])
(ROOT / "body.tex").write_text(body[section_two:])
appendix = (ROOT / "appendix.raw.tex").read_text()
appendix = normalize_longtable_headers(appendix)
appendix = normalize_symbols(appendix)
(ROOT / "appendix.tex").write_text(appendix)
if __name__ == "__main__":
prepare()