Fix EPUB margin page turns
This commit is contained in:
parent
7073d70540
commit
17a1eb9ddc
5 changed files with 72 additions and 9 deletions
Binary file not shown.
|
|
@ -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"<strong>[^<]+</strong>", rendered) else ""
|
||||
paragraphs.append(f"<p{css_class}>{rendered}</p>")
|
||||
header = ""
|
||||
if page.page_index == 1:
|
||||
header = f'<header><p class="movement">{html.escape(page.movement)}</p><h1>{html.escape(page.title)}</h1></header>'
|
||||
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'<item id="chapter-{i}" href="text/chapter-{i:02}.xhtml" media-type="application/xhtml+xml"/>'
|
||||
for i in range(1, len(chapters) + 1)
|
||||
f'<item id="{page.item_id}" href="{page.href}" media-type="application/xhtml+xml"/>'
|
||||
for page in pages
|
||||
)
|
||||
spine = "\n".join(f'<itemref idref="chapter-{i}"/>' for i in range(1, len(chapters) + 1))
|
||||
spine = "\n".join(f'<itemref idref="{page.item_id}"/>' for page in pages)
|
||||
nav_points = "\n".join(
|
||||
f'<li><a href="text/chapter-{i:02}.xhtml">{html.escape(chapter.title)}</a></li>'
|
||||
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))
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export default (() => {
|
|||
const AetheriaEpubReader: QuartzComponent = () => (
|
||||
<section
|
||||
class="aetheria-epub-reader"
|
||||
data-epub-src="/static/fiction/the-burden-of-proof.epub"
|
||||
data-epub-src="/static/fiction/the-burden-of-proof.epub?v=reader-pages-2"
|
||||
aria-label="The Burden of Proof ebook reader"
|
||||
>
|
||||
<div class="aetheria-reader-heading">
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
|||
})
|
||||
}
|
||||
|
||||
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())
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue