mirror of
https://github.com/standardebooks/web.git
synced 2025-07-16 11:26:37 -04:00
Update navigation footer script in Python
This commit is contained in:
parent
bd72edc248
commit
fcda045958
2 changed files with 50 additions and 40 deletions
|
@ -354,7 +354,7 @@ do
|
||||||
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>|"
|
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
|
# Add a chapter navigation footer to each page
|
||||||
"${scriptsDir}"/inject-chapter-navigation-footer "${workDir}"/src/epub "${bookUrl}"
|
"${scriptsDir}"/inject-chapter-navigation-footer "${workDir}" "${bookUrl}"
|
||||||
|
|
||||||
# Adjust sponsored links in the colophon
|
# 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
|
sed --in-place 's|<p><a href="http|<p><a rel="nofollow" href="http|g' "${workDir}"/src/epub/text/colophon.xhtml
|
||||||
|
|
88
scripts/inject-chapter-navigation-footer
Executable file → Normal file
88
scripts/inject-chapter-navigation-footer
Executable file → Normal file
|
@ -1,43 +1,53 @@
|
||||||
#!/usr/bin/php
|
#!/usr/bin/env python3
|
||||||
<?
|
|
||||||
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.
|
import argparse
|
||||||
$doc = new DOMDocument();
|
import os
|
||||||
libxml_use_internal_errors(true); // Suppress warnings from output.
|
import html
|
||||||
$doc->loadHTMLFile($epubDirectory . '/toc.xhtml');
|
import se
|
||||||
libxml_clear_errors();
|
import se.se_epub
|
||||||
$toc = $doc->getElementById('toc');
|
import se.easy_xml
|
||||||
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").
|
# Require arguments
|
||||||
if(file_exists($epubDirectory . '/' . $obj->Path . '.xhtml')){
|
parser = argparse.ArgumentParser(description='A script to inject the navigation footer with links in the web version of a Standard Ebook')
|
||||||
$chapterLinks[] = $obj;
|
parser.add_argument('ebook_dir', type=str, help='Path to the main directory of the ebook') # No trailing "/" on this path
|
||||||
}
|
parser.add_argument('book_url', type=str, help='The ebook URL') # No trailing "/" on this link
|
||||||
}
|
args = parser.parse_args()
|
||||||
|
|
||||||
for($i = 0; $i < count($chapterLinks); $i++){
|
ebook_dir = args.ebook_dir
|
||||||
$previousObj = $i > 0 ? $chapterLinks[$i - 1] : null;
|
book_url = args.book_url
|
||||||
$nextObj = $i < count($chapterLinks) - 1 ? $chapterLinks[$i + 1] : null;
|
|
||||||
|
|
||||||
$fileName = $epubDirectory . '/' . $chapterLinks[$i]->Path . '.xhtml';
|
# Make an instance of the SeEpub class so we can use helper functions to easily get spine
|
||||||
$chapter = file_get_contents($fileName);
|
se_epub = se.se_epub.SeEpub(ebook_dir)
|
||||||
// Inject the navigation footer.
|
|
||||||
$previousLink = $previousObj ? sprintf('<a href="%s" rel="prev"><i>Previous:</i> %s</a>', $bookUrl . '/' . $previousObj->Path, htmlspecialchars($previousObj->Name)) : '';
|
# Get spine paths
|
||||||
$nextLink = $nextObj ? sprintf('<a href="%s" rel="next"><i>Next:</i> %s</a>', $bookUrl . '/' . $nextObj->Path, htmlspecialchars($nextObj->Name)) : '';
|
spine_paths = se_epub.spine_file_paths
|
||||||
$footer = sprintf('<footer><ul><li>%s</li><li>%s</li></ul></footer>', $previousLink, $nextLink);
|
|
||||||
$chapter = str_replace('</body>', $footer . '</body>', $chapter);
|
# Make sure spine paths are of the form "text/chapter-1.xhtml", with no leading "/"
|
||||||
file_put_contents($fileName, $chapter);
|
rel_spine_paths = [
|
||||||
}
|
'/'.join(str(path).split('/')[str(path).split('/').index('text'):]).lstrip('/') for path in spine_paths
|
||||||
}
|
]
|
||||||
?>
|
|
||||||
|
# Get titles from spine
|
||||||
|
spine_titles = [se.easy_xml.EasyXmlTree(open(file_path).read()).xpath("//head/title/text()", True) for file_path in spine_paths]
|
||||||
|
|
||||||
|
# Make list of dicts containing title and relative path for each file
|
||||||
|
chapter_links = [{"title": title, "path": path} for title, path in zip(spine_titles, rel_spine_paths)]
|
||||||
|
|
||||||
|
# Create footers
|
||||||
|
for i, chapter_link in enumerate(chapter_links):
|
||||||
|
previousObj = chapter_links[i - 1] if i > 0 else None
|
||||||
|
nextObj = chapter_links[i + 1] if i < len(chapter_links) - 1 else None
|
||||||
|
|
||||||
|
fileName = os.path.join(ebook_dir, "src/epub", chapter_link["path"])
|
||||||
|
|
||||||
|
with open(fileName, 'r') as file:
|
||||||
|
chapter = file.read()
|
||||||
|
|
||||||
|
previousLink = f'<a href="{book_url}/{previousObj["path"]}" rel="prev"><i>Previous:</i> {html.escape(previousObj["title"])}</a>' if previousObj else ''
|
||||||
|
nextLink = f'<a href="{book_url}/{nextObj["path"]}" rel="next"><i>Next:</i> {html.escape(nextObj["title"])}</a>' if nextObj else ''
|
||||||
|
|
||||||
|
footer = f'<footer><ul><li>{previousLink}</li><li>{nextLink}</li></ul></footer>'
|
||||||
|
chapter = chapter.replace('</body>', f'{footer}</body>')
|
||||||
|
|
||||||
|
with open(fileName, 'w') as file:
|
||||||
|
file.write(chapter)
|
Loading…
Add table
Add a link
Reference in a new issue