diff --git a/lib/extras.js b/lib/extras.js index ded78f4..9d06e9d 100644 --- a/lib/extras.js +++ b/lib/extras.js @@ -8,8 +8,8 @@ const BOOK_ID = "urn:uuid:e7f3532d-8db6-4888-be80-1976166b7059"; const NCX_FILENAME = "toc.ncx"; -module.exports = function (contentPath, chaptersPath) { - return getChapters(contentPath, chaptersPath).then(function (chapters) { +module.exports = function (contentPath, chaptersPath, manifestPath) { + return getChapters(contentPath, chaptersPath, manifestPath).then(function (chapters) { return Promise.all([ writeOpf(chapters, contentPath), writeNcx(chapters, contentPath) @@ -83,20 +83,24 @@ ${navPoints} return fs.writeFile(path.resolve(contentPath, NCX_FILENAME), contents); } -function getChapters(contentPath, chaptersPath) { +function getChapters(contentPath, chaptersPath, manifestPath) { const hrefPrefix = `${path.relative(contentPath, chaptersPath)}/`; - return fs.readdir(chaptersPath).then(function (filenames) { - return filenames.filter(function (f) { - return path.extname(f) === ".xhtml"; - }) - .sort() - .map(function (f) { - return { - id: path.basename(f), - title: path.basename(f), // TODO extract actual title... inconvenient - href: `${hrefPrefix}${f}` - }; + return fs.readFile(manifestPath, { encoding: "utf-8" }).then(function (manifestContents) { + const manifestChapters = JSON.parse(manifestContents); + + return fs.readdir(chaptersPath).then(function (filenames) { + return filenames.filter(function (f) { + return path.extname(f) === ".xhtml"; + }) + .sort() + .map(function (f, i) { + return { + id: path.basename(f), + title: manifestChapters[i].title, + href: `${hrefPrefix}${f}` + }; + }); }); }); } diff --git a/lib/worm-scraper.js b/lib/worm-scraper.js index d5d3ecf..9e61d05 100644 --- a/lib/worm-scraper.js +++ b/lib/worm-scraper.js @@ -18,21 +18,21 @@ const chaptersPath = path.resolve(contentPath, "chapters"); const manifestPath = path.resolve(cachePath, "manifest.json"); Promise.resolve() + // .then(function () { + // return download(START_CHAPTER_URL, cachePath, manifestPath); + // }) + // .then(function () { + // return rimraf(chaptersPath); + // }) + // .then(function () { + // return mkdirp(chaptersPath); + // }) + // .then(function () { + // return convert(cachePath, manifestPath, chaptersPath); + // }) .then(function () { - return download(START_CHAPTER_URL, cachePath, manifestPath); + return extras(contentPath, chaptersPath, manifestPath); }) - .then(function () { - return rimraf(chaptersPath); - }) - .then(function () { - return mkdirp(chaptersPath); - }) - .then(function () { - return convert(cachePath, manifestPath, chaptersPath); - }) -/* .then(function () { - return extras(contentPath, chaptersPath); - })*/ .then(function () { console.log("All done!"); });