From 2f8b68ec62e42eab9c6ed3915dcc61fbed0bd05c Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Mon, 4 Mar 2019 21:22:12 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D0=B0=D1=80=D1=81=D0=B8=D0=BD=D0=B3=D0=B0?= =?UTF-8?q?=20Pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/BookConverter/ConvertHtml.js | 30 ++++++++++++++++++++++-- server/core/BookConverter/ConvertPdf.js | 4 ++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/server/core/BookConverter/ConvertHtml.js b/server/core/BookConverter/ConvertHtml.js index ce28f9d4..0ba57090 100644 --- a/server/core/BookConverter/ConvertHtml.js +++ b/server/core/BookConverter/ConvertHtml.js @@ -34,10 +34,13 @@ class ConvertHtml extends ConvertBase { let desc = {_n: 'description', 'title-info': titleInfo}; let pars = []; let body = {_n: 'body', section: {_a: []}}; - let fb2 = [desc, body]; + let binary = []; + let fb2 = [desc, body, binary]; let title = ''; let inTitle = false; + let inImage = false; + let image = {}; let spaceCounter = []; @@ -80,6 +83,15 @@ class ConvertHtml extends ConvertBase { if (inTitle && !title) title = text; + + if (inImage) { + image._t = text; + binary.push(image); + + pars.push({_n: 'image', _attrs: {'l:href': '#' + image._attrs.id}, _t: ''}); + newParagraph(); + } + }; const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars @@ -90,18 +102,27 @@ class ConvertHtml extends ConvertBase { if (tag == 'title') inTitle = true; + + if (tag == 'fb2-image') { + inImage = true; + const attrs = sax.getAttrsSync(tail); + image = {_n: 'binary', _attrs: {id: attrs.name.value, 'content-type': attrs.type.value}, _t: ''}; + } }; const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars if (tag == 'title') inTitle = false; + + if (tag == 'fb2-image') + inImage = false; }; let buf = this.decode(data).toString(); sax.parseSync(buf, { onStartNode, onEndNode, onTextNode, - innerCut: new Set(['head', 'script', 'style', 'binary']) + innerCut: new Set(['head', 'script', 'style', 'binary', 'fb2-image']) }); titleInfo['book-title'] = title; @@ -148,6 +169,11 @@ class ConvertHtml extends ConvertBase { i = 0; for (const par of pars) { + if (par._n != 'p') { + newPars.push(par); + continue; + } + if (i > 0) newPar(); i++; diff --git a/server/core/BookConverter/ConvertPdf.js b/server/core/BookConverter/ConvertPdf.js index f323d987..afcf5c6f 100644 --- a/server/core/BookConverter/ConvertPdf.js +++ b/server/core/BookConverter/ConvertPdf.js @@ -86,7 +86,7 @@ class ConvertPdf extends ConvertHtml { height: parseInt((attrs.height && attrs.height.value ? attrs.height.value : null), 10), }; - if (line.width !== '0' || line.height !== '0') { + if (line.width != 0 || line.height != 0) { inText = true; if (isNaN(line.top) || isNaN(prevTop) || (Math.abs(prevTop - line.top) > 3)) { putImage(line.top); @@ -98,7 +98,7 @@ class ConvertPdf extends ConvertHtml { } if (tag == 'image') { - let attrs = sax.getAttrsSync(tail); + const attrs = sax.getAttrsSync(tail); const src = (attrs.src && attrs.src.value ? attrs.src.value : ''); if (src) { const image = {