Промежуточный коммит, парсинг изображений

This commit is contained in:
Book Pauk
2019-02-18 23:55:32 +07:00
parent 8d40ed0bda
commit d59d0a6a42

View File

@@ -2,6 +2,8 @@ import he from 'he';
import sax from '../../../../server/core/BookConverter/sax'; import sax from '../../../../server/core/BookConverter/sax';
import {sleep} from '../../../share/utils'; import {sleep} from '../../../share/utils';
const maxImageLineCount = 100;
export default class BookParser { export default class BookParser {
constructor() { constructor() {
// defaults // defaults
@@ -143,6 +145,12 @@ export default class BookParser {
binaryId = (attrs.id.value ? attrs.id.value : ''); binaryId = (attrs.id.value ? attrs.id.value : '');
} }
if (tag == 'image') {
let attrs = sax.getAttrsSync(tail);
if (attrs.href.value)
newParagraph(`<image href="${attrs.href.value}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
}
if (path.indexOf('/fictionbook/body') == 0) { if (path.indexOf('/fictionbook/body') == 0) {
if (tag == 'title') { if (tag == 'title') {
newParagraph(' ', 1); newParagraph(' ', 1);
@@ -301,6 +309,7 @@ export default class BookParser {
try { try {
await Promise.all(dimPromises); await Promise.all(dimPromises);
} catch (e) { } catch (e) {
//
} }
} }
@@ -340,18 +349,26 @@ export default class BookParser {
splitToStyle(s) { splitToStyle(s) {
let result = [];/*array of { let result = [];/*array of {
style: {bold: Boolean, italic: Boolean, center: Boolean}, style: {bold: Boolean, italic: Boolean, center: Boolean},
image: Boolean,
imageId: String,
text: String, text: String,
}*/ }*/
let style = {}; let style = {};
let image = {};
/*let attrs = sax.getAttrsSync(tail);
if (attrs.href.value)
newParagraph(' '.repeat(maxImageLineCount) + `<image href="${attrs.href.value}" />`, maxImageLineCount);
*/
const onTextNode = async(text) => {// eslint-disable-line no-unused-vars const onTextNode = async(text) => {// eslint-disable-line no-unused-vars
result.push({ result.push({
style: Object.assign({}, style), style: Object.assign({}, style),
text: text image,
text
}); });
}; };
const onStartNode = async(elemName) => {// eslint-disable-line no-unused-vars const onStartNode = async(elemName, tail) => {// eslint-disable-line no-unused-vars
switch (elemName) { switch (elemName) {
case 'strong': case 'strong':
style.bold = true; style.bold = true;
@@ -362,6 +379,9 @@ export default class BookParser {
case 'center': case 'center':
style.center = true; style.center = true;
break; break;
case 'image':
image = {};
break;
} }
}; };
@@ -376,6 +396,9 @@ export default class BookParser {
case 'center': case 'center':
style.center = false; style.center = false;
break; break;
case 'image':
image = {};
break;
} }
}; };
@@ -383,7 +406,6 @@ export default class BookParser {
onStartNode, onEndNode, onTextNode onStartNode, onEndNode, onTextNode
}); });
//длинные слова (или белиберду без пробелов) тоже разобьем //длинные слова (или белиберду без пробелов) тоже разобьем
const maxWordLength = this.maxWordLength; const maxWordLength = this.maxWordLength;
const parts = result; const parts = result;
@@ -398,7 +420,7 @@ export default class BookParser {
if (i - spaceIndex >= maxWordLength && i < p.text.length - 1 && if (i - spaceIndex >= maxWordLength && i < p.text.length - 1 &&
this.measureText(p.text.substr(spaceIndex + 1, i - spaceIndex), p.style) >= this.w - this.p) { this.measureText(p.text.substr(spaceIndex + 1, i - spaceIndex), p.style) >= this.w - this.p) {
result.push({style: p.style, text: p.text.substr(0, i + 1)}); result.push({style: p.style, image: p.image, text: p.text.substr(0, i + 1)});
p = {style: p.style, text: p.text.substr(i + 1)}; p = {style: p.style, text: p.text.substr(i + 1)};
spaceIndex = -1; spaceIndex = -1;
i = -1; i = -1;