From 04cf6bce032920d02c349fe82ca6bf6c35f04c11 Mon Sep 17 00:00:00 2001 From: Brandon Date: Fri, 4 Nov 2022 23:13:53 -0700 Subject: [PATCH] Add chapter navigation footer --- scripts/deploy-ebook-to-www | 5 ++- scripts/inject-chapter-navigation-footer | 43 ++++++++++++++++++++++++ www/css/web.css | 26 ++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100755 scripts/inject-chapter-navigation-footer diff --git a/scripts/deploy-ebook-to-www b/scripts/deploy-ebook-to-www index b725df93..c9eed45f 100755 --- a/scripts/deploy-ebook-to-www +++ b/scripts/deploy-ebook-to-www @@ -339,9 +339,12 @@ do workTitle=$(grep --only-matching --extended-regexp "(.+?)" "${workDir}"/src/epub/content.opf | sed --regexp-extended "s/<[^>]+?>//g") find "${workDir}"/src/epub \( -type d -name .git -prune \) -o -type f -name "*.xhtml" -print0 | xargs -0 sed --in-place --regexp-extended "s||<title>${workTitle} - |g" - # Add the nav to each page + # Add the header nav to each page find "${workDir}"/src/epub \( -type d -name .git -prune \) -o -type f -name "*.xhtml" -print0 | xargs -0 sed --in-place --regexp-extended "s%<body(.*?)>%<body\1><header><nav><ul><li><a href=\"/\">Standard Ebooks</a></li><li><a href=\"${bookUrl}\">Back to ebook</a></li><li><a href=\"${bookUrl}/text\">Table of contents</a></li></ul></nav></header>%" + # Add a chapter navigation footer to each page + "${scriptsDir}"/inject-chapter-navigation-footer "${workDir}"/src/epub "${bookUrl}" + # Adjust sponsored links in the colophon sed --in-place 's|<p><a href="http|<p><a rel="nofollow" href="http|g' "${workDir}"/src/epub/text/colophon.xhtml diff --git a/scripts/inject-chapter-navigation-footer b/scripts/inject-chapter-navigation-footer new file mode 100755 index 00000000..3981eda5 --- /dev/null +++ b/scripts/inject-chapter-navigation-footer @@ -0,0 +1,43 @@ +#!/usr/bin/php +<? +if(count($argv) < 3){ + print 'Expected usage: inject-chapter-navigation-footer <epubDirectory> <bookUrl>'; + exit(1); +} +$epubDirectory = $argv[1]; +$bookUrl = $argv[2]; + +// Parse the order of chapters from the table of contents. +$doc = new DOMDocument(); +libxml_use_internal_errors(true); // Suppress warnings from output. +$doc->loadHTMLFile($epubDirectory . '/toc.xhtml'); +libxml_clear_errors(); +$toc = $doc->getElementById('toc'); +if($toc){ + $chapterLinks = array(); + foreach($toc->getElementsByTagName('a') as $link){ + $obj = new stdClass(); + $obj->Path = $link->getAttribute('href'); + $obj->Name = $link->textContent; + + // Only include links pointing to actual files (e.g. skip href="text/chapter-7#panawes-story"). + if(file_exists($epubDirectory . '/' . $obj->Path . '.xhtml')){ + $chapterLinks[] = $obj; + } + } + + for($i = 0; $i < count($chapterLinks); $i++){ + $previousObj = $i > 0 ? $chapterLinks[$i - 1] : null; + $nextObj = $i < count($chapterLinks) - 1 ? $chapterLinks[$i + 1] : null; + + $fileName = $epubDirectory . '/' . $chapterLinks[$i]->Path . '.xhtml'; + $chapter = file_get_contents($fileName); + // Inject the navigation footer. + $previousLink = $previousObj ? sprintf('<a href="%s"><em>Previous:</em> %s</a>', $bookUrl . '/' . $previousObj->Path, htmlspecialchars($previousObj->Name)) : ''; + $nextLink = $nextObj ? sprintf('<a href="%s"><em>Next:</em> %s</a>', $bookUrl . '/' . $nextObj->Path, htmlspecialchars($nextObj->Name)) : ''; + $footer = sprintf('<footer><ul><li>%s</li><li>%s</li></ul></footer>', $previousLink, $nextLink); + $chapter = str_replace('</body>', $footer . '</body>', $chapter); + file_put_contents($fileName, $chapter); + } +} +?> diff --git a/www/css/web.css b/www/css/web.css index 670aca0d..422ee3c8 100644 --- a/www/css/web.css +++ b/www/css/web.css @@ -1,5 +1,9 @@ @namespace epub "http://www.idpf.org/2007/ops"; +html{ + position: relative; +} + body{ font-family: "Georgia", serif; font-size: 18px; @@ -51,6 +55,28 @@ body > header li:first-child > a:hover{ transform: scale(1.025) rotate(1deg); } +body > footer{ + position: absolute; + bottom: 0; + left: 0; + width: 100%; +} + +body > footer ul{ + display: flex; + justify-content: space-between; + margin: 0; + padding: 0.5em 1em; + list-style: none; +} + +body > footer li{ + max-width: 40%; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + body > section[epub|type~="titlepage"], body > section[epub|type~="halftitlepage"]{ min-height: auto;