|
@@ -17,33 +17,139 @@ export default class BookParser {
|
|
throw new Error('Неверный формат файла');
|
|
throw new Error('Неверный формат файла');
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ let path = '';
|
|
|
|
+ let tag = '';
|
|
let nextPerc = 0;
|
|
let nextPerc = 0;
|
|
|
|
|
|
- /*
|
|
|
|
- firstName = book.get('//FictionBook:first-name', nameSpace).text();
|
|
|
|
- lastName = book.get('//FictionBook:last-name', nameSpace).text();
|
|
|
|
- genre = book.get('//FictionBook:genre', nameSpace).text();
|
|
|
|
- date = book.get('//FictionBook:date', nameSpace).text();
|
|
|
|
- bookTitle = book.get('//FictionBook:book-title', nameSpace).text();
|
|
|
|
- annotation = book.get('//FictionBook:annotation', nameSpace).text();
|
|
|
|
- id = book.get('//FictionBook:id', nameSpace).text();
|
|
|
|
|
|
+ let paraIndex = -1;
|
|
|
|
+ let paraOffset = 0;
|
|
|
|
+ let para = []; /*array of
|
|
|
|
+ {
|
|
|
|
+ index: Number,
|
|
|
|
+ offset: Number, //сумма всех length до этого параграфа
|
|
|
|
+ length: Number, //длина text без тегов
|
|
|
|
+ text: String //текст параграфа (или title или epigraph и т.д) с вложенными тегами
|
|
|
|
+ }
|
|
*/
|
|
*/
|
|
|
|
+ const newParagraph = (text, len) => {
|
|
|
|
+ paraIndex++;
|
|
|
|
+ let p = {
|
|
|
|
+ index: paraIndex,
|
|
|
|
+ offset: paraOffset,
|
|
|
|
+ length: len,
|
|
|
|
+ text: text
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ para[paraIndex] = p;
|
|
|
|
+ paraOffset += p.length;
|
|
|
|
+ };
|
|
|
|
+ const growParagraph = (text, len) => {
|
|
|
|
+ let p = para[paraIndex];
|
|
|
|
+ if (p) {
|
|
|
|
+ paraOffset -= p.length;
|
|
|
|
+ if (p.text == ' ') {
|
|
|
|
+ p.length = 0;
|
|
|
|
+ p.text = '';
|
|
|
|
+ }
|
|
|
|
+ p.length += len;
|
|
|
|
+ p.text += text;
|
|
|
|
+ } else {
|
|
|
|
+ p = {
|
|
|
|
+ index: paraIndex,
|
|
|
|
+ offset: paraOffset,
|
|
|
|
+ length: len,
|
|
|
|
+ text: text
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ para[paraIndex] = p;
|
|
|
|
+ paraOffset += p.length;
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ let fb2 = {};
|
|
|
|
+
|
|
const parser = this.parser;
|
|
const parser = this.parser;
|
|
- let result = {};
|
|
|
|
|
|
|
|
parser.on('error', (msgError) => {// eslint-disable-line no-unused-vars
|
|
parser.on('error', (msgError) => {// eslint-disable-line no-unused-vars
|
|
});
|
|
});
|
|
|
|
|
|
parser.on('startNode', (elemName, getAttr, isTagEnd, getStrNode) => {// eslint-disable-line no-unused-vars
|
|
parser.on('startNode', (elemName, getAttr, isTagEnd, getStrNode) => {// eslint-disable-line no-unused-vars
|
|
- //console.log(elemName, ' start');
|
|
|
|
|
|
+ tag = elemName;
|
|
|
|
+ path += '/' + elemName;
|
|
|
|
+
|
|
|
|
+ if ((tag == 'p' || tag == 'empty-line') && path.indexOf('/FictionBook/body/section') == 0) {
|
|
|
|
+ newParagraph(' ', 1);
|
|
|
|
+ }
|
|
});
|
|
});
|
|
|
|
|
|
parser.on('endNode', (elemName, isTagStart, getStrNode) => {// eslint-disable-line no-unused-vars
|
|
parser.on('endNode', (elemName, isTagStart, getStrNode) => {// eslint-disable-line no-unused-vars
|
|
- //console.log(elemName, ' end');
|
|
|
|
|
|
+ if (tag == elemName) {
|
|
|
|
+ path = path.substr(0, path.length - tag.length - 1);
|
|
|
|
+ let i = path.lastIndexOf('/');
|
|
|
|
+ if (i >= 0) {
|
|
|
|
+ tag = path.substr(i + 1);
|
|
|
|
+ } else {
|
|
|
|
+ tag = path;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
});
|
|
});
|
|
|
|
|
|
- parser.on('textNode', (text) => {// eslint-disable-line no-unused-vars
|
|
|
|
- //console.log(text);
|
|
|
|
|
|
+ parser.on('textNode', (text) => {
|
|
|
|
+ text = text.trim();
|
|
|
|
+
|
|
|
|
+ switch (path) {
|
|
|
|
+ case '/FictionBook/description/title-info/author/first-name':
|
|
|
|
+ fb2.firstName = text;
|
|
|
|
+ break;
|
|
|
|
+ case '/FictionBook/description/title-info/author/last-name':
|
|
|
|
+ fb2.lastName = text;
|
|
|
|
+ break;
|
|
|
|
+ case '/FictionBook/description/title-info/genre':
|
|
|
|
+ fb2.genre = text;
|
|
|
|
+ break;
|
|
|
|
+ case '/FictionBook/description/title-info/date':
|
|
|
|
+ fb2.date = text;
|
|
|
|
+ break;
|
|
|
|
+ case '/FictionBook/description/title-info/book-title':
|
|
|
|
+ fb2.bookTitle = text;
|
|
|
|
+ break;
|
|
|
|
+ case '/FictionBook/description/title-info/id':
|
|
|
|
+ fb2.id = text;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (path.indexOf('/FictionBook/description/title-info/annotation') == 0) {
|
|
|
|
+ if (!fb2.annotation)
|
|
|
|
+ fb2.annotation = '';
|
|
|
|
+ if (tag != 'annotation')
|
|
|
|
+ fb2.annotation += `<${tag}>${text}</${tag}>`;
|
|
|
|
+ else
|
|
|
|
+ fb2.annotation += text;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (text == '')
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (path.indexOf('/FictionBook/body/title') == 0) {
|
|
|
|
+ newParagraph(text, text.length);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (text == '')
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (path.indexOf('/FictionBook/body/section') == 0) {
|
|
|
|
+ switch (tag) {
|
|
|
|
+ case 'p':
|
|
|
|
+ growParagraph(text, text.length);
|
|
|
|
+ break;
|
|
|
|
+ case 'section':
|
|
|
|
+ case 'title':
|
|
|
|
+ newParagraph(text, text.length);
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ growParagraph(`<${tag}>${text}</${tag}>`, text.length);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
});
|
|
});
|
|
|
|
|
|
parser.on('cdata', (data) => {// eslint-disable-line no-unused-vars
|
|
parser.on('cdata', (data) => {// eslint-disable-line no-unused-vars
|
|
@@ -61,9 +167,11 @@ export default class BookParser {
|
|
});
|
|
});
|
|
|
|
|
|
await parser.parse(data);
|
|
await parser.parse(data);
|
|
- if (callback)
|
|
|
|
- callback(100);
|
|
|
|
|
|
|
|
- return result;
|
|
|
|
|
|
+ this.meta = fb2;
|
|
|
|
+ this.para = para;
|
|
|
|
+
|
|
|
|
+ callback(100);
|
|
|
|
+ return {fb2};
|
|
}
|
|
}
|
|
}
|
|
}
|