"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) { return getChapters(contentPath, chaptersPath).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) { 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}` }; }); }); }