Update extras code to use cache manifest

Finally we have reasonable chapter titles in the TOC.
This commit is contained in:
Domenic Denicola 2015-05-11 21:23:13 -04:00
commit ace6a55be8
2 changed files with 31 additions and 27 deletions

View file

@ -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}`
};
});
});
});
}

View file

@ -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!");
});