Fix kepub issue, bump version and update deps

This commit is contained in:
daniel-j 2018-05-17 11:36:04 +02:00
parent 3c18655eed
commit ce3e7022b6
3 changed files with 70 additions and 42 deletions

View file

@ -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": {

View file

@ -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 '<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\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)
})
}

View file

@ -6,10 +6,18 @@ const kepubify = require('../src/kepubify').default
console.log(1, kepubify(`<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html><body>text <p>aaaa</p><p>Some text. Woo <!-- or --> not. Here is <img /> another sentence.</p><!-- comment --><p>More text <img/> tail</p> body tail</body> html tail</html>`) === `<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\n<html><body>text <div class="book-inner"><div class="book-columns"><p><span class="koboSpan" id="kobo.1.0">aaaa</span></p><p><span class="koboSpan" id="kobo.2.0">Some text.</span><span class="koboSpan" id="kobo.2.1"> Woo not.</span><span class="koboSpan" id="kobo.2.2"> Here is </span><span class="koboSpan" id="kobo.2.4"> another sentence.</span><span class="koboSpan" id="kobo.2.3"><img /></span></p><p><span class="koboSpan" id="kobo.3.0">More text </span><span class="koboSpan" id="kobo.3.2"> tail</span><span class="koboSpan" id="kobo.3.1"><img /></span></p><span class="koboSpan" id="kobo.3.3"> body tail</span></div></div></body></html>`)
<html><body>text <p>aaaa</p><p>Some text. Woo <!-- or --> not. Here is <img /> another sentence.</p><!-- comment --><p>More text <img/> tail</p> body tail</body> html tail</html>`) === `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html><body>text <div class="book-inner"><div class="book-columns"><p><span class="koboSpan" id="kobo.1.1">aaaa</span></p><p><span class="koboSpan" id="kobo.2.1">Some text.</span><span class="koboSpan" id="kobo.2.2"> Woo not.</span><span class="koboSpan" id="kobo.2.3"> Here is </span><img /><span class="koboSpan" id="kobo.2.5"> another sentence.</span></p><p><span class="koboSpan" id="kobo.3.1">More text </span><img /><span class="koboSpan" id="kobo.3.3"> tail</span></p><span class="koboSpan" id="kobo.3.5"> body tail</span></div></div></body></html>`)
console.log(2, kepubify(`<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html><body><p>Dated: June 5th. Wohoo</p></body></html>`) === `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html><body><div class="book-inner"><div class="book-columns"><p><span class="koboSpan" id="kobo.1.0">Dated:</span><span class="koboSpan" id="kobo.1.1"> June 5th.</span><span class="koboSpan" id="kobo.1.2"> Wohoo</span></p></div></div></body></html>`)
<html><body><div class="book-inner"><div class="book-columns"><p><span class="koboSpan" id="kobo.1.1">Dated:</span><span class="koboSpan" id="kobo.1.2"> June 5th.</span><span class="koboSpan" id="kobo.1.3"> Wohoo</span></p></div></div></body></html>`)
console.log(3, kepubify(`<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html><body><pre>hello</pre><p>“Well, you know, Water Bearer and all,” she laughed weakly. “Dont kid yourself, though. Youre <i>strong,</i> Daphne, and Im not just saying that. You saved us, and youve come all this way through all these trials. If you get a little heartbroken now and again, well, youre entitled to it. Youre 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. <i>Apparently</i> thats why things here resemble stuff on our earth so wellbecause 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.</p></body></html>`) === `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html><body><div class="book-inner"><div class="book-columns"><pre>hello</pre><p><span class="koboSpan" id="kobo.1.1">Well, you know, Water Bearer and all, she laughed weakly.</span><span class="koboSpan" id="kobo.1.2"> Dont kid yourself, though.</span><span class="koboSpan" id="kobo.1.3"> Youre </span><i><span class="koboSpan" id="kobo.1.5">strong,</span></i><span class="koboSpan" id="kobo.1.7"> Daphne, and Im not just saying that.</span><span class="koboSpan" id="kobo.1.8"> You saved us, and youve come all this way through all these trials.</span><span class="koboSpan" id="kobo.1.9"> If you get a little heartbroken now and again, well, youre entitled to it.</span><span class="koboSpan" id="kobo.1.10"> Youre like your mother in that.</span><span class="koboSpan" id="kobo.1.11"> She smiled brightly.</span><span class="koboSpan" id="kobo.1.12"> The Seer told me a bit about that, you know.</span><span class="koboSpan" id="kobo.1.13"> The whole thing passed down the female line, on and on for ages, all of them determined and gifted. </span><i><span class="koboSpan" id="kobo.1.15">Apparently</span></i><span class="koboSpan" id="kobo.1.17"> thats why things here resemble stuff on our earth so wellbecause the Everfree Ways have been following your maternal line all across Europe and the Americas, and apparently a brief stop at New Zealand.</span><span class="koboSpan" id="kobo.1.18"> They were super adventurous.</span></p></div></div></body></html>`)