Доделал в начальном виде распознавание текста страницы самлиб
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
const fs = require('fs-extra');
|
||||
const FileDetector = require('../FileDetector');
|
||||
const URL = require('url').URL;
|
||||
const EasySAXParser = require('./easysax');
|
||||
const iconv = require('iconv-lite');
|
||||
const chardet = require('chardet');
|
||||
const _ = require('lodash');
|
||||
|
||||
const FileDetector = require('../FileDetector');
|
||||
const EasySAXParser = require('./easysaxmod');
|
||||
|
||||
class BookConverter {
|
||||
constructor() {
|
||||
@@ -12,9 +16,9 @@ class BookConverter {
|
||||
const fileType = await this.detector.detectFile(inputFile);
|
||||
|
||||
if (fileType && (fileType.ext == 'html' || fileType.ext == 'xml')) {
|
||||
const data = await fs.readFile(inputFile, 'utf8');
|
||||
const data = await fs.readFile(inputFile);
|
||||
|
||||
if (data.indexOf('<FictionBook') >= 0) {
|
||||
if (data.toString().indexOf('<FictionBook') >= 0) {
|
||||
await fs.writeFile(outputFile, data);
|
||||
return;
|
||||
}
|
||||
@@ -26,7 +30,6 @@ class BookConverter {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//Заглушка
|
||||
await fs.writeFile(outputFile, data);
|
||||
callback(100);
|
||||
@@ -39,12 +42,30 @@ class BookConverter {
|
||||
}
|
||||
|
||||
async convertSamlib(data) {
|
||||
let fb2 = [{parentName: 'description'}];
|
||||
let titleInfo = {};
|
||||
let desc = {_n: 'description', 'title-info': titleInfo};
|
||||
let pars = [];
|
||||
let body = {_n: 'body', section: {_a: [pars]}};
|
||||
let fb2 = [desc, body];
|
||||
|
||||
let path = '';
|
||||
let tag = '';
|
||||
let tag = '';// eslint-disable-line no-unused-vars
|
||||
|
||||
let inText = false;
|
||||
|
||||
const newParagraph = () => {
|
||||
pars.push({_n: 'p', _t: ''});
|
||||
};
|
||||
|
||||
const growParagraph = (text) => {
|
||||
const l = pars.length;
|
||||
if (l) {
|
||||
if (pars[l - 1]._t == '')
|
||||
text = text.trimLeft();
|
||||
pars[l - 1]._t += text;
|
||||
}
|
||||
};
|
||||
|
||||
const parser = new EasySAXParser();
|
||||
|
||||
parser.on('error', (msgError) => {// eslint-disable-line no-unused-vars
|
||||
@@ -57,7 +78,10 @@ class BookConverter {
|
||||
if (!inText) {
|
||||
path += '/' + elemName;
|
||||
tag = elemName;
|
||||
console.log(path);
|
||||
} else {
|
||||
if (elemName == 'p' || elemName == 'dd') {
|
||||
newParagraph();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
@@ -78,14 +102,28 @@ console.log(path);
|
||||
|
||||
let i = path.lastIndexOf('/');
|
||||
tag = path.substr(i + 1);
|
||||
|
||||
console.log('cl', elemName);
|
||||
console.log('tag', tag);
|
||||
console.log(path);
|
||||
}
|
||||
});
|
||||
|
||||
parser.on('textNode', (text) => {// eslint-disable-line no-unused-vars
|
||||
switch (path) {
|
||||
case '/html/body/center/h2':
|
||||
titleInfo['book-title'] = text;
|
||||
return;
|
||||
case '/html/body/div/h3':
|
||||
if (!titleInfo.author)
|
||||
titleInfo.author = {};
|
||||
text = text.replace(':', '').trim().split(' ');
|
||||
if (text[0])
|
||||
titleInfo.author['last-name'] = text[0];
|
||||
if (text[1])
|
||||
titleInfo.author['first-name'] = text[1];
|
||||
if (text[2])
|
||||
titleInfo.author['middle-name'] = text[2];
|
||||
return;
|
||||
}
|
||||
if (inText)
|
||||
growParagraph(text);
|
||||
});
|
||||
|
||||
parser.on('cdata', (data) => {// eslint-disable-line no-unused-vars
|
||||
@@ -100,7 +138,22 @@ console.log(path);
|
||||
});
|
||||
*/
|
||||
|
||||
await parser.parse(data);
|
||||
await parser.parse(iconv.decode(data, chardet.detect(data)));
|
||||
|
||||
const title = (titleInfo['book-title'] ? titleInfo['book-title'] : '');
|
||||
let author = '';
|
||||
if (titleInfo.author) {
|
||||
author = _.compact([
|
||||
(titleInfo.author['last-name'] ? titleInfo.author['last-name'] : ''),
|
||||
(titleInfo.author['first-name'] ? titleInfo.author['first-name'] : ''),
|
||||
(titleInfo.author['middle-name'] ? titleInfo.author['middle-name'] : ''),
|
||||
]).join(' ');
|
||||
}
|
||||
|
||||
pars.unshift({_n: 'title', _a: [
|
||||
{_n: 'p', _t: author}, {_n: 'p', _t: ''},
|
||||
{_n: 'p', _t: title}, {_n: 'p', _t: ''},
|
||||
]})
|
||||
|
||||
return this.formatFb2(fb2);
|
||||
}
|
||||
@@ -120,19 +173,25 @@ console.log(out);
|
||||
for (const n of node) {
|
||||
out += this.formatFb2Node(n);
|
||||
}
|
||||
} else if (typeof node == 'string') {
|
||||
out += `<${name}>${node}</${name}>`;
|
||||
} else {
|
||||
if (node.parentName)
|
||||
name = node.parentName;
|
||||
if (node._n)
|
||||
name = node._n;
|
||||
if (!name)
|
||||
throw new Error(`malformed fb2 object`);
|
||||
|
||||
out += `<${name}>`;
|
||||
for (let nodeName in node) {
|
||||
if (nodeName == 'parentName')
|
||||
continue;
|
||||
if (node.hasOwnProperty('_t')) {
|
||||
out += node._t;
|
||||
} else {
|
||||
for (let nodeName in node) {
|
||||
if (nodeName == '_n')
|
||||
continue;
|
||||
|
||||
const n = node[nodeName];
|
||||
out += this.formatFb2Node(n, nodeName);
|
||||
const n = node[nodeName];
|
||||
out += this.formatFb2Node(n, nodeName);
|
||||
}
|
||||
}
|
||||
out += `</${name}>`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user