mirror of
https://github.com/daniel-j/fimfic2epub.git
synced 2024-06-29 03:20:17 +12:00
autofix bad indentation
This commit is contained in:
parent
0b438c2032
commit
ff4c0416af
|
@ -13,13 +13,18 @@ if (!isNode) {
|
||||||
tidy = process.tidy
|
tidy = process.tidy
|
||||||
}
|
}
|
||||||
|
|
||||||
let matchYoutube = /<div class="youtube_container">(.+?)<\/div>/g
|
|
||||||
|
|
||||||
export function cleanMarkup (html, callback) {
|
export function cleanMarkup (html, callback) {
|
||||||
// fix center tags
|
// fix center tags
|
||||||
html = html.replace(/<center>/g, '<p style="text-align: center;">')
|
html = html.replace(/<center>/g, '<p style="text-align: center;">')
|
||||||
html = html.replace(/<\/center>/g, '</p>')
|
html = html.replace(/<\/center>/g, '</p>')
|
||||||
|
|
||||||
|
// replace HTML non-breaking spaces with normal spaces
|
||||||
|
html = html.replace(/ /g, ' ')
|
||||||
|
|
||||||
|
html = fixParagraphIndent(html)
|
||||||
|
|
||||||
|
html = fixDoubleSpacing(html)
|
||||||
|
|
||||||
let cache = new Map()
|
let cache = new Map()
|
||||||
let completeCount = 0
|
let completeCount = 0
|
||||||
|
|
||||||
|
@ -41,6 +46,7 @@ export function cleanMarkup (html, callback) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let matchYoutube = /<div class="youtube_container">(.+?)<\/div>/g
|
||||||
for (let ma; (ma = matchYoutube.exec(html));) {
|
for (let ma; (ma = matchYoutube.exec(html));) {
|
||||||
let youtubeId = ma[1].match(/src="https:\/\/www.youtube.com\/embed\/(.+?)"/)[1]
|
let youtubeId = ma[1].match(/src="https:\/\/www.youtube.com\/embed\/(.+?)"/)[1]
|
||||||
cache.set(youtubeId, null)
|
cache.set(youtubeId, null)
|
||||||
|
@ -77,8 +83,6 @@ export function cleanMarkup (html, callback) {
|
||||||
html = html.replace('<blockquote style="margin: 10px 0px; box-sizing:border-box; -moz-box-sizing:border-box;margin-right:25px; padding: 15px;background-color: #F7F7F7;border: 1px solid #AAA;width: 50%;float:left;box-shadow: 5px 5px 0px #EEE;">', '<blockquote class="left_insert">')
|
html = html.replace('<blockquote style="margin: 10px 0px; box-sizing:border-box; -moz-box-sizing:border-box;margin-right:25px; padding: 15px;background-color: #F7F7F7;border: 1px solid #AAA;width: 50%;float:left;box-shadow: 5px 5px 0px #EEE;">', '<blockquote class="left_insert">')
|
||||||
html = html.replace('<blockquote style="margin: 10px 0px; box-sizing:border-box; -moz-box-sizing:border-box;margin-left:25px; padding: 15px;background-color: #F7F7F7;border: 1px solid #AAA;width: 50%;float:right;box-shadow: 5px 5px 0px #EEE;">', '<blockquote class="right_insert">')
|
html = html.replace('<blockquote style="margin: 10px 0px; box-sizing:border-box; -moz-box-sizing:border-box;margin-left:25px; padding: 15px;background-color: #F7F7F7;border: 1px solid #AAA;width: 50%;float:right;box-shadow: 5px 5px 0px #EEE;">', '<blockquote class="right_insert">')
|
||||||
|
|
||||||
html = fixDoubleSpacing(html)
|
|
||||||
|
|
||||||
html = tidy(`<?xml version="1.0" encoding="utf-8"?>\n` + html, tidyOptions)
|
html = tidy(`<?xml version="1.0" encoding="utf-8"?>\n` + html, tidyOptions)
|
||||||
|
|
||||||
callback(html)
|
callback(html)
|
||||||
|
@ -93,3 +97,19 @@ export function fixDoubleSpacing (html) {
|
||||||
html = html.replace(/\s+(<\/[a-z][^>]*>)\s+/g, '$1 ')
|
html = html.replace(/\s+(<\/[a-z][^>]*>)\s+/g, '$1 ')
|
||||||
return html
|
return html
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function fixParagraphIndent (html) {
|
||||||
|
// from FimFictionConverter by Nyerguds// from FimFictionConverter by Nyerguds
|
||||||
|
let fixIndent = 2
|
||||||
|
if (fixIndent > 0) {
|
||||||
|
// only trigger indenting when finding as many whitespace characters in a row as indicated by the FixIndent setting.
|
||||||
|
|
||||||
|
// Add indented class, with the search keeping into account that there could be opening tags behind the p tag.
|
||||||
|
html = html.replace(new RegExp('<p>((<([^>]+)>)*)\\s{' + fixIndent + '}\\s*', 'g'), '<p class="indented">$1')
|
||||||
|
html = html.replace(new RegExp('<p class="(((?!indented)[^>])*)">((<([^>]+)>)*)\\s{' + fixIndent + '}\\s*', 'g'), '<p class="indented $1">$3')
|
||||||
|
|
||||||
|
// Cleanup of remaining start whitespace in already indented paragraphs:
|
||||||
|
html = html.replace(/<p([^>]*)>((<[^>]+>)*)\\s+/g, '<p$1>$2')
|
||||||
|
}
|
||||||
|
return html
|
||||||
|
}
|
||||||
|
|
|
@ -35,6 +35,9 @@ h1, h2, h3, h4, h5, h6 {
|
||||||
font-weight: normal;
|
font-weight: normal;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
& + p.indented {
|
||||||
|
text-indent: 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
img {
|
img {
|
||||||
|
|
|
@ -24,6 +24,8 @@ function prettyDate (d) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createChapter (ch, html, callback) {
|
export function createChapter (ch, html, callback) {
|
||||||
|
let trimWhitespace = /^\s*(<br\s*\/?\s*>)+|(<br\s*\/?\s*>)+\s*$/ig
|
||||||
|
|
||||||
let authorNotesPos = html.indexOf('<div class="authors-note"')
|
let authorNotesPos = html.indexOf('<div class="authors-note"')
|
||||||
let authorNotes = ''
|
let authorNotes = ''
|
||||||
if (authorNotesPos !== -1) {
|
if (authorNotesPos !== -1) {
|
||||||
|
@ -31,6 +33,7 @@ export function createChapter (ch, html, callback) {
|
||||||
authorNotes = html.substring(authorNotesPos + 22)
|
authorNotes = html.substring(authorNotesPos + 22)
|
||||||
authorNotes = authorNotes.substring(0, authorNotes.indexOf('\t\n\t</div>'))
|
authorNotes = authorNotes.substring(0, authorNotes.indexOf('\t\n\t</div>'))
|
||||||
authorNotes = authorNotes.trim()
|
authorNotes = authorNotes.trim()
|
||||||
|
authorNotes = authorNotes.replace(trimWhitespace, '')
|
||||||
}
|
}
|
||||||
|
|
||||||
let chapterPos = html.indexOf('<div id="chapter_container">')
|
let chapterPos = html.indexOf('<div id="chapter_container">')
|
||||||
|
@ -38,7 +41,10 @@ export function createChapter (ch, html, callback) {
|
||||||
|
|
||||||
let pos = chapter.indexOf('\t</div>\t\t\n\t')
|
let pos = chapter.indexOf('\t</div>\t\t\n\t')
|
||||||
|
|
||||||
chapter = chapter.substring(0, pos)
|
chapter = chapter.substring(0, pos).trim()
|
||||||
|
|
||||||
|
// remove leading and trailing <br /> tags and whitespace
|
||||||
|
chapter = chapter.replace(trimWhitespace, '')
|
||||||
|
|
||||||
let sections = [
|
let sections = [
|
||||||
[
|
[
|
||||||
|
|
Loading…
Reference in a new issue