Доделал в начальном виде fb2-парсер
This commit is contained in:
@@ -78,6 +78,12 @@ class Reader extends Vue {
|
|||||||
this.$root.addKeyHook(this.keyHook);
|
this.$root.addKeyHook(this.keyHook);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mounted() {
|
||||||
|
/*while (this.lastOpenedBook) {
|
||||||
|
this.commit('reader/delOpenedBook', this.lastOpenedBook);
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
get loaderActive() {
|
get loaderActive() {
|
||||||
return this.reader.loaderActive;
|
return this.reader.loaderActive;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,33 +17,139 @@ export default class BookParser {
|
|||||||
throw new Error('Неверный формат файла');
|
throw new Error('Неверный формат файла');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let path = '';
|
||||||
|
let tag = '';
|
||||||
let nextPerc = 0;
|
let nextPerc = 0;
|
||||||
|
|
||||||
/*
|
let paraIndex = -1;
|
||||||
firstName = book.get('//FictionBook:first-name', nameSpace).text();
|
let paraOffset = 0;
|
||||||
lastName = book.get('//FictionBook:last-name', nameSpace).text();
|
let para = []; /*array of
|
||||||
genre = book.get('//FictionBook:genre', nameSpace).text();
|
{
|
||||||
date = book.get('//FictionBook:date', nameSpace).text();
|
index: Number,
|
||||||
bookTitle = book.get('//FictionBook:book-title', nameSpace).text();
|
offset: Number, //сумма всех length до этого параграфа
|
||||||
annotation = book.get('//FictionBook:annotation', nameSpace).text();
|
length: Number, //длина text без тегов
|
||||||
id = book.get('//FictionBook:id', nameSpace).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
|
parser.on('textNode', (text) => {
|
||||||
//console.log(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};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user