Skip to content

Commit d7c152e

Browse files
committed
Overhaul for iBooks compat
1 parent 639a327 commit d7c152e

11 files changed

+347
-1500
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
cache/*.htm
22
cache/*.html
33
node_modules/
4+
temp/*

build.js

+38-36
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
#!/usr/bin/env nodejs
1+
#!/usr/bin/env node
22

33
const cheerio = require('cheerio');
4-
const Streampub = require('streampub');
4+
const nodepub = require('nodepub');
55
const jetpack = require('fs-jetpack');
66
const { execSync } = require('child_process');
7+
const archiver = require('archiver');
78

8-
var version = process.argv.length > 2 ? process.argv[2] : 'default';
9+
let version = process.argv.length > 2 ? process.argv[2] : 'default';
910

1011
const config = JSON.parse(jetpack.read('meta/' + version + '.json'));
1112
config.metadata = Object.assign({
@@ -20,27 +21,20 @@ config.metadata = Object.assign({
2021
includeTOC: false
2122
}, config.metadata);
2223

23-
var scrapeError = false;
24+
let scrapeError = false;
2425

25-
var epub = new Streampub(config.metadata);
26-
var epubPath = `output/${version}.epub`
27-
epub.pipe(jetpack.createWriteStream(epubPath));
28-
29-
let cover = cheerio.load(jetpack.read('templates/cover.xhtml'));
30-
cover('h1#title').text(config.metadata.title);
31-
cover('figure#cover').append(`<img alt="Cover" src="${config.img}" />`);
32-
cover('span#author').text(config.metadata.author);
33-
epub.write(Streampub.newChapter(config.metadata.title, cover.html(), 0));
34-
35-
let toc = cheerio.load(jetpack.read('templates/nav.xhtml'));
36-
37-
var chapterNumber = 2;
26+
let epub = nodepub.document(config.metadata, config.img, writeTOC);
3827

3928
function addChapterToBook(html, url, cache_path){
4029
let $ = cheerio.load(html);
4130
let title = $(config.titleSelector).first().text();
42-
let content = $(config.contentSelector).html();
31+
console.log('Adding "' + title + '" to the book.')
4332
if(config.withoutSelector) $(config.withoutSelector).remove();
33+
$('br').replaceWith('\n');
34+
$('img').insertAfter('</img>');
35+
$('hr').insertAfter('</hr>');
36+
$('[async]').removeAttr('async')
37+
let content = $(config.contentSelector);
4438
let path = url;
4539
if(typeof url === 'object'){
4640
path = url.url;
@@ -53,12 +47,25 @@ function addChapterToBook(html, url, cache_path){
5347
scrapeError = true;
5448
}
5549
let safe_title = title.toLowerCase().replace(/ /g, '-');
56-
let newDoc = cheerio.load(jetpack.read('templates/chapter.xhtml'));
57-
newDoc('#chapter')
58-
.append('<h1>'+title+'</h1>')
59-
.append(content);
60-
epub.write(Streampub.newChapter(title, newDoc('body').html(), chapterNumber));
61-
toc('#list').append(`<li><a href="chapter-${chapterNumber++}.xhtml">${title}</a></li>`);
50+
let newDoc = `
51+
<h1 style="margin: 1rem auto;">${title}</h1>
52+
<div style="">
53+
${content.html()}
54+
</div>
55+
`;
56+
epub.addSection(title, newDoc);
57+
}
58+
59+
function writeTOC(links){
60+
let toc = cheerio.load(jetpack.read('templates/nav.xhtml'));
61+
let list = toc('#list');
62+
links.forEach(link => {
63+
if (link.itemType !== "contents") {
64+
list.append(`\n <li><a href="${link.link}">${link.title}</a></li>`);
65+
}
66+
});
67+
list.append('\n');
68+
return toc('body').html();
6269
}
6370

6471
config.urls.forEach(url => {
@@ -76,15 +83,10 @@ config.urls.forEach(url => {
7683
addChapterToBook(jetpack.read(cache_path), url, cache_path);
7784
});
7885

79-
epub.write(Streampub.newChapter("Table of Contents", toc.html(), 1));
80-
81-
epub.write(Streampub.newFile('style/blitz.css', jetpack.createReadStream('style/blitz.css')));
82-
epub.write(Streampub.newFile('style/blitz-kindle.css', jetpack.createReadStream('style/blitz-kindle.css')));
83-
epub.write(Streampub.newFile(config.img, jetpack.createReadStream(config.img)));
84-
85-
if(scrapeError){
86-
console.error('Scrape errors occurred: No epub produced.');
87-
} else {
88-
epub.end();
89-
console.log(`Book successfully written to ${epubPath}`);
90-
}
86+
const archive = archiver('zip', { store: false });
87+
const output = jetpack.createWriteStream(`${__dirname}/output/${version}.epub`);
88+
archive.pipe(output);
89+
epub.writeFilesForEPUB('./temp', err => { if (err) { console.log(err) } });
90+
archive.directory('./temp/', false);
91+
output.on('close', () => console.log(archive.pointer() + ' total bytes'));
92+
archive.finalize();

0 commit comments

Comments
 (0)