Рефакторинг
This commit is contained in:
@@ -5,6 +5,7 @@ const he = require('he');
|
|||||||
const LimitedQueue = require('../../LimitedQueue');
|
const LimitedQueue = require('../../LimitedQueue');
|
||||||
const textUtils = require('./textUtils');
|
const textUtils = require('./textUtils');
|
||||||
const utils = require('../../utils');
|
const utils = require('../../utils');
|
||||||
|
const xmlParser = require('../../xmlParser');
|
||||||
|
|
||||||
const queue = new LimitedQueue(3, 20, 2*60*1000);//2 минуты ожидание подвижек
|
const queue = new LimitedQueue(3, 20, 2*60*1000);//2 минуты ожидание подвижек
|
||||||
|
|
||||||
@@ -102,61 +103,14 @@ class ConvertBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
formatFb2(fb2) {
|
formatFb2(fb2) {
|
||||||
let out = '<?xml version="1.0" encoding="utf-8"?>';
|
const out = xmlParser.formatXml({
|
||||||
out += '<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">';
|
FictionBook: {
|
||||||
out += this.formatFb2Node(fb2);
|
_attrs: {xmlns: 'http://www.gribuser.ru/xml/fictionbook/2.0', 'xmlns:l': 'http://www.w3.org/1999/xlink'},
|
||||||
out += '</FictionBook>';
|
_a: [fb2],
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
formatFb2Node(node, name) {
|
|
||||||
let out = '';
|
|
||||||
|
|
||||||
if (Array.isArray(node)) {
|
|
||||||
for (const n of node) {
|
|
||||||
out += this.formatFb2Node(n);
|
|
||||||
}
|
}
|
||||||
} else if (typeof node == 'string') {
|
}, 'utf-8', this.repSpaces);
|
||||||
if (name)
|
|
||||||
out += `<${name}>${this.repSpaces(node)}</${name}>`;
|
|
||||||
else
|
|
||||||
out += this.repSpaces(node);
|
|
||||||
} else {
|
|
||||||
if (node._n)
|
|
||||||
name = node._n;
|
|
||||||
|
|
||||||
let attrs = '';
|
return out.replace(/<p>\s*?<\/p>/g, '<empty-line/>');
|
||||||
if (node._attrs) {
|
|
||||||
for (let attrName in node._attrs) {
|
|
||||||
attrs += ` ${attrName}="${node._attrs[attrName]}"`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let tOpen = '';
|
|
||||||
let tBody = '';
|
|
||||||
let tClose = '';
|
|
||||||
if (name)
|
|
||||||
tOpen += `<${name}${attrs}>`;
|
|
||||||
if (node.hasOwnProperty('_t'))
|
|
||||||
tBody += this.repSpaces(node._t);
|
|
||||||
|
|
||||||
for (let nodeName in node) {
|
|
||||||
if (nodeName && nodeName[0] == '_' && nodeName != '_a')
|
|
||||||
continue;
|
|
||||||
|
|
||||||
const n = node[nodeName];
|
|
||||||
tBody += this.formatFb2Node(n, nodeName);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name)
|
|
||||||
tClose += `</${name}>`;
|
|
||||||
|
|
||||||
if (attrs == '' && name == 'p' && tBody.trim() == '')
|
|
||||||
out += '<empty-line/>'
|
|
||||||
else
|
|
||||||
out += `${tOpen}${tBody}${tClose}`;
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user