autofix bad indentation

This commit is contained in:
daniel-j 2016-08-10 22:26:14 +02:00
parent 0b438c2032
commit ff4c0416af
3 changed files with 34 additions and 5 deletions

View file

@ -13,13 +13,18 @@ if (!isNode) {
tidy = process.tidy
}
let matchYoutube = /<div class="youtube_container">(.+?)<\/div>/g
export function cleanMarkup (html, callback) {
// fix center tags
html = html.replace(/<center>/g, '<p style="text-align: center;">')
html = html.replace(/<\/center>/g, '</p>')
// replace HTML non-breaking spaces with normal spaces
html = html.replace(/&nbsp;/g, ' ')
html = fixParagraphIndent(html)
html = fixDoubleSpacing(html)
let cache = new Map()
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));) {
let youtubeId = ma[1].match(/src="https:\/\/www.youtube.com\/embed\/(.+?)"/)[1]
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-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)
callback(html)
@ -93,3 +97,19 @@ export function fixDoubleSpacing (html) {
html = html.replace(/\s+(<\/[a-z][^>]*>)\s+/g, '$1 ')
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
}

View file

@ -35,6 +35,9 @@ h1, h2, h3, h4, h5, h6 {
font-weight: normal;
margin: 0;
}
& + p.indented {
text-indent: 0;
}
}
img {

View file

@ -24,6 +24,8 @@ function prettyDate (d) {
}
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 authorNotes = ''
if (authorNotesPos !== -1) {
@ -31,6 +33,7 @@ export function createChapter (ch, html, callback) {
authorNotes = html.substring(authorNotesPos + 22)
authorNotes = authorNotes.substring(0, authorNotes.indexOf('\t\n\t</div>'))
authorNotes = authorNotes.trim()
authorNotes = authorNotes.replace(trimWhitespace, '')
}
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')
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 = [
[