From ff4c0416af650ca1c9cd5341936e811b07cc6131 Mon Sep 17 00:00:00 2001 From: daniel-j Date: Wed, 10 Aug 2016 22:26:14 +0200 Subject: [PATCH] autofix bad indentation --- src/cleanMarkup.js | 28 ++++++++++++++++++++++++---- src/style.styl | 3 +++ src/templates.js | 8 +++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/cleanMarkup.js b/src/cleanMarkup.js index 3069ffb..237ffb6 100644 --- a/src/cleanMarkup.js +++ b/src/cleanMarkup.js @@ -13,13 +13,18 @@ if (!isNode) { tidy = process.tidy } -let matchYoutube = /
(.+?)<\/div>/g - export function cleanMarkup (html, callback) { // fix center tags html = html.replace(/
/g, '

') html = html.replace(/<\/center>/g, '

') + // replace HTML non-breaking spaces with normal spaces + html = html.replace(/ /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>/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('
', '
') html = html.replace('
', '
') - html = fixDoubleSpacing(html) - html = tidy(`\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('

((<([^>]+)>)*)\\s{' + fixIndent + '}\\s*', 'g'), '

$1') + html = html.replace(new RegExp('

((<([^>]+)>)*)\\s{' + fixIndent + '}\\s*', 'g'), '

$3') + + // Cleanup of remaining start whitespace in already indented paragraphs: + html = html.replace(/]*)>((<[^>]+>)*)\\s+/g, '$2') + } + return html +} diff --git a/src/style.styl b/src/style.styl index e0caa68..1631190 100644 --- a/src/style.styl +++ b/src/style.styl @@ -35,6 +35,9 @@ h1, h2, h3, h4, h5, h6 { font-weight: normal; margin: 0; } + & + p.indented { + text-indent: 0; + } } img { diff --git a/src/templates.js b/src/templates.js index 41cca5d..55a0435 100644 --- a/src/templates.js +++ b/src/templates.js @@ -24,6 +24,8 @@ function prettyDate (d) { } export function createChapter (ch, html, callback) { + let trimWhitespace = /^\s*()+|()+\s*$/ig + let authorNotesPos = html.indexOf('

')) authorNotes = authorNotes.trim() + authorNotes = authorNotes.replace(trimWhitespace, '') } let chapterPos = html.indexOf('
') @@ -38,7 +41,10 @@ export function createChapter (ch, html, callback) { let pos = chapter.indexOf('\t
\t\t\n\t') - chapter = chapter.substring(0, pos) + chapter = chapter.substring(0, pos).trim() + + // remove leading and trailing
tags and whitespace + chapter = chapter.replace(trimWhitespace, '') let sections = [ [