"use strict"; const fs = require("mz/fs"); const path = require("path"); const BOOK_TITLE = "Worm"; const BOOK_AUTHOR = "wildbow"; const BOOK_ID = "urn:uuid:e7f3532d-8db6-4888-be80-1976166b7059"; const NCX_FILENAME = "toc.ncx"; module.exports = function (contentPath, chaptersPath, manifestPath) { return getChapters(contentPath, chaptersPath, manifestPath).then(function (chapters) { return Promise.all([ writeOpf(chapters, contentPath), writeNcx(chapters, contentPath) ]); }); }; function writeOpf(chapters, contentPath) { const manifestChapters = chapters.map(function (c) { return ``; }).join("\n"); const spineChapters = chapters.map(function (c) { return ``; }).join("\n"); const contents = ` ${BOOK_TITLE} en ${BOOK_ID} ${BOOK_AUTHOR} ${manifestChapters} ${spineChapters} `; return fs.writeFile(path.resolve(contentPath, "content.opf"), contents); } function writeNcx(chapters, contentPath) { const navPoints = chapters.map(function (c, i) { return ` ${c.title} `; }).join("\n"); const contents = ` ${BOOK_TITLE} ${BOOK_AUTHOR} ${navPoints} `; return fs.writeFile(path.resolve(contentPath, NCX_FILENAME), contents); } function getChapters(contentPath, chaptersPath, manifestPath) { const hrefPrefix = `${path.relative(contentPath, chaptersPath)}/`; 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}` }; }); }); }); }