Работа над BookInfoDialog

This commit is contained in:
Book Pauk
2022-11-11 22:12:13 +07:00
parent a3190e4af3
commit 2dd67487dc
6 changed files with 54 additions and 54 deletions

View File

@@ -63,12 +63,11 @@ class Fb2Helper {
pickNode: route => route.indexOf('fictionbook/body') !== 0,
});
const desc = parser.$$('description').toObject();
const coverImage = parser.inspector(desc).$('description/title-info/coverpage/image');
const coverImage = parser.$$('/description/title-info/coverpage/image');
let cover = null;
let coverExt = '';
if (coverImage) {
if (coverImage.count) {
const coverAttrs = coverImage.attrs();
const href = coverAttrs[`${parser.xlinkNS}:href`];
let coverType = coverAttrs['content-type'];
@@ -79,24 +78,21 @@ class Fb2Helper {
const binaryId = (href[0] == '#' ? href.substring(1) : href);
//найдем нужный image
parser.$$('binary').eachSelf(node => {
for (const node of parser.$$array('/binary')) {
let attrs = node.attrs();
if (!attrs)
return;
attrs = Object.fromEntries(attrs);
if (attrs.id === binaryId) {
const textNode = new Fb2Parser(node.value);
const base64 = textNode.$self('*TEXT').value;
const base64 = node.text();
cover = (base64 ? Buffer.from(base64, 'base64') : null);
}
});
}
}
}
parser.remove('binary');
return {fb2: parser.toObject(), cover, coverExt};
return {fb2: parser, cover, coverExt};
}
}

View File

@@ -3,7 +3,7 @@ const XmlParser = require('../xml/XmlParser');
class Fb2Parser extends XmlParser {
get xlinkNS() {
if (!this._xlinkNS) {
const rootAttrs = this.$self().attrs();
const rootAttrs = this.selectFirstSelf().attrs();
let ns = 'l';
for (const [key, value] of rootAttrs) {
if (value == 'http://www.w3.org/1999/xlink') {
@@ -18,27 +18,24 @@ class Fb2Parser extends XmlParser {
return this._xlinkNS;
}
bookInfo(fb2Object) {
bookInfo() {
const result = {};
if (!fb2Object)
fb2Object = this.toObject();
const desc = this.inspector(fb2Object).$('fictionbook/description');
const desc = this.$$('/description/');
if (!desc)
return result;
const parseAuthors = (node, tagName) => {
const authors = [];
for (const a of node.$$(tagName)) {
for (const a of node.$$array(tagName)) {
let names = [];
names.push(a.text('last-name'));
names.push(a.text('first-name'));
names.push(a.text('middle-name'));
names.push(a.text('/last-name'));
names.push(a.text('/first-name'));
names.push(a.text('/middle-name'));
names = names.filter(n => n);
if (!names.length)
names.push(a.text('nickname'));
names.push(a.text('/nickname'));
authors.push(names.join(' '));
}
@@ -48,7 +45,7 @@ class Fb2Parser extends XmlParser {
const parseSequence = (node, tagName) => {
const sequence = [];
for (const s of node.$$(tagName)) {
for (const s of node.$$array(tagName)) {
const seqAttrs = s.attrs() || {};
const name = seqAttrs['name'] || null;
const num = seqAttrs['number'] || null;
@@ -64,7 +61,7 @@ class Fb2Parser extends XmlParser {
const info = {};
info.genre = [];
for (const g of titleInfo.$$('genre'))
for (const g of titleInfo.$$array('genre'))
info.genre.push(g.text());
info.author = parseAuthors(titleInfo, 'author');
@@ -77,7 +74,7 @@ class Fb2Parser extends XmlParser {
info.annotationHtml = null;
if (info.annotation) {
//annotation как кусок xml
info.annotationXml = (new XmlParser()).fromObject(info.annotation).toString({noHeader: true});
info.annotationXml = titleInfo.$$('annotation/').toString({noHeader: true});
//annotation как html
info.annotationHtml = this.toHtml(info.annotationXml);
@@ -97,19 +94,19 @@ class Fb2Parser extends XmlParser {
}
//title-info
const titleInfo = desc.$('title-info');
const titleInfo = desc.$$('title-info/');
if (titleInfo) {
result.titleInfo = parseTitleInfo(titleInfo);
}
//src-title-info
const srcTitleInfo = desc.$('src-title-info');
const srcTitleInfo = desc.$$('src-title-info/');
if (srcTitleInfo) {
result.srcTitleInfo = parseTitleInfo(srcTitleInfo);
}
//document-info
const documentInfo = desc.$('document-info');
const documentInfo = desc.$$('document-info/');
if (documentInfo) {
const info = {};
@@ -118,7 +115,7 @@ class Fb2Parser extends XmlParser {
info.date = documentInfo.text('date');
info.srcUrl = [];
for (const url of documentInfo.$$('src-url'))
for (const url of documentInfo.$$array('src-url'))
info.srcUrl.push(url.text());
info.srcOcr = documentInfo.text('src-ocr');
@@ -131,7 +128,7 @@ class Fb2Parser extends XmlParser {
info.historyHtml = null;
if (info.history) {
//history как кусок xml
info.historyXml = (new XmlParser()).fromObject(info.history).toString({noHeader: true});
info.historyXml = documentInfo.$$('history/').toString({noHeader: true});
//history как html
info.historyHtml = this.toHtml(info.historyXml);
@@ -143,7 +140,7 @@ class Fb2Parser extends XmlParser {
}
//publish-info
const publishInfo = desc.$('publish-info');
const publishInfo = desc.$$('publish-info/');
if (publishInfo) {
const info = {};
@@ -160,7 +157,7 @@ class Fb2Parser extends XmlParser {
return result;
}
bookInfoList(fb2Object, options = {}) {
bookInfoList(bookInfo, options = {}) {
let {
correctMapping = false,
valueToString = false,
@@ -236,7 +233,7 @@ class Fb2Parser extends XmlParser {
];
mapping = correctMapping(mapping);
const bookInfo = this.bookInfo(fb2Object);
bookInfo = (bookInfo ? bookInfo : this.bookInfo());
//заполняем mapping
let result = [];