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

This commit is contained in:
Book Pauk
2019-02-20 19:43:39 +07:00
parent dd61c04d63
commit d3a30b87f4
2 changed files with 15 additions and 12 deletions

View File

@@ -73,7 +73,7 @@ export default class DrawHelper {
let lineText = ''; let lineText = '';
let center = false; let center = false;
let space = false; let space = 0;
let j = 0; let j = 0;
//формируем строку //формируем строку
for (const part of line.parts) { for (const part of line.parts) {
@@ -97,7 +97,7 @@ export default class DrawHelper {
lineText += `${tOpen}${text}${tClose}`; lineText += `${tOpen}${text}${tClose}`;
center = center || part.style.center; center = center || part.style.center;
space = space || part.style.space; space = (part.style.space > 0 ? part.style.space : space);
//избражения //избражения
//image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number}, //image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number},
@@ -129,7 +129,7 @@ export default class DrawHelper {
const centerStyle = (center ? `text-align: center; text-align-last: center; width: ${this.w}px` : '') const centerStyle = (center ? `text-align: center; text-align-last: center; width: ${this.w}px` : '')
if ((line.first || space) && !center) { if ((line.first || space) && !center) {
let p = (line.first ? this.p : 0); let p = (line.first ? this.p : 0);
p = (space ? p + this.p : p); p = (space ? p + this.p*space : p);
lineText = `<span style="display: inline-block; margin-left: ${p}px"></span>${lineText}`; lineText = `<span style="display: inline-block; margin-left: ${p}px"></span>${lineText}`;
} }

View File

@@ -39,7 +39,7 @@ export default class BookParser {
let center = false; let center = false;
let bold = false; let bold = false;
let italic = false; let italic = false;
let space = false; let space = 0;
this.binary = {}; this.binary = {};
let binaryId = ''; let binaryId = '';
@@ -179,7 +179,7 @@ export default class BookParser {
if (tag == 'epigraph') { if (tag == 'epigraph') {
italic = true; italic = true;
space = true; space += 1;
} }
if (tag == 'poem') { if (tag == 'poem') {
@@ -187,7 +187,8 @@ export default class BookParser {
} }
if (tag == 'text-author') { if (tag == 'text-author') {
space = true; newParagraph(' ', 1);
space += 1;
} }
} }
}; };
@@ -214,7 +215,7 @@ export default class BookParser {
if (tag == 'epigraph') { if (tag == 'epigraph') {
italic = false; italic = false;
space = false; space -= 1;
} }
if (tag == 'stanza') { if (tag == 'stanza') {
@@ -222,7 +223,7 @@ export default class BookParser {
} }
if (tag == 'text-author') { if (tag == 'text-author') {
space = false; space -= 1;
} }
} }
@@ -285,7 +286,7 @@ export default class BookParser {
let tOpen = (center ? '<center>' : ''); let tOpen = (center ? '<center>' : '');
tOpen += (bold ? '<strong>' : ''); tOpen += (bold ? '<strong>' : '');
tOpen += (italic ? '<emphasis>' : ''); tOpen += (italic ? '<emphasis>' : '');
tOpen += (space ? '<space>' : ''); tOpen += (space ? `<space w="${space}">` : '');
let tClose = (space ? '</space>' : ''); let tClose = (space ? '</space>' : '');
tClose += (italic ? '</emphasis>' : ''); tClose += (italic ? '</emphasis>' : '');
tClose += (bold ? '</strong>' : ''); tClose += (bold ? '</strong>' : '');
@@ -389,7 +390,9 @@ export default class BookParser {
style.center = true; style.center = true;
break; break;
case 'space': { case 'space': {
style.space = true; let attrs = sax.getAttrsSync(tail);
if (attrs.w.value)
style.space = attrs.w.value;
break; break;
} }
case 'image': { case 'image': {
@@ -420,7 +423,7 @@ export default class BookParser {
style.center = false; style.center = false;
break; break;
case 'space': case 'space':
style.space = false; style.space = 0;
break; break;
case 'image': case 'image':
image = {}; image = {};
@@ -619,7 +622,7 @@ export default class BookParser {
str += sp1 + word; str += sp1 + word;
let p = (j == 0 ? parsed.p : 0); let p = (j == 0 ? parsed.p : 0);
p = (style.space ? p + parsed.p : p); p = (style.space ? p + parsed.p*style.space : p);
let w = this.measureText(str, style) + p; let w = this.measureText(str, style) + p;
let wordTail = word; let wordTail = word;
if (w > parsed.w && prevStr != '') { if (w > parsed.w && prevStr != '') {