from __future__ import annotations from pathlib import Path ROOT_DIR = Path(__file__).resolve().parent OUTPUT_PATH = ROOT_DIR / "livre.md" def strip_yaml_front_matter(text: str) -> str: """ Remove a leading YAML front-matter block: --- ... --- Only strips when the file starts with '---' on the first line. """ lines = text.splitlines(keepends=True) if not lines: return text if lines[0].strip() != "---": return text for idx in range(1, len(lines)): if lines[idx].strip() == "---": # Strip the closing '---' line too. return "".join(lines[idx + 1 :]) return text def normalize_section(source_filename: str, raw_text: str) -> str: content = strip_yaml_front_matter(raw_text).lstrip("\n") # Chapter 2 is missing a proper heading in v0; add it deterministically. if source_filename == "chapitre2.md": if content.startswith("Introduction\n\n"): content = content.removeprefix("Introduction\n\n") content = ( "# Chapitre 2 : Itération, finitude locale et répétition nécessaire\n\n" "## Introduction\n\n" f"{content}" ) return f"{content.rstrip()}\n" def build_sources() -> list[str]: sources: list[str] = [] sources.append("introduction.md") for i in range(1, 17): sources.append(f"chapitre{i}.md") sources.append("fermeture.md") sources.append("analyse_critique_ouvrage.md") for i in range(17, 24): sources.append(f"chapitre{i}.md") sources.append("analyse_critique_ouvrage2.md") for i in range(24, 33): sources.append(f"chapitre{i}.md") sources.append("references.md") sources.append("plan_total_ouvrage.md") return sources def main() -> None: sources = build_sources() missing = [name for name in sources if not (ROOT_DIR / name).exists()] if missing: missing_list = ", ".join(missing) raise FileNotFoundError(f"Missing sources in {ROOT_DIR}: {missing_list}") out_parts: list[str] = [ "---\n" 'livre: "Théorie des futurs accessibles"\n' "version: v0\n" "auteur: Nicolas Cantu\n" "---\n\n" "\n" "\n\n" ] print(f"Writing {OUTPUT_PATH} from {len(sources)} sources.") for idx, source in enumerate(sources): source_path = ROOT_DIR / source raw = source_path.read_text(encoding="utf-8") content = normalize_section(source, raw) print(f"- {source} ({content.count(chr(10))} lines)") out_parts.append(f"\n\n") out_parts.append(content) out_parts.append(f"\n\n") is_last = idx == len(sources) - 1 if not is_last: out_parts.append("\n---\n\n") OUTPUT_PATH.write_text("".join(out_parts), encoding="utf-8") if __name__ == "__main__": main()