Commit 16a55dec authored by Alf Eaton's avatar Alf Eaton

Process chapters in sequence

Chapter order should be preserved even if one chapter takes longer to process than another.
parent 5a1a46ec
......@@ -4,10 +4,11 @@ const path = require('path')
const tidy = require('libtidy')
const mime = require('mime-types')
const cheerio = require('cheerio')
const Promise = require('bluebird')
const archiver = require('archiver')
const builder = require('xmlbuilder')
// given an array of HTML files and the, plus metadata, generate an EPUB file
// given an array of HTML files, plus metadata, generate an EPUB file
class HTMLEPUB {
constructor (book = {}, options = {}) {
this.book = book
......@@ -18,19 +19,20 @@ class HTMLEPUB {
}
load (items) {
let counter = 0
return Promise.all(items.map(item => {
return Promise.mapSeries(items, (item, index) => {
return this.parse(item.content).then($ => {
const id = `page-${++counter}`
const pageIndex = index + 1
const id = `page-${pageIndex}`
const title = item.title || $('h1').text()
const target = `xhtml/${id}.xhtml`
this.xhtml.push({ id, $, title, target })
this.xhtml[index] = { id, $, title, target }
this.extract($)
this.extract($, pageIndex)
})
}))
})
}
stream (outputStream) {
......@@ -116,14 +118,14 @@ class HTMLEPUB {
})
}
extract ($) {
extract ($, pageIndex) {
const epub = this
const resourceRoot = epub.options.resourceRoot.replace(/\/?$/, '/') // ensure trailing slash
$('img[src]').each((index, node) => {
const $node = $(node)
const id = `image-${index}`
const id = `image-${pageIndex}-${index}`
const uri = $node.attr('src').replace(/^\//, '') // ensure no leading slash
const source = url.resolve(resourceRoot, uri)
......@@ -143,7 +145,7 @@ class HTMLEPUB {
$('head > link[rel="stylesheet"]').each((index, node) => {
const $node = $(node)
const id = `style-${index}`
const id = `style-${pageIndex}-${index}`
const uri = $node.attr('href').replace(/^\//, '') // ensure no leading slash
const source = url.resolve(resourceRoot, uri)
......
......@@ -9,12 +9,13 @@
"scripts": {
"test": "jest --runInBand test"
},
"main": "lib/index.js",
"main": "lib",
"files": [
"lib"
],
"dependencies": {
"archiver": "^1.3.0",
"bluebird": "^3.5.1",
"cheerio": "^0.22.0",
"libtidy": "^0.3.2",
"mime-types": "^2.1.15",
......@@ -30,7 +31,7 @@
"eslintConfig": {
"extends": "standard",
"parserOptions": {
"ecmaVersion": 6
"ecmaVersion": 2015
},
"env": {
"es6": true,
......
......@@ -4,7 +4,7 @@
</head>
<body>
<div>
<h1>Test Chapter</h1>
<h1>Chapter 1</h1>
<h2>Test Section</h2>
<div>
<p>Test paragraph</p>
......@@ -13,6 +13,14 @@
<input name="example">
</form>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<p>We will now prove the Pythogorian theorem: <math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math></p>
<svg width="200" height="250" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect x="10" y="10" width="30" height="30" stroke="black" fill="transparent" stroke-width="5"/>
</svg>
......
<html>
<head>
<link rel="stylesheet" href="/styles/epub.css">
</head>
<body>
<div>
<h1>Chapter 2</h1>
<h2>Test Section</h2>
<div>
<p>Test paragraph</p>
</div>
</div>
</body>
</html>
......@@ -68,6 +68,23 @@ test('extracts images', (done) => {
}).catch(done.fail)
})
test('chapter order is preserved', (done) => {
const epub = new HTMLEPUB(book, { resourceRoot })
epub.load(html).then(() => {
try {
expect(epub.xhtml[0].id).toEqual('page-1')
expect(epub.xhtml[0].$('h1').text()).toEqual('Chapter 1')
expect(epub.xhtml[1].id).toEqual('page-2')
expect(epub.xhtml[1].$('h1').text()).toEqual('Chapter 2')
done()
} catch (e) {
done.fail(e)
}
}).catch(done.fail)
})
test('extracts stylesheets', (done) => {
const epub = new HTMLEPUB(book, { resourceRoot })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment