File size: 3,014 Bytes
d1c6c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
"""Compile the editable LaTeX source and verify references and page count.

Requires pdflatex and bibtex (bibtex8/bibtexu accepted as fallbacks), plus the
packages named in main.tex. TeX Live or MiKTeX can supply them. No network is
used by this script, though a TeX installation may offer to install packages.
"""
from __future__ import annotations
import json
import re
import shutil
import subprocess
from pathlib import Path

ROOT=Path(__file__).resolve().parents[1]
OUTPUT='Aelyrion_Noulith_III_Research_Manuscript_v3.0.0.pdf'


def main() -> None:
    tex=shutil.which('pdflatex')
    bib=next((path for name in ('bibtex','bibtex8','bibtexu') if (path:=shutil.which(name))),None)
    if not tex or not bib:
        raise SystemExit('Install TeX Live or MiKTeX with pdflatex and bibtex (or bibtex8), then retry.')
    if not (ROOT/'manuscript/validation_summary.tex').exists():
        raise SystemExit('Run scripts/run_validation.py before building the manuscript.')
    cwd=ROOT/'manuscript'; logs=[]
    commands=[[tex,'-interaction=nonstopmode','-halt-on-error','main.tex'],[bib,'main']]
    commands += [[tex,'-interaction=nonstopmode','-halt-on-error','main.tex'] for _ in range(3)]
    for command in commands:
        result=subprocess.run(command,cwd=cwd,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        logs.append('$ '+' '.join(command)+'\n'+result.stdout)
        (ROOT/'validation/latex_build.txt').write_text('\n'.join(logs),encoding='utf-8')
        if result.returncode:
            raise SystemExit('LaTeX build failed. See validation/latex_build.txt.')
    log=(cwd/'main.log').read_text(errors='replace')
    fatal_patterns=['There were undefined references','There were undefined citations','LaTeX Warning: Label(s) may have changed']
    if any(pattern in log for pattern in fatal_patterns):
        raise SystemExit('Unresolved LaTeX cross-references or citations; inspect manuscript/main.log.')
    matches=re.findall(r'Output written on main\.pdf \((\d+) pages?',log)
    if not matches:
        raise SystemExit('Could not determine actual PDF page count from the successful TeX log.')
    pages=int(matches[-1])
    if pages<51:
        raise SystemExit(f'PDF is only {pages} pages; the release requires more than 50.')
    overfull=re.findall(r'^Overfull.*$',log,re.M)
    if overfull:
        raise SystemExit('Overfull TeX boxes detected; correct layout before releasing.')
    shutil.copy2(cwd/'main.pdf',ROOT/OUTPUT)
    warnings=re.findall(r'^(?:LaTeX|Package .*?) Warning:.*$',log,re.M)
    report={'pdf':OUTPUT,'pages':pages,'unresolved_references':False,'overfull_boxes':len(overfull),
        'other_warnings':warnings,'pdf_visual_review':'Recorded separately after rendering; compilation alone is not visual review.'}
    (ROOT/'validation/build_report.json').write_text(json.dumps(report,indent=2)+'\n')
    print(f'Built {OUTPUT}: {pages} pages, no unresolved references or overfull boxes.')

if __name__=='__main__':
    main()