From a26f622fbb6e56442a64908c6442e4fa767c90c6 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Sat, 9 May 2015 01:15:22 +0200 Subject: [PATCH] Work on assembling the extras --- .editorconfig | 3 ++ .gitignore | 2 + lib/extras.js | 102 ++++++++++++++++++++++++++++++++++++++++++++ lib/worm-scraper.js | 16 +++++-- 4 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 lib/extras.js diff --git a/.editorconfig b/.editorconfig index 4960585..66e49e7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,3 +7,6 @@ trim_trailing_whitespace = true charset = utf-8 indent_style = space indent_size = 2 + +[out/mimetype] +insert_final_newline = false diff --git a/.gitignore b/.gitignore index ba721ad..5fde29d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ cache/ out/ +!out/mimetime +!out/META-INF/ diff --git a/lib/extras.js b/lib/extras.js new file mode 100644 index 0000000..ded78f4 --- /dev/null +++ b/lib/extras.js @@ -0,0 +1,102 @@ +"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}` + }; + }); + }); +} diff --git a/lib/worm-scraper.js b/lib/worm-scraper.js index 276d66d..75367cf 100644 --- a/lib/worm-scraper.js +++ b/lib/worm-scraper.js @@ -5,6 +5,7 @@ const rimraf = require("rimraf-then"); const download = require("./download.js"); const convert = require("./convert.js"); +const extras = require("./extras.js"); require("./track-rejections.js"); @@ -13,16 +14,23 @@ const START_CHAPTER_URL = "https://parahumans.wordpress.com/2011/06/11/1-1/"; const cachePath = path.resolve("cache"); const outPath = path.resolve("out"); const contentPath = path.resolve(outPath, "OEBPS"); +const chaptersPath = path.resolve(contentPath, "chapters"); -rimraf(outPath) +Promise.resolve() .then(function () { - return mkdirp(contentPath); +// return download(START_CHAPTER_URL, cachePath); }) .then(function () { - return download(START_CHAPTER_URL, cachePath); +// return rimraf(chaptersPath); }) .then(function () { - return convert(cachePath, contentPath); +// return mkdirp(chaptersPath); + }) + .then(function () { +// return convert(cachePath, chaptersPath); + }) + .then(function () { + return extras(contentPath, chaptersPath); }) .then(function () { console.log("All done!");