Улучшение парсинга fb2

This commit is contained in:
Book Pauk
2019-02-13 14:49:51 +07:00
parent 5c29594b3d
commit 0d27dabd62

View File

@@ -63,7 +63,8 @@ export default class BookParser {
const growParagraph = (text, len) => { const growParagraph = (text, len) => {
if (paraIndex < 0) { if (paraIndex < 0) {
newParagraph(text, len); newParagraph(' ', 1);
growParagraph(text, len);
return; return;
} }
@@ -96,18 +97,19 @@ export default class BookParser {
tag = elemName; tag = elemName;
path += '/' + elemName; path += '/' + elemName;
if ((tag == 'p' || tag == 'empty-line' || tag == 'v') && path.indexOf('/fictionbook/body/section') == 0) { if (path.indexOf('/fictionbook/body') == 0) {
if (tag == 'title') {
newParagraph(' ', 1); newParagraph(' ', 1);
bold = true;
center = true;
} }
if (tag == 'emphasis' || tag == 'strong') { if (tag == 'emphasis' || tag == 'strong') {
growParagraph(`<${tag}>`, 0); growParagraph(`<${tag}>`, 0);
} }
if (tag == 'title') { if ((tag == 'p' || tag == 'empty-line' || tag == 'v')) {
newParagraph(' ', 1); newParagraph(' ', 1);
bold = true;
center = true;
} }
if (tag == 'subtitle') { if (tag == 'subtitle') {
@@ -126,20 +128,21 @@ export default class BookParser {
if (tag == 'text-author') { if (tag == 'text-author') {
newParagraph(' <s> <s> <s> ', 4); newParagraph(' <s> <s> <s> ', 4);
} }
}
}; };
const onEndNode = (elemName) => {// eslint-disable-line no-unused-vars const onEndNode = (elemName) => {// eslint-disable-line no-unused-vars
if (tag == elemName) { if (tag == elemName) {
if (tag == 'emphasis' || tag == 'strong') { if (path.indexOf('/fictionbook/body') == 0) {
growParagraph(`</${tag}>`, 0);
}
if (tag == 'title') { if (tag == 'title') {
bold = false; bold = false;
center = false; center = false;
} }
if (tag == 'emphasis' || tag == 'strong') {
growParagraph(`</${tag}>`, 0);
}
if (tag == 'subtitle') { if (tag == 'subtitle') {
bold = false; bold = false;
} }
@@ -151,6 +154,7 @@ export default class BookParser {
if (tag == 'stanza') { if (tag == 'stanza') {
newParagraph(' ', 1); newParagraph(' ', 1);
} }
}
path = path.substr(0, path.length - tag.length - 1); path = path.substr(0, path.length - tag.length - 1);
let i = path.lastIndexOf('/'); let i = path.lastIndexOf('/');
@@ -211,12 +215,12 @@ export default class BookParser {
let tOpen = (center ? '<center>' : ''); let tOpen = (center ? '<center>' : '');
tOpen += (bold ? '<strong>' : ''); tOpen += (bold ? '<strong>' : '');
tOpen += (italic ? '<emphasis>' : ''); tOpen += (italic ? '<emphasis>' : '');
let tClose = (center ? '</center>' : ''); let tClose = (italic ? '</emphasis>' : '');
tClose += (bold ? '</strong>' : ''); tClose += (bold ? '</strong>' : '');
tClose += (italic ? '</emphasis>' : ''); tClose += (center ? '</center>' : '');
if (path.indexOf('/fictionbook/body/title') == 0) { if (path.indexOf('/fictionbook/body/title') == 0) {
newParagraph(`${tOpen}${text}${tClose}`, text.length, true); growParagraph(`${tOpen}${text}${tClose}`, text.length, true);
} }
if (path.indexOf('/fictionbook/body/section') == 0) { if (path.indexOf('/fictionbook/body/section') == 0) {