Добавил загрузку внешних изображений
This commit is contained in:
@@ -103,9 +103,8 @@ export default class DrawHelper {
|
|||||||
//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},
|
||||||
const img = part.image;
|
const img = part.image;
|
||||||
if (img && img.id && !img.inline && !imageDrawn.has(img.paraIndex)) {
|
if (img && img.id && !img.inline && !imageDrawn.has(img.paraIndex)) {
|
||||||
if (img.local) {
|
const bin = this.parsed.binary[img.id];
|
||||||
const bin = this.parsed.binary[img.id];
|
if (bin) {
|
||||||
|
|
||||||
let imgH = img.lineCount*this.lineHeight;
|
let imgH = img.lineCount*this.lineHeight;
|
||||||
imgH = (imgH <= bin.h ? imgH : bin.h);
|
imgH = (imgH <= bin.h ? imgH : bin.h);
|
||||||
let imgW = bin.w;
|
let imgW = bin.w;
|
||||||
@@ -118,9 +117,11 @@ export default class DrawHelper {
|
|||||||
|
|
||||||
const left = (this.w - imgW)/2;
|
const left = (this.w - imgW)/2;
|
||||||
const top = ((img.lineCount*this.lineHeight - imgH)/2) + (i - img.imageLine)*this.lineHeight;
|
const top = ((img.lineCount*this.lineHeight - imgH)/2) + (i - img.imageLine)*this.lineHeight;
|
||||||
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
|
if (img.local) {
|
||||||
} else {
|
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
|
||||||
//
|
} else {
|
||||||
|
lineText += `<img src="${img.id}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
imageDrawn.add(img.paraIndex);
|
imageDrawn.add(img.paraIndex);
|
||||||
}
|
}
|
||||||
@@ -128,11 +129,13 @@ export default class DrawHelper {
|
|||||||
if (img && img.id && img.inline) {
|
if (img && img.id && img.inline) {
|
||||||
if (img.local) {
|
if (img.local) {
|
||||||
const bin = this.parsed.binary[img.id];
|
const bin = this.parsed.binary[img.id];
|
||||||
let resize = '';
|
if (bin) {
|
||||||
if (bin.h > this.fontSize) {
|
let resize = '';
|
||||||
resize = `height: ${this.fontSize - 3}px`;
|
if (bin.h > this.fontSize) {
|
||||||
|
resize = `height: ${this.fontSize - 3}px`;
|
||||||
|
}
|
||||||
|
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="${resize}"/>`;
|
||||||
}
|
}
|
||||||
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="${resize}"/>`;
|
|
||||||
} else {
|
} else {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,6 +85,26 @@ export default class BookParser {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getExternalImageDimensions = (src) => {
|
||||||
|
return new Promise (async(resolve, reject) => {
|
||||||
|
const i = new Image();
|
||||||
|
let resolved = false;
|
||||||
|
i.onload = () => {
|
||||||
|
resolved = true;
|
||||||
|
this.binary[src] = {
|
||||||
|
w: i.width,
|
||||||
|
h: i.height,
|
||||||
|
};
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
|
||||||
|
i.src = src;
|
||||||
|
await sleep(30*1000);
|
||||||
|
if (!resolved)
|
||||||
|
reject('Не удалось получить размер изображения');
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const newParagraph = (text, len, addIndex) => {
|
const newParagraph = (text, len, addIndex) => {
|
||||||
paraIndex++;
|
paraIndex++;
|
||||||
let p = {
|
let p = {
|
||||||
@@ -147,21 +167,26 @@ export default class BookParser {
|
|||||||
|
|
||||||
if (tag == 'binary') {
|
if (tag == 'binary') {
|
||||||
let attrs = sax.getAttrsSync(tail);
|
let attrs = sax.getAttrsSync(tail);
|
||||||
binaryType = (attrs['content-type'].value ? attrs['content-type'].value : '');
|
binaryType = (attrs['content-type'] && attrs['content-type'].value ? attrs['content-type'].value : '');
|
||||||
if (binaryType == 'image/jpeg' || binaryType == 'image/png')
|
if (binaryType == 'image/jpeg' || binaryType == 'image/png')
|
||||||
binaryId = (attrs.id.value ? attrs.id.value : '');
|
binaryId = (attrs.id.value ? attrs.id.value : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tag == 'image') {
|
if (tag == 'image') {
|
||||||
let attrs = sax.getAttrsSync(tail);
|
let attrs = sax.getAttrsSync(tail);
|
||||||
if (attrs.href.value) {
|
if (attrs.href && attrs.href.value) {
|
||||||
if (inPara && !this.showInlineImagesInCenter)
|
const href = attrs.href.value;
|
||||||
growParagraph(`<image-inline href="${attrs.href.value}"></image-inline>`, 0);
|
if (href[0] == '#') {//local
|
||||||
else
|
if (inPara && !this.showInlineImagesInCenter)
|
||||||
newParagraph(`<image href="${attrs.href.value}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
|
growParagraph(`<image-inline href="${href}"></image-inline>`, 0);
|
||||||
|
else
|
||||||
if (inPara && this.showInlineImagesInCenter)
|
newParagraph(`<image href="${href}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
|
||||||
newParagraph(' ', 1);
|
if (inPara && this.showInlineImagesInCenter)
|
||||||
|
newParagraph(' ', 1);
|
||||||
|
} else {//external
|
||||||
|
dimPromises.push(getExternalImageDimensions(href));
|
||||||
|
newParagraph(`<image href="${href}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -409,14 +434,14 @@ export default class BookParser {
|
|||||||
break;
|
break;
|
||||||
case 'space': {
|
case 'space': {
|
||||||
let attrs = sax.getAttrsSync(tail);
|
let attrs = sax.getAttrsSync(tail);
|
||||||
if (attrs.w.value)
|
if (attrs.w && attrs.w.value)
|
||||||
style.space = attrs.w.value;
|
style.space = attrs.w.value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'image': {
|
case 'image': {
|
||||||
let attrs = sax.getAttrsSync(tail);
|
let attrs = sax.getAttrsSync(tail);
|
||||||
let id = attrs.href.value;
|
if (attrs.href && attrs.href.value) {
|
||||||
if (id) {
|
let id = attrs.href.value;
|
||||||
let local = false;
|
let local = false;
|
||||||
if (id[0] == '#') {
|
if (id[0] == '#') {
|
||||||
id = id.substr(1);
|
id = id.substr(1);
|
||||||
@@ -428,8 +453,8 @@ export default class BookParser {
|
|||||||
}
|
}
|
||||||
case 'image-inline': {
|
case 'image-inline': {
|
||||||
let attrs = sax.getAttrsSync(tail);
|
let attrs = sax.getAttrsSync(tail);
|
||||||
let id = attrs.href.value;
|
if (attrs.href && attrs.href.value) {
|
||||||
if (id) {
|
let id = attrs.href.value;
|
||||||
let local = false;
|
let local = false;
|
||||||
if (id[0] == '#') {
|
if (id[0] == '#') {
|
||||||
id = id.substr(1);
|
id = id.substr(1);
|
||||||
@@ -617,6 +642,8 @@ export default class BookParser {
|
|||||||
if (part.image.id && !part.image.inline) {
|
if (part.image.id && !part.image.inline) {
|
||||||
parsed.visible = this.showImages;
|
parsed.visible = this.showImages;
|
||||||
const bin = this.binary[part.image.id];
|
const bin = this.binary[part.image.id];
|
||||||
|
if (!bin)
|
||||||
|
continue;
|
||||||
|
|
||||||
let lineCount = this.imageHeightLines;
|
let lineCount = this.imageHeightLines;
|
||||||
const c = Math.ceil(bin.h/this.lineHeight);
|
const c = Math.ceil(bin.h/this.lineHeight);
|
||||||
@@ -648,10 +675,12 @@ export default class BookParser {
|
|||||||
|
|
||||||
if (part.image.id && part.image.inline && this.showImages) {
|
if (part.image.id && part.image.inline && this.showImages) {
|
||||||
const bin = this.binary[part.image.id];
|
const bin = this.binary[part.image.id];
|
||||||
let imgH = (bin.h > this.fontSize ? this.fontSize : bin.h);
|
if (bin) {
|
||||||
imgW += bin.w*imgH/bin.h;
|
let imgH = (bin.h > this.fontSize ? this.fontSize : bin.h);
|
||||||
line.parts.push({style, text: '',
|
imgW += bin.w*imgH/bin.h;
|
||||||
image: {local: part.image.local, inline: true, id: part.image.id}});
|
line.parts.push({style, text: '',
|
||||||
|
image: {local: part.image.local, inline: true, id: part.image.id}});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let words = part.text.split(' ');
|
let words = part.text.split(' ');
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ const textUtils = require('./textUtils');
|
|||||||
const FileDetector = require('../FileDetector');
|
const FileDetector = require('../FileDetector');
|
||||||
|
|
||||||
const repSpaces = (text) => text.replace(/ |[\t\n\r]/g, ' ');
|
const repSpaces = (text) => text.replace(/ |[\t\n\r]/g, ' ');
|
||||||
|
const repSpaces2 = (text) => text.replace(/[\n\r]/g, '');
|
||||||
|
|
||||||
class BookConverter {
|
class BookConverter {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -31,7 +32,7 @@ class BookConverter {
|
|||||||
if (parsedUrl.hostname == 'samlib.ru' ||
|
if (parsedUrl.hostname == 'samlib.ru' ||
|
||||||
parsedUrl.hostname == 'budclub.ru' ||
|
parsedUrl.hostname == 'budclub.ru' ||
|
||||||
parsedUrl.hostname == 'zhurnal.lib.ru') {
|
parsedUrl.hostname == 'zhurnal.lib.ru') {
|
||||||
await fs.writeFile(outputFile, this.convertSamlib(data));
|
await fs.writeFile(outputFile, this.convertSamlib(data, parsedUrl.hostname));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,7 +217,7 @@ class BookConverter {
|
|||||||
return this.formatFb2(fb2);
|
return this.formatFb2(fb2);
|
||||||
}
|
}
|
||||||
|
|
||||||
convertSamlib(data) {
|
convertSamlib(data, hostname) {
|
||||||
let titleInfo = {};
|
let titleInfo = {};
|
||||||
let desc = {_n: 'description', 'title-info': titleInfo};
|
let desc = {_n: 'description', 'title-info': titleInfo};
|
||||||
let pars = [];
|
let pars = [];
|
||||||
@@ -225,6 +226,7 @@ class BookConverter {
|
|||||||
|
|
||||||
let inSubtitle = false;
|
let inSubtitle = false;
|
||||||
let inJustify = true;
|
let inJustify = true;
|
||||||
|
let inImage = false;
|
||||||
let path = '';
|
let path = '';
|
||||||
let tag = '';// eslint-disable-line no-unused-vars
|
let tag = '';// eslint-disable-line no-unused-vars
|
||||||
|
|
||||||
@@ -235,10 +237,10 @@ class BookConverter {
|
|||||||
let italic = false;
|
let italic = false;
|
||||||
let bold = false;
|
let bold = false;
|
||||||
|
|
||||||
const openTag = (name) => {
|
const openTag = (name, attrs) => {
|
||||||
if (name == 'p')
|
if (name == 'p')
|
||||||
inPara = true;
|
inPara = true;
|
||||||
let n = {_n: name, _a: [], _p: node};
|
let n = {_n: name, _attrs: attrs, _a: [], _p: node};
|
||||||
node._a.push(n);
|
node._a.push(n);
|
||||||
node = n;
|
node = n;
|
||||||
};
|
};
|
||||||
@@ -269,7 +271,7 @@ class BookConverter {
|
|||||||
path += '/' + elemName;
|
path += '/' + elemName;
|
||||||
tag = elemName;
|
tag = elemName;
|
||||||
} else {
|
} else {
|
||||||
if (inPara && elemName != 'i' && elemName != 'b')
|
if (inPara && elemName != 'i' && elemName != 'b' && elemName != 'img')
|
||||||
closeTag('p');
|
closeTag('p');
|
||||||
|
|
||||||
switch (elemName) {
|
switch (elemName) {
|
||||||
@@ -299,6 +301,17 @@ class BookConverter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
case 'img': {
|
||||||
|
const attrs = sax.getAttrsSync(tail);
|
||||||
|
if (attrs.src && attrs.src.value) {
|
||||||
|
let href = attrs.src.value;
|
||||||
|
if (href[0] == '/')
|
||||||
|
href = `http://${hostname}${href}`;
|
||||||
|
openTag('image', {href});
|
||||||
|
inImage = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -346,6 +359,11 @@ class BookConverter {
|
|||||||
inJustify = false;
|
inJustify = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'img':
|
||||||
|
if (inImage)
|
||||||
|
closeTag('image');
|
||||||
|
inImage = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -390,7 +408,7 @@ class BookConverter {
|
|||||||
growParagraph(`${tOpen}${text}${tClose}`);
|
growParagraph(`${tOpen}${text}${tClose}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
sax.parseSync(repSpaces(this.decode(data).toString()), {
|
sax.parseSync(repSpaces(repSpaces2(this.decode(data).toString())), {
|
||||||
onStartNode, onEndNode, onTextNode, onComment,
|
onStartNode, onEndNode, onTextNode, onComment,
|
||||||
innerCut: new Set(['head', 'script', 'style'])
|
innerCut: new Set(['head', 'script', 'style'])
|
||||||
});
|
});
|
||||||
@@ -437,8 +455,15 @@ class BookConverter {
|
|||||||
if (node._n)
|
if (node._n)
|
||||||
name = node._n;
|
name = node._n;
|
||||||
|
|
||||||
|
let attrs = '';
|
||||||
|
if (node._attrs) {
|
||||||
|
for (let attrName in node._attrs) {
|
||||||
|
attrs += ` ${attrName}="${node._attrs[attrName]}"`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (name)
|
if (name)
|
||||||
out += `<${name}>`;
|
out += `<${name}${attrs}>`;
|
||||||
if (node.hasOwnProperty('_t'))
|
if (node.hasOwnProperty('_t'))
|
||||||
out += repSpaces(node._t);
|
out += repSpaces(node._t);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user