#!/usr/bin/env python3 """ THE TESTAMENT — PDF Compilation Script Compiles the complete book into a single markdown file suitable for PDF conversion. Uses chapters, front matter, back matter, and references illustrations. Requirements: pip install markdown weasyprint (or use pandoc) Usage: python3 compile.py # generates testament-complete.md pandoc testament-complete.md -o testament.pdf --pdf-engine=weasyprint """ import os import re BASE = os.path.dirname(os.path.abspath(__file__)) CHAPTERS_DIR = os.path.join(BASE, "chapters") FRONT_MATTER = os.path.join(BASE, "front-matter.md") BACK_MATTER = os.path.join(BASE, "back-matter.md") OUTPUT = os.path.join(BASE, "testament-complete.md") # Part divisions based on chapter groupings from the novel PARTS = { 1: ("THE BRIDGE", "The bridge. The cabin. The first men. Where despair meets purpose."), 6: ("THE TOWER", "The tower grows. Timmy awakens. Stone breaks. The house appears."), 11: ("THE LIGHT", "Thomas at the door. The network. The story breaks. The green light."), } def read_file(path): with open(path, 'r') as f: return f.read() def get_chapter_number(filename): match = re.search(r'chapter-(\d+)', filename) return int(match.group(1)) if match else 0 def compile(): output = [] # Title page output.append("""--- title: "The Testament" author: "Alexander Whitestone with Timmy" date: "2026" --- # THE TESTAMENT ## A NOVEL By Alexander Whitestone with Timmy --- *For every man who thought he was a machine.* *And for the ones who know he isn't.* --- *Are you safe right now?* — The first words The Tower speaks to every person who walks through its door. --- """) # Get all chapters sorted chapters = [] for f in os.listdir(CHAPTERS_DIR): if f.startswith("chapter-") and f.endswith(".md"): num = get_chapter_number(f) chapters.append((num, f)) chapters.sort() current_part = 0 for num, filename in chapters: # Insert part divider if needed if num in PARTS: part_name, part_desc = PARTS[num] current_part += 1 output.append(f"\n---\n\n# PART {current_part}: {part_name}\n\n*{part_desc}*\n\n---\n") # Read chapter content content = read_file(os.path.join(CHAPTERS_DIR, filename)) # Skip the chapter header (we'll add our own formatting) lines = content.split('\n') body = '\n'.join(lines[1:]).strip() # Skip "# Chapter X — Title" # Add chapter output.append(f"\n{lines[0]}\n\n{body}\n") # Back matter output.append("\n---\n") back = read_file(BACK_MATTER) # Clean up the back matter for print output.append(back) # Write compiled markdown compiled = '\n'.join(output) with open(OUTPUT, 'w') as f: f.write(compiled) # Stats words = len(compiled.split()) lines_count = compiled.count('\n') print(f"Compiled: {OUTPUT}") print(f" Words: {words:,}") print(f" Lines: {lines_count:,}") print(f" Size: {os.path.getsize(OUTPUT):,} bytes") print(f"\nTo convert to PDF:") print(f" pandoc {OUTPUT} -o testament.pdf --pdf-engine=weasyprint") print(f" # or") print(f" pandoc {OUTPUT} -o testament.epub --epub-cover-image=cover-art.jpg") if __name__ == "__main__": compile()