Улучшен convertHtml

This commit is contained in:
Book Pauk
2019-02-23 19:47:16 +07:00
parent aeadb5aeb8
commit 1e1a58b58c

View File

@@ -111,19 +111,21 @@ class BookConverter {
}; };
const growParagraph = (text) => { const growParagraph = (text) => {
if (!pars.length)
newParagraph();
const l = pars.length; const l = pars.length;
if (l) { if (pars[l - 1]._t == '')
if (pars[l - 1]._t == '') text = text.trimLeft();
text = text.trimLeft(); pars[l - 1]._t += text;
pars[l - 1]._t += text;
}
//посчитаем отступы у текста, чтобы выделить потом параграфы //посчитаем отступы у текста, чтобы выделить потом параграфы
const lines = text.split('\n'); const lines = text.split('\n');
for (const line of lines) { for (let line of lines) {
const sp = line.split(' '); line = repSpaces2(line).replace(/\t/g, ' ');
let l = 0; let l = 0;
while (l < sp.length && sp[l].trim() == '') { while (l < line.length && line[l] == ' ') {
l++; l++;
} }
if (!spaceCounter[l]) if (!spaceCounter[l])
@@ -132,7 +134,6 @@ class BookConverter {
} }
}; };
newParagraph();
const newPara = new Set(['tr', 'br', 'br/', 'dd', 'p', 'title', '/title', 'h1', 'h2', 'h3', '/h1', '/h2', '/h3']); const newPara = new Set(['tr', 'br', 'br/', 'dd', 'p', 'title', '/title', 'h1', 'h2', 'h3', '/h1', '/h2', '/h3']);
const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
@@ -186,22 +187,23 @@ class BookConverter {
}; };
const growPar = (text) => { const growPar = (text) => {
if (!newPars.length)
newPar();
const l = newPars.length; const l = newPars.length;
if (l) { newPars[l - 1]._t += text;
newPars[l - 1]._t += text;
}
} }
for (const par of pars) { for (const par of pars) {
newPar();
const lines = par._t.split('\n'); const lines = par._t.split('\n');
for (const line of lines) { for (let line of lines) {
const sp = line.split(' '); line = repSpaces2(line).replace(/\t/g, ' ');
let l = 0; let l = 0;
while (l < sp.length && sp[l].trim() == '') { while (l < line.length && line[l] == ' ') {
l++; l++;
} }
if (l >= parIndent) if (l >= parIndent)
newPar(); newPar();
growPar(line.trim() + ' '); growPar(line.trim() + ' ');