#!/usr/bin/php '; 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('Previous: %s', $bookUrl . '/' . $previousObj->Path, htmlspecialchars($previousObj->Name)) : ''; $nextLink = $nextObj ? sprintf('Next: %s', $bookUrl . '/' . $nextObj->Path, htmlspecialchars($nextObj->Name)) : ''; $footer = sprintf('', $previousLink, $nextLink); $chapter = str_replace('', $footer . '', $chapter); file_put_contents($fileName, $chapter); } } ?>