diff --git a/Aetheria/static/fiction/the-burden-of-proof.epub b/Aetheria/static/fiction/the-burden-of-proof.epub index 7e3e60d..d4c775a 100644 Binary files a/Aetheria/static/fiction/the-burden-of-proof.epub and b/Aetheria/static/fiction/the-burden-of-proof.epub differ diff --git a/scripts/fiction/build_the_burden_of_proof_epub.py b/scripts/fiction/build_the_burden_of_proof_epub.py index cdcb163..3635174 100644 --- a/scripts/fiction/build_the_burden_of_proof_epub.py +++ b/scripts/fiction/build_the_burden_of_proof_epub.py @@ -28,6 +28,27 @@ class Chapter: blocks: list[str] +@dataclass +class SpinePage: + chapter_index: int + page_index: int + title: str + movement: str + blocks: list[str] + + @property + def href(self) -> str: + if self.page_index == 1: + return f"text/chapter-{self.chapter_index:02}.xhtml" + return f"text/chapter-{self.chapter_index:02}-p{self.page_index:02}.xhtml" + + @property + def item_id(self) -> str: + if self.page_index == 1: + return f"chapter-{self.chapter_index}" + return f"chapter-{self.chapter_index}-p{self.page_index}" + + def inline_markup(text: str) -> str: placeholders: dict[str, str] = {} @@ -92,6 +113,40 @@ def chapter_xhtml(chapter: Chapter) -> str: return xhtml_page(chapter.title, body, "chapter") +def paginate_chapters(chapters: list[Chapter], target_chars: int = 1400) -> list[SpinePage]: + pages: list[SpinePage] = [] + for chapter_index, chapter in enumerate(chapters, 1): + chunks: list[list[str]] = [] + current: list[str] = [] + current_chars = 0 + for block in chapter.blocks: + block_chars = len(block) + if current and current_chars + block_chars > target_chars: + chunks.append(current) + current = [] + current_chars = 0 + current.append(block) + current_chars += block_chars + if current: + chunks.append(current) + for page_index, blocks in enumerate(chunks, 1): + pages.append(SpinePage(chapter_index, page_index, chapter.title, chapter.movement, blocks)) + return pages + + +def page_xhtml(page: SpinePage) -> str: + paragraphs = [] + for block in page.blocks: + rendered = inline_markup(block) + css_class = " class=\"pov\"" if re.fullmatch(r"[^<]+", rendered) else "" + paragraphs.append(f"{rendered}

") + header = "" + if page.page_index == 1: + header = f'

{html.escape(page.movement)}

{html.escape(page.title)}

' + body = header + "\n".join(paragraphs) + return xhtml_page(page.title, body, "chapter") + + def modified_timestamp(source: Path) -> str: epoch = int(os.environ.get("SOURCE_DATE_EPOCH", source.stat().st_mtime)) return datetime.fromtimestamp(epoch, timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") @@ -99,12 +154,13 @@ def modified_timestamp(source: Path) -> str: def build(source: Path, output: Path) -> None: title, chapters = parse_manuscript(source) + pages = paginate_chapters(chapters) output.parent.mkdir(parents=True, exist_ok=True) chapter_items = "\n".join( - f'' - for i in range(1, len(chapters) + 1) + f'' + for page in pages ) - spine = "\n".join(f'' for i in range(1, len(chapters) + 1)) + spine = "\n".join(f'' for page in pages) nav_points = "\n".join( f'
  • {html.escape(chapter.title)}
  • ' for i, chapter in enumerate(chapters, 1) @@ -135,8 +191,8 @@ def build(source: Path, output: Path) -> None: archive.writestr("EPUB/toc.ncx", ncx) archive.writestr("EPUB/styles/book.css", css) archive.writestr("EPUB/text/title.xhtml", title_page) - for i, chapter in enumerate(chapters, 1): - archive.writestr(f"EPUB/text/chapter-{i:02}.xhtml", chapter_xhtml(chapter)) + for page in pages: + archive.writestr(f"EPUB/{page.href}", page_xhtml(page)) validate(output, len(chapters)) diff --git a/site/quartz/components/AetheriaEpubReader.tsx b/site/quartz/components/AetheriaEpubReader.tsx index d9cae73..3fd76b2 100644 --- a/site/quartz/components/AetheriaEpubReader.tsx +++ b/site/quartz/components/AetheriaEpubReader.tsx @@ -7,7 +7,7 @@ export default (() => { const AetheriaEpubReader: QuartzComponent = () => (
    diff --git a/site/quartz/components/scripts/aetheriaEpubReader.inline.ts b/site/quartz/components/scripts/aetheriaEpubReader.inline.ts index 0974dc4..6c31a88 100644 --- a/site/quartz/components/scripts/aetheriaEpubReader.inline.ts +++ b/site/quartz/components/scripts/aetheriaEpubReader.inline.ts @@ -17,7 +17,7 @@ declare global { } const cookieName = "aetheria_tbp_reader" -const settingsVersion = 2 +const settingsVersion = 3 const defaults: ReaderSettings = { version: settingsVersion, theme: "dark", @@ -35,7 +35,7 @@ function readSettings(): ReaderSettings { return { ...defaults, version: settingsVersion, - cfi: typeof saved.cfi === "string" ? saved.cfi : undefined, + cfi: saved.version === settingsVersion && typeof saved.cfi === "string" ? saved.cfi : undefined, theme: ["dark", "light", "sepia"].includes(saved.theme) ? saved.theme : defaults.theme, font: ["serif", "sans"].includes(saved.font) ? saved.font : defaults.font, fontSize: Number.isFinite(saved.fontSize) ? Math.min(150, Math.max(80, saved.fontSize)) : defaults.fontSize, @@ -63,6 +63,10 @@ function loadScript(src: string, ready: () => boolean): Promise { }) } +function chapterHrefFor(locationHref: string): string { + return locationHref.replace(/chapter-(\d+)-p\d+\.xhtml$/, "chapter-$1.xhtml") +} + async function initReader(root: HTMLElement) { if (root.dataset.readerReady === "true") return root.dataset.readerReady = "true" @@ -146,7 +150,7 @@ async function initReader(root: HTMLElement) { saveSettings(settings) const percentage = book.locations?.length() ? book.locations.percentageFromCfi(settings.cfi) : null progress.textContent = percentage == null ? "Position saved" : `${Math.round(percentage * 100)}% - position saved` - if (location.start.href) chapterSelect.value = location.start.href + if (location.start.href) chapterSelect.value = chapterHrefFor(location.start.href) }) const mutationObserver = new MutationObserver(() => clampRenditionDom()) diff --git a/site/quartz/components/styles/aetheriaEpubReader.scss b/site/quartz/components/styles/aetheriaEpubReader.scss index 8116ac1..9130915 100644 --- a/site/quartz/components/styles/aetheriaEpubReader.scss +++ b/site/quartz/components/styles/aetheriaEpubReader.scss @@ -7,12 +7,15 @@ body[data-slug="Fiction/The-Burden-of-Proof"] { } .center { + min-width: 0; max-width: min(72rem, calc(100vw - 2rem)); } } .aetheria-epub-reader { margin: 1.5rem 0 2.5rem; + min-width: 0; + max-width: 100%; border: 1px solid #2b3946; border-radius: 6px; overflow: hidden;