Рефакторинг, плюс небольшие доработки

This commit is contained in:
Book Pauk
2020-12-09 01:29:09 +07:00
parent fd9ec736d7
commit 174c877eee
3 changed files with 32 additions and 27 deletions

View File

@@ -39,13 +39,13 @@ class ConvertFb3 extends ConvertHtml {
const title = this.getTitle(text)
.replace(/<\/?p>/g, '')
;
text = `<title>${title}</title>` + text
text = `<fb2-title>${title}</fb2-title>` + text
.replace(/<title>/g, '<br><b>')
.replace(/<\/title>/g, '</b><br>')
.replace(/<subtitle>/g, '<br><br><fb2-subtitle>')
.replace(/<\/subtitle>/g, '</fb2-subtitle>')
;
return await super.run(Buffer.from(text), {skipCheck: true, cutTitle: true});
return await super.run(Buffer.from(text), {skipCheck: true});
}
}

View File

@@ -34,7 +34,6 @@ class ConvertHtml extends ConvertBase {
} else {
isText = opts.isText;
}
let {cutTitle} = opts;
let titleInfo = {};
let desc = {_n: 'description', 'title-info': titleInfo};
@@ -44,7 +43,9 @@ class ConvertHtml extends ConvertBase {
let fb2 = [desc, body, binary];
let title = '';
let author = '';
let inTitle = false;
let inAuthor = false;
let inSubTitle = false;
let inImage = false;
let image = {};
@@ -94,7 +95,7 @@ class ConvertHtml extends ConvertBase {
const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
text = this.escapeEntities(text);
if (!cutCounter && !(cutTitle && inTitle)) {
if (!(cutCounter || inTitle) || inSubTitle) {
let tOpen = '';
tOpen += (inSubTitle ? '<subtitle>' : '');
tOpen += (bold ? '<strong>' : '');
@@ -110,6 +111,9 @@ class ConvertHtml extends ConvertBase {
if (inTitle && !title)
title = text;
if (inAuthor && !author)
author = text;
if (inImage) {
image._t = text;
binary.push(image);
@@ -142,10 +146,12 @@ class ConvertHtml extends ConvertBase {
}
}
if (tag == 'title' || tag == 'cut-title') {
if (tag == 'title' || tag == 'fb2-title') {
inTitle = true;
if (tag == 'cut-title')
cutTitle = true;
}
if (tag == 'fb2-author') {
inAuthor = true;
}
if (tag == 'fb2-subtitle') {
@@ -181,9 +187,13 @@ class ConvertHtml extends ConvertBase {
}
}
if (tag == 'title' || tag == 'cut-title')
if (tag == 'title' || tag == 'fb2-title')
inTitle = false;
if (tag == 'fb2-author') {
inAuthor = false;
}
if (tag == 'fb2-subtitle')
inSubTitle = false;
@@ -195,10 +205,15 @@ class ConvertHtml extends ConvertBase {
sax.parseSync(buf, {
onStartNode, onEndNode, onTextNode,
innerCut: new Set(['head', 'script', 'style', 'binary', 'fb2-image'])
innerCut: new Set(['head', 'script', 'style', 'binary', 'fb2-image', 'fb2-title', 'fb2-author'])
});
titleInfo['book-title'] = title;
if (author)
titleInfo.author = {'last-name': author};
body.section._a[0] = pars;
//подозрение на чистый текст, надо разбить на параграфы
if (isText || (buf.length > 30*1024 && pars.length < buf.length/2000)) {
let total = 0;
@@ -228,28 +243,19 @@ class ConvertHtml extends ConvertBase {
if (parIndent > 2) parIndent--;
let newPars = [];
let curPar = {};
const newPar = () => {
newPars.push({_n: 'p', _t: ''});
curPar = {_n: 'p', _t: ''};
newPars.push(curPar);
};
const growPar = (text) => {
if (!newPars.length)
newPar();
const l = newPars.length;
newPars[l - 1]._t += text;
}
i = 0;
for (const par of pars) {
if (par._n != 'p') {
newPars.push(par);
continue;
}
if (i > 0)
newPar();
i++;
newPar();
let j = 0;
const lines = par._t.split('\n');
@@ -266,13 +272,12 @@ class ConvertHtml extends ConvertBase {
newPar();
j++;
}
growPar(line.trim() + ' ');
curPar._t += line.trim() + ' ';
}
}
body.section._a[0] = newPars;
} else {
body.section._a[0] = pars;
}
//убираем лишнее, делаем валидный fb2, т.к. в рез-те разбиения на параграфы бьются теги

View File

@@ -48,7 +48,7 @@ class ConvertSites extends ConvertHtml {
if (text === false)
return false;
return await super.run(Buffer.from(text), {skipCheck: true, cutTitle: true});
return await super.run(Buffer.from(text), {skipCheck: true});
}
getTitle(text) {
@@ -79,7 +79,7 @@ class ConvertSites extends ConvertHtml {
let book = this.getTitle(text);
book = book.replace(' (fb2) | Флибуста', '');
const title = `<title>${author}${(author ? ' - ' : '')}${book}</title>`;
const title = `<fb2-title>${author}${(author ? ' - ' : '')}${book}</fb2-title>`;
let begin = '<h3 class="book">';
if (text.indexOf(begin) <= 0)