diff --git a/package.json b/package.json index cbc419f..10c365b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fimfic2epub", - "version": "1.7.32", + "version": "1.7.33", "description": "Tool to generate improved EPUB ebooks from Fimfiction stories", "author": "djazz", "license": "MIT", @@ -27,7 +27,7 @@ "detect-node": "^2.0.3", "elementtree": "^0.1.7", "escape-string-regexp": "^1.0.5", - "file-type": "^7.7.1", + "file-type": "^8.0.0", "fonteditor-core": "^1.0.2", "html-entities": "^1.2.1", "html-to-text": "^4.0.0", @@ -39,7 +39,7 @@ "mithril-node-render": "^2.3.0", "node-png": "^0.4.3", "pretty-data": "^0.40.0", - "request": "^2.85.0", + "request": "^2.86.0", "sanitize-filename": "^1.6.1", "syllable": "^3.0.0", "twemoji": "^2.5.1", @@ -51,7 +51,7 @@ "autosize": "^4.0.2", "babel-core": "^6.26.3", "babel-loader": "^7.1.4", - "babel-preset-env": "^1.6.1", + "babel-preset-env": "^1.7.0", "babel-register": "^6.26.0", "binary-loader": "0.0.1", "del": "^3.0.0", @@ -66,7 +66,7 @@ "gulp-chmod": "^2.0.0", "gulp-filter": "^5.1.0", "gulp-json-editor": "^2.3.0", - "gulp-rename": "^1.2.2", + "gulp-rename": "^1.2.3", "gulp-standard": "^11.0.0", "gulp-util": "^3.0.8", "gulp-watch": "^5.0.0", @@ -79,7 +79,7 @@ "standard": "^11.0.1", "stylus": "^0.54.5", "stylus-loader": "^3.0.2", - "webpack": "^4.8.1", + "webpack": "^4.8.3", "webpack-node-externals": "^1.7.2" }, "standard": { diff --git a/src/kepubify.js b/src/kepubify.js index 2b12503..f683977 100644 --- a/src/kepubify.js +++ b/src/kepubify.js @@ -8,7 +8,10 @@ export default function kepubify (html) { const body = tree.find('./body') addDivs(body) const state = {paragraph: 0, segment: 0} - body.getchildren().forEach((child) => addSpansToNode(child, body, state)) + body.getchildren().forEach((child) => { + fixupTree(child, body) + addSpansToNode(child, body, state) + }) return '\n\n' + tree.write({ xml_declaration: false }) @@ -33,7 +36,7 @@ function createSpan (paragraph, segment) { return span } -function textToSpans (node, text, state) { +function textToSentences (text) { const tokenSentences = text .replace('\0', '') .replace(/\s+/g, ' ') // Replace all whitespace (including newlines) with a single space @@ -52,45 +55,62 @@ function textToSpans (node, text, state) { } return tokenSentences.map((sentence, i) => { - const span = createSpan(state.paragraph, state.segment++) - span.text = sentence - return span + // const span = createSpan(state.paragraph, state.segment++) + // span.text = sentence + return sentence + }) +} + +// Makes text nodes of .text and .tail as children +function fixupTree (node, parent) { + if (node.tag !== '#') { + if (node.text && !node.tag.match(specialTags)) { + let el = et.Element('#') + el.text = node.text + node._children.unshift(el) + delete node.text + } + if (node.tail) { + let el = et.Element('#') + el.text = node.tail + let pos = parent._children.indexOf(node) + 1 + parent._children.splice(pos, 0, el) + delete node.tail + } + } + node._children.slice(0).forEach((child) => { + fixupTree(child, node) }) } function addSpansToNode (node, parent, state) { - let nodePosition = parent.getchildren().indexOf(node) + // text node + if (node.tag === '#') { + state.segment++ + + let sentences = textToSentences(node.text) + let pos + + sentences.forEach((sentence) => { + let span = createSpan(state.paragraph, state.segment++) + span.text = sentence + + // insert the span before the text node + pos = parent._children.indexOf(node) + parent._children.splice(pos, 0, span) + }) + + // remove the text node + pos = parent._children.indexOf(node) + parent._children.splice(pos, 1) + } if (node.tag.match(paragraphTags)) { - state.paragraph++ state.segment = 0 + state.paragraph++ } - if (node.tag.match(specialTags)) { - const span = createSpan(state.paragraph, state.segment++) - span.append(node) - parent.getchildren().splice(nodePosition, 1, span) - } else { - let prependNodes = [] - - if (node.text) { - prependNodes = textToSpans(node, node.text, state) - node.text = null - } - - node.getchildren().forEach((child) => { - addSpansToNode(child, node, state) - }) - - prependNodes.forEach((span, i) => { - node.getchildren().splice(i, 0, span) - }) - } - if (node.tail) { - nodePosition = parent.getchildren().indexOf(node) - textToSpans(node, node.tail, state).forEach((span, i) => { - parent.getchildren().splice(nodePosition + 1 + i, 0, span) - }) - node.tail = null - } + node.getchildren().slice(0).forEach((child) => { + addSpansToNode(child, node, state) + }) } diff --git a/test/test-kepubify.js b/test/test-kepubify.js index 7cfa20c..38ccf06 100644 --- a/test/test-kepubify.js +++ b/test/test-kepubify.js @@ -6,10 +6,18 @@ const kepubify = require('../src/kepubify').default console.log(1, kepubify(` -
textaaaa
Some text. Woo not. Here is another sentence.
More text tail
body tail html tail`) === `\n\ntextaaaa
Some text. Woo not. Here is another sentence.
More text tail
body tailaaaa
Some text. Woo not. Here is another sentence.
More text tail
body tail html tail`) === ` + +textaaaa
Some text. Woo not. Here is another sentence.
More text tail
body tailDated: June 5th. Wohoo
`) === ` -Dated: June 5th. Wohoo
Dated: June 5th. Wohoo
hello
“Well, you know, Water Bearer and all,” she laughed weakly. “Don’t kid yourself, though. You’re strong, Daphne, and I’m not just saying that. You saved us, and you’ve come all this way through all these trials. If you get a little heartbroken now and again, well, you’re entitled to it. You’re like your mother in that.” She smiled brightly. “The Seer told me a bit about that, you know. The whole… thing passed down the female line, on and on for ages, all of them determined and gifted. Apparently that’s why things here resemble stuff on our earth so well—because the Everfree Ways have been following your maternal line all across Europe and the Americas, and apparently a brief stop at New Zealand. They were super adventurous.”
`) === ` + +hello
“Well, you know, Water Bearer and all,” she laughed weakly. “Don’t kid yourself, though. You’re strong, Daphne, and I’m not just saying that. You saved us, and you’ve come all this way through all these trials. If you get a little heartbroken now and again, well, you’re entitled to it. You’re like your mother in that.” She smiled brightly. “The Seer told me a bit about that, you know. The whole… thing passed down the female line, on and on for ages, all of them determined and gifted. Apparently that’s why things here resemble stuff on our earth so well—because the Everfree Ways have been following your maternal line all across Europe and the Americas, and apparently a brief stop at New Zealand. They were super adventurous.”