Улучшено распознавание xml-формата

This commit is contained in:
Book Pauk
2020-12-15 15:04:30 +07:00
parent b1f5f3dd28
commit 8ef6551560
3 changed files with 10 additions and 2 deletions

View File

@@ -103,6 +103,11 @@ class ConvertBase {
return he.escape(he.decode(text.replace(/ /g, ' '))); return he.escape(he.decode(text.replace(/ /g, ' ')));
} }
isDataXml(data) {
const str = data.toString().trim();
return (str.indexOf('<?xml version="1.0"') == 0 || str.indexOf('<?xml version=\'1.0\'') == 0 );
}
formatFb2(fb2) { formatFb2(fb2) {
const out = xmlParser.formatXml({ const out = xmlParser.formatXml({
FictionBook: { FictionBook: {

View File

@@ -6,7 +6,10 @@ class ConvertFb2 extends ConvertBase {
check(data, opts) { check(data, opts) {
const {dataType} = opts; const {dataType} = opts;
return (dataType && dataType.ext == 'xml' && data.toString().indexOf('<FictionBook') >= 0); return (
( (dataType && dataType.ext == 'xml') || this.isDataXml(data) ) &&
data.toString().indexOf('<FictionBook') >= 0
);
} }
async run(data, opts) { async run(data, opts) {

View File

@@ -7,7 +7,7 @@ class ConvertHtml extends ConvertBase {
const {dataType} = opts; const {dataType} = opts;
//html? //html?
if (dataType && (dataType.ext == 'html' || dataType.ext == 'xml')) if ( ( (dataType && (dataType.ext == 'html' || dataType.ext == 'xml')) ) || this.isDataXml(data) )
return {isText: false}; return {isText: false};
//может это чистый текст? //может это чистый текст?