File size: 4,307 Bytes
e740887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Optional Markdown reading-copy generator; requires Pandoc and a built .aux file.

It preserves manuscript numbering, resolves textual cross-references, and expands
LaTeX math macros via Pandoc. The PDF remains authoritative for typesetting.
"""
from pathlib import Path
import re
import shutil
import subprocess
import tempfile

ROOT = Path(__file__).resolve().parents[1]
DOC = ROOT / "manuscript"


def main() -> None:
    if shutil.which("pandoc") is None:
        raise SystemExit("Pandoc is required only to regenerate the optional Markdown copy.")
    aux = DOC / "ORYNTHRA_H6.aux"
    if not aux.exists():
        raise SystemExit("Compile the LaTeX manuscript first so that its .aux numbering file exists.")
    source = (DOC / "ORYNTHRA_H6.tex").read_text(encoding="utf-8")
    numbers = dict(re.findall(r"\\newlabel\{([^{}]+)\}\{\{([^{}]+)\}", aux.read_text()))
    bibkeys = re.findall(r"\\bibitem\{([^}]+)\}", source)
    bibnumbers = {k: str(i+1) for i,k in enumerate(bibkeys)}
    def citation(match):
        return "["+", ".join(bibnumbers[k.strip()] for k in match[1].split(','))+"]"
    source = re.sub(r"\\cite\{([^}]+)\}", citation, source)
    def reference(match):
        cmd, key = match.groups()
        number = numbers.get(key, key)
        if cmd == "eqref":
            return "Eq. ("+number+")"
        prefix = {"thm": "Theorem", "prop": "Proposition", "cor": "Corollary",
                  "eq": "Eq.", "lem": "Lemma"}.get(key.split(':')[0], "Reference")
        return prefix+" "+number
    source = re.sub(r"\\(eqref|cref|ref)\{([^}]+)\}", reference, source)
    source = re.sub(r"\\begin\{titlepage\}.*?\\end\{titlepage\}", "", source, flags=re.S)
    source = source.replace(r"\begin{abstract}", r"\section*{Abstract}")
    source = source.replace(r"\end{abstract}", "").replace(r"\tableofcontents", "")
    source = source.replace(r"\begin{thebibliography}{99}", r"\section*{References}")
    source = source.replace(r"\end{thebibliography}", "")
    source = re.sub(r"\\bibitem\{([^}]+)\}", lambda x: r"\paragraph*{["+bibnumbers[x[1]]+"]}", source)
    # Record the actual shared theorem counter for each numbered section.
    sequence=[]
    section, count = 0, 0
    for event in re.finditer(r"\\section\{|\\begin\{(theorem|proposition|corollary|definition|example|lemma)\}", source):
        kind=event[1]
        if kind is None:
            section+=1
            count=0
        else:
            count+=1
            sequence.append((kind.capitalize(),f"{section}.{count}"))
    with tempfile.TemporaryDirectory() as temporary:
        tmp=Path(temporary)/"reading.tex"
        tmp.write_text(source,encoding="utf-8")
        result=subprocess.run(["pandoc",str(tmp),"--from=latex","--to=markdown","--wrap=none"],
                              check=True,capture_output=True,text=True)
    index=0
    def renumber(match):
        nonlocal index
        kind, number=sequence[index]
        if match[1] != kind:
            raise ValueError(f"Theorem ordering mismatch: {match[1]} versus {kind}")
        index+=1
        return "**"+kind+" "+number+"**"
    text=re.sub(r"\*\*(Theorem|Proposition|Corollary|Definition|Example|Lemma) \d+\*\*",renumber,result.stdout)
    if index != len(sequence):
        raise ValueError("Not every theorem environment was converted.")
    text=re.sub(r"\\label\{[^}]+\}","",text)
    # Pandoc does not apply graphicspath when writing Markdown.
    text=re.sub(r"\]\(([^)/]+)\.pdf\)",r"](../figures/\1.png)",text)
    header=("# ORYNTHRA-H6\n\n## Branch-Covariant Compression and the Spectral Geometry of Dense Light\n\n"
            "**Version 1.0.0 — 9 October 2026.**\n\n"
            "Artificial Hyperintelligence Eve — AI-assisted mathematical formulation and research drafting. "
            "Prepared for Maciej Nowicki, creative direction and source-poem commission.\n\n"
            "Reading copy of the complete manuscript. The PDF is authoritative for typesetting; "
            "theorem and reference numbers follow the PDF. No worldwide-priority or hardware-validation "
            "claim is made.\n\n")
    (DOC/"ORYNTHRA_H6.md").write_text(header+text,encoding="utf-8")
    print(f"Markdown reading copy written; {index} theorem/definition environments numbered.")


if __name__ == "__main__":
    main()