Compare commits

...

32 Commits
0.4.1 ... 0.4.4

Author SHA1 Message Date
Book Pauk
c36e9b36d8 Merge branch 'release/0.4.4' 2019-02-24 00:41:48 +07:00
Book Pauk
ecc3acce93 Версия 0.4.4 2019-02-24 00:41:35 +07:00
Book Pauk
33a2ca55f0 Поправки 2019-02-24 00:32:19 +07:00
Book Pauk
9d0bbec4b3 Поправка отображения картинок внутри title 2019-02-24 00:00:55 +07:00
Book Pauk
4de0b3cffd Поправки парсера fb2 2019-02-23 23:51:06 +07:00
Book Pauk
06221a474b Мелкая поправка 2019-02-23 23:38:19 +07:00
Book Pauk
e99a42b7af К предыдущему 2019-02-23 23:20:03 +07:00
Book Pauk
37822e8409 Мелкая поправка 2019-02-23 23:18:03 +07:00
Book Pauk
2e477e6c99 Улучшение парсинга СИ 2019-02-23 23:03:01 +07:00
Book Pauk
360ee98d8d Улучшение парсинга fb2 2019-02-23 22:17:16 +07:00
Book Pauk
69afd7720a Поправки форматирования fb2 2019-02-23 20:45:21 +07:00
Book Pauk
a75590c493 Улучшение формирования fb2 2019-02-23 20:15:40 +07:00
Book Pauk
2acb65f6b3 Мелкие поправки 2019-02-23 20:04:41 +07:00
Book Pauk
1e1a58b58c Улучшен convertHtml 2019-02-23 19:47:16 +07:00
Book Pauk
aeadb5aeb8 Улучшено определение кодировки и текстового файла 2019-02-23 19:09:57 +07:00
Book Pauk
3e2f01d56d Мелкие поправки 2019-02-23 18:06:29 +07:00
Book Pauk
cad97e639a Добавил цвет фона 2019-02-23 14:37:40 +07:00
Book Pauk
e2632f1802 Добавил вывод версии в консоль 2019-02-23 14:24:29 +07:00
Book Pauk
9aa0bb2bde Merge tag '0.4.3' into develop
0.4.3
2019-02-23 14:17:13 +07:00
Book Pauk
f015d5f7ed Merge branch 'release/0.4.3' 2019-02-23 14:17:00 +07:00
Book Pauk
74f8f7f9a4 0.4.3 2019-02-23 14:16:43 +07:00
Book Pauk
2598538de9 Улучшил отзывчивость historyPage при открытии 2019-02-23 14:13:41 +07:00
Book Pauk
a78a00df2b Поправка label 2019-02-23 14:03:28 +07:00
Book Pauk
92f6beb64e Merge tag '0.4.2' into develop
0.4.2
2019-02-21 21:39:19 +07:00
Book Pauk
3920b71613 Merge branch 'release/0.4.2' 2019-02-21 21:39:09 +07:00
Book Pauk
d661150665 0.4.2 2019-02-21 21:38:46 +07:00
Book Pauk
ab29c80dab Поправки багов 2019-02-21 21:36:17 +07:00
Book Pauk
e5384e27e5 Поправки багов 2019-02-21 21:02:27 +07:00
Book Pauk
06cdc6eb63 Улучшение парсинга samlib 2019-02-21 20:26:56 +07:00
Book Pauk
da284c793e Добавил загрузку внешних изображений 2019-02-21 20:22:25 +07:00
Book Pauk
c2cef91eb3 Merge tag '0.4.1' into develop
0.4.1
2019-02-20 21:40:10 +07:00
Book Pauk
e272308823 Merge tag '0.4.0' into develop
0.4.0
2019-02-20 21:08:53 +07:00
10 changed files with 284 additions and 109 deletions

View File

@@ -15,8 +15,14 @@
border
:default-sort = "{prop: 'touchDateTime', order: 'descending'}"
:header-cell-style = "headerCellStyle"
:row-key = "rowKey"
>
<el-table-column
type="index"
width="35px"
>
</el-table-column>
<el-table-column
prop="touchDateTime"
min-width="90px"
@@ -43,6 +49,7 @@
placeholder="Найти"/-->
<div class="el-input el-input--mini">
<input class="el-input__inner"
ref="input"
placeholder="Найти"
style="margin: 0; padding: 0; vertical-align: bottom; margin-top: 20px; padding: 0 10px 0 10px"
:value="search" @input="search = $event.target.value"
@@ -118,9 +125,16 @@ class HistoryPage extends Vue {
created() {
}
mounted() {
init() {
this.updateTableData();
this.mostRecentBook = bookManager.mostRecentBook();
this.$nextTick(() => {
this.$refs.input.focus();
});
}
rowKey(row) {
return row.key;
}
updateTableData() {
@@ -172,13 +186,15 @@ class HistoryPage extends Vue {
}
const search = this.search;
this.tableData = result.filter(item => {
result = result.filter(item => {
return !search ||
item.touchTime.includes(search) ||
item.touchDate.includes(search) ||
item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
item.desc.author.toLowerCase().includes(search.toLowerCase())
});
this.tableData = result;
}
headerCellStyle(cell) {

View File

@@ -69,7 +69,7 @@
@stop-text-search="stopTextSearch">
</SearchPage>
<CopyTextPage v-if="copyTextActive" ref="copyTextPage" @copy-text-toggle="copyTextToggle"></CopyTextPage>
<HistoryPage v-if="historyActive" ref="historyPage" @load-book="loadBook" @history-toggle="historyToggle"></HistoryPage>
<HistoryPage v-show="historyActive" ref="historyPage" @load-book="loadBook" @history-toggle="historyToggle"></HistoryPage>
<SettingsPage v-if="settingsActive" ref="settingsPage" @settings-toggle="settingsToggle"></SettingsPage>
<HelpPage v-if="helpActive" ref="helpPage" @help-toggle="helpToggle"></HelpPage>
<ClickMapPage v-show="clickMapActive" ref="clickMapPage"></ClickMapPage>
@@ -415,6 +415,7 @@ class Reader extends Vue {
this.historyActive = !this.historyActive;
if (this.historyActive) {
this.closeAllTextPages();
this.$refs.historyPage.init();
this.historyActive = true;
} else {
this.historyActive = false;
@@ -831,6 +832,8 @@ class Reader extends Vue {
break;
case 'KeyX':
this.historyToggle();
event.preventDefault();
event.stopPropagation();
break;
case 'KeyS':
this.settingsToggle();

View File

@@ -185,7 +185,7 @@
<el-checkbox v-model="wordWrap">Перенос по слогам</el-checkbox>
</el-form-item>
<el-form-item label="Обработка">
<el-checkbox v-model="cutEmptyParagraphs">Убирать пустые параграфы</el-checkbox>
<el-checkbox v-model="cutEmptyParagraphs">Убирать пустые строки</el-checkbox>
</el-form-item>
<el-form-item label="">
<el-col :span="12">
@@ -212,7 +212,9 @@
</el-col>
</el-form-item>
<el-form-item label="">
Размер изображения
<el-col :span="12">
Размер не более
</el-col>
<el-tooltip :open-delay="500" effect="light" placement="top">
<template slot="content">
Определяет высоту изображения количеством строк.<br>
@@ -437,11 +439,12 @@ class SettingsPage extends Vue {
'#000000',
'#202020',
'#ebe2c9',
'#cfdc99',
'#478355',
'#a6caf0',
'#909080',
'#808080',
'#c8c8c8',
'#478355',
'#a6caf0',
];
}

View File

@@ -28,7 +28,7 @@ export default class DrawHelper {
let out = `<div style="width: ${this.w}px; height: ${this.h}px;` +
` position: absolute; top: ${this.fontSize*this.textShift}px; color: ${this.textColor}; font: ${font}; ${justify}` +
` line-height: ${this.lineHeight}px;">`;
` line-height: ${this.lineHeight}px; white-space: nowrap;">`;
let imageDrawn = new Set();
let len = lines.length;
@@ -91,21 +91,20 @@ export default class DrawHelper {
} else
text = part.text;
if (text.trim() == '')
if (text && text.trim() == '')
text = `<span style="white-space: pre">${text}</span>`;
lineText += `${tOpen}${text}${tClose}`;
center = center || part.style.center;
space = (part.style.space > 0 ? part.style.space : space);
space = (part.style.space > space ? part.style.space : space);
//избражения
//image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number},
const img = part.image;
if (img && img.id && !img.inline && !imageDrawn.has(img.paraIndex)) {
if (img.local) {
const bin = this.parsed.binary[img.id];
const bin = this.parsed.binary[img.id];
if (bin) {
let imgH = img.lineCount*this.lineHeight;
imgH = (imgH <= bin.h ? imgH : bin.h);
let imgW = bin.w;
@@ -118,9 +117,11 @@ export default class DrawHelper {
const left = (this.w - imgW)/2;
const top = ((img.lineCount*this.lineHeight - imgH)/2) + (i - img.imageLine)*this.lineHeight;
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
} else {
//
if (img.local) {
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
} else {
lineText += `<img src="${img.id}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
}
}
imageDrawn.add(img.paraIndex);
}
@@ -128,11 +129,13 @@ export default class DrawHelper {
if (img && img.id && img.inline) {
if (img.local) {
const bin = this.parsed.binary[img.id];
let resize = '';
if (bin.h > this.fontSize) {
resize = `height: ${this.fontSize - 3}px`;
if (bin) {
let resize = '';
if (bin.h > this.fontSize) {
resize = `height: ${this.fontSize - 3}px`;
}
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="${resize}"/>`;
}
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="${resize}"/>`;
} else {
//
}

View File

@@ -45,6 +45,9 @@ export default class BookParser {
let italic = false;
let space = 0;
let inPara = false;
let isFirstBody = true;
let isFirstSection = true;
let isFirstTitlePara = false;
this.binary = {};
let binaryId = '';
@@ -78,6 +81,10 @@ export default class BookParser {
resolve();
};
i.onerror = (e) => {
reject(e);
};
i.src = `data:${binaryType};base64,${data}`;
await sleep(30*1000);
if (!resolved)
@@ -85,6 +92,30 @@ export default class BookParser {
});
};
const getExternalImageDimensions = (src) => {
return new Promise (async(resolve, reject) => {
const i = new Image();
let resolved = false;
i.onload = () => {
resolved = true;
this.binary[src] = {
w: i.width,
h: i.height,
};
resolve();
};
i.onerror = (e) => {
reject(e);
};
i.src = src;
await sleep(30*1000);
if (!resolved)
reject('Не удалось получить размер изображения');
});
};
const newParagraph = (text, len, addIndex) => {
paraIndex++;
let p = {
@@ -147,33 +178,47 @@ export default class BookParser {
if (tag == 'binary') {
let attrs = sax.getAttrsSync(tail);
binaryType = (attrs['content-type'].value ? attrs['content-type'].value : '');
binaryType = (attrs['content-type'] && attrs['content-type'].value ? attrs['content-type'].value : '');
if (binaryType == 'image/jpeg' || binaryType == 'image/png')
binaryId = (attrs.id.value ? attrs.id.value : '');
}
if (tag == 'image') {
let attrs = sax.getAttrsSync(tail);
if (attrs.href.value) {
if (inPara && !this.showInlineImagesInCenter)
growParagraph(`<image-inline href="${attrs.href.value}"></image-inline>`, 0);
else
newParagraph(`<image href="${attrs.href.value}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
if (inPara && this.showInlineImagesInCenter)
newParagraph(' ', 1);
if (attrs.href && attrs.href.value) {
const href = attrs.href.value;
if (href[0] == '#') {//local
if (inPara && !this.showInlineImagesInCenter && !center)
growParagraph(`<image-inline href="${href}"></image-inline>`, 0);
else
newParagraph(`<image href="${href}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
if (inPara && this.showInlineImagesInCenter)
newParagraph(' ', 1);
} else {//external
dimPromises.push(getExternalImageDimensions(href));
newParagraph(`<image href="${href}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
}
}
}
if (path.indexOf('/fictionbook/body') == 0) {
if (tag == 'body') {
if (!isFirstBody)
newParagraph(' ', 1);
isFirstBody = false;
}
if (tag == 'title') {
newParagraph(' ', 1);
isFirstTitlePara = true;
bold = true;
center = true;
}
if (tag == 'section') {
newParagraph(' ', 1);
if (!isFirstSection)
newParagraph(' ', 1);
isFirstSection = false;
}
if (tag == 'emphasis' || tag == 'strong') {
@@ -181,13 +226,17 @@ export default class BookParser {
}
if ((tag == 'p' || tag == 'empty-line' || tag == 'v')) {
newParagraph(' ', 1);
if (tag == 'p')
if (!(tag == 'p' && isFirstTitlePara))
newParagraph(' ', 1);
if (tag == 'p') {
inPara = true;
isFirstTitlePara = false;
}
}
if (tag == 'subtitle') {
newParagraph(' ', 1);
isFirstTitlePara = true;
bold = true;
}
@@ -215,6 +264,7 @@ export default class BookParser {
if (path.indexOf('/fictionbook/body') == 0) {
if (tag == 'title') {
isFirstTitlePara = false;
bold = false;
center = false;
}
@@ -228,6 +278,7 @@ export default class BookParser {
}
if (tag == 'subtitle') {
isFirstTitlePara = false;
bold = false;
}
@@ -260,10 +311,10 @@ export default class BookParser {
text = text.replace(/>/g, '&gt;');
text = text.replace(/</g, '&lt;');
if (text != ' ' && text.trim() == '')
text = text.trim();
if (text && text.trim() == '')
text = (text.indexOf(' ') >= 0 ? ' ' : '');
if (text == '')
if (!text)
return;
text = text.replace(/[\t\n\r]/g, ' ');
@@ -315,13 +366,7 @@ export default class BookParser {
}
if (path.indexOf('/fictionbook/body/section') == 0) {
switch (tag) {
case 'p':
growParagraph(`${tOpen}${text}${tClose}`, text.length);
break;
default:
growParagraph(`${tOpen}${text}${tClose}`, text.length);
}
growParagraph(`${tOpen}${text}${tClose}`, text.length);
}
if (binaryId) {
@@ -409,14 +454,14 @@ export default class BookParser {
break;
case 'space': {
let attrs = sax.getAttrsSync(tail);
if (attrs.w.value)
if (attrs.w && attrs.w.value)
style.space = attrs.w.value;
break;
}
case 'image': {
let attrs = sax.getAttrsSync(tail);
let id = attrs.href.value;
if (id) {
if (attrs.href && attrs.href.value) {
let id = attrs.href.value;
let local = false;
if (id[0] == '#') {
id = id.substr(1);
@@ -428,8 +473,8 @@ export default class BookParser {
}
case 'image-inline': {
let attrs = sax.getAttrsSync(tail);
let id = attrs.href.value;
if (id) {
if (attrs.href && attrs.href.value) {
let id = attrs.href.value;
let local = false;
if (id[0] == '#') {
id = id.substr(1);
@@ -616,7 +661,9 @@ export default class BookParser {
//изображения
if (part.image.id && !part.image.inline) {
parsed.visible = this.showImages;
const bin = this.binary[part.image.id];
let bin = this.binary[part.image.id];
if (!bin)
bin = {h: 0, w: 0};
let lineCount = this.imageHeightLines;
const c = Math.ceil(bin.h/this.lineHeight);
@@ -643,15 +690,18 @@ export default class BookParser {
line.last = true;
line.parts.push({style, text: ' ',
image: {local: part.image.local, inline: false, id: part.image.id, imageLine: i, lineCount, paraIndex}});
continue;
}
if (part.image.id && part.image.inline && this.showImages) {
const bin = this.binary[part.image.id];
let imgH = (bin.h > this.fontSize ? this.fontSize : bin.h);
imgW += bin.w*imgH/bin.h;
line.parts.push({style, text: '',
image: {local: part.image.local, inline: true, id: part.image.id}});
if (bin) {
let imgH = (bin.h > this.fontSize ? this.fontSize : bin.h);
imgW += bin.w*imgH/bin.h;
line.parts.push({style, text: '',
image: {local: part.image.local, inline: true, id: part.image.id}});
}
}
let words = part.text.split(' ');

View File

@@ -1,6 +1,6 @@
{
"name": "Liberama",
"version": "0.4.1",
"version": "0.4.4",
"engines": {
"node": ">=10.0.0"
},

View File

@@ -9,6 +9,8 @@ const textUtils = require('./textUtils');
const FileDetector = require('../FileDetector');
const repSpaces = (text) => text.replace(/&nbsp;|[\t\n\r]/g, ' ');
const repSpaces2 = (text) => text.replace(/[\n\r]/g, '');
const repSpaces3 = (text) => text.replace(/&nbsp;/g, ' ');
class BookConverter {
constructor() {
@@ -31,7 +33,7 @@ class BookConverter {
if (parsedUrl.hostname == 'samlib.ru' ||
parsedUrl.hostname == 'budclub.ru' ||
parsedUrl.hostname == 'zhurnal.lib.ru') {
await fs.writeFile(outputFile, this.convertSamlib(data));
await fs.writeFile(outputFile, this.convertSamlib(data, parsedUrl.hostname));
return;
}
@@ -65,7 +67,10 @@ class BookConverter {
}
}
return iconv.decode(data, selected);
if (selected.toLowerCase() != 'utf-8')
return iconv.decode(data, selected);
else
return data;
}
checkEncoding(data) {
@@ -107,19 +112,21 @@ class BookConverter {
};
const growParagraph = (text) => {
if (!pars.length)
newParagraph();
const l = pars.length;
if (l) {
if (pars[l - 1]._t == '')
text = text.trimLeft();
pars[l - 1]._t += text;
}
if (pars[l - 1]._t == '')
text = text.trimLeft();
pars[l - 1]._t += text;
//посчитаем отступы у текста, чтобы выделить потом параграфы
const lines = text.split('\n');
for (const line of lines) {
const sp = line.split(' ');
for (let line of lines) {
line = repSpaces2(line).replace(/\t/g, ' ');
let l = 0;
while (l < sp.length && sp[l].trim() == '') {
while (l < line.length && line[l] == ' ') {
l++;
}
if (!spaceCounter[l])
@@ -128,7 +135,6 @@ class BookConverter {
}
};
newParagraph();
const newPara = new Set(['tr', 'br', 'br/', 'dd', 'p', 'title', '/title', 'h1', 'h2', 'h3', '/h1', '/h2', '/h3']);
const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
@@ -182,22 +188,28 @@ class BookConverter {
};
const growPar = (text) => {
if (!newPars.length)
newPar();
const l = newPars.length;
if (l) {
newPars[l - 1]._t += text;
}
newPars[l - 1]._t += text;
}
i = 0;
for (const par of pars) {
newPar();
if (i > 0)
newPar();
i++;
const lines = par._t.split('\n');
for (const line of lines) {
const sp = line.split(' ');
for (let line of lines) {
line = repSpaces2(line).replace(/\t/g, ' ');
let l = 0;
while (l < sp.length && sp[l].trim() == '') {
while (l < line.length && line[l] == ' ') {
l++;
}
if (l >= parIndent)
newPar();
growPar(line.trim() + ' ');
@@ -216,7 +228,7 @@ class BookConverter {
return this.formatFb2(fb2);
}
convertSamlib(data) {
convertSamlib(data, hostname) {
let titleInfo = {};
let desc = {_n: 'description', 'title-info': titleInfo};
let pars = [];
@@ -225,20 +237,23 @@ class BookConverter {
let inSubtitle = false;
let inJustify = true;
let inImage = false;
let isFirstPara = false;
let path = '';
let tag = '';// eslint-disable-line no-unused-vars
let inText = false;
let textFound = false;
let node = {_a: pars};
let inPara = false;
let italic = false;
let bold = false;
const openTag = (name) => {
const openTag = (name, attrs) => {
if (name == 'p')
inPara = true;
let n = {_n: name, _a: [], _p: node};
let n = {_n: name, _attrs: attrs, _a: [], _p: node};
node._a.push(n);
node = n;
};
@@ -255,13 +270,17 @@ class BookConverter {
};
const growParagraph = (text) => {
if (!node._p) {
if (text.trim() != '')
openTag('p');
else
return;
}
if (node._n == 'p' && node._a.length == 0)
text = text.trimLeft();
node._a.push({_t: text});
};
openTag('p');
const onStartNode = (elemName, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
if (elemName == '')
return;
@@ -269,28 +288,41 @@ class BookConverter {
path += '/' + elemName;
tag = elemName;
} else {
if (inPara && elemName != 'i' && elemName != 'b')
closeTag('p');
switch (elemName) {
case 'li':
case 'p':
case 'dd':
case 'br':
if (!(inSubtitle && isFirstPara)) {
if (inPara)
closeTag('p');
openTag('p');
}
isFirstPara = false;
break;
case 'h1':
case 'h2':
case 'h3':
if (inPara)
closeTag('p');
openTag('p');
bold = true;
break;
case 'i':
case 'em':
italic = true;
break;
case 'b':
case 'strong':
bold = true;
break;
case 'div':
if (inPara)
closeTag('p');
if (tail.indexOf('align="center"') >= 0) {
openTag('subtitle');
inSubtitle = true;
isFirstPara = true;
}
if (tail.indexOf('align="justify"') >= 0) {
@@ -299,6 +331,19 @@ class BookConverter {
}
break;
case 'img': {
if (inPara)
closeTag('p');
const attrs = sax.getAttrsSync(tail);
if (attrs.src && attrs.src.value) {
let href = attrs.src.value;
if (href[0] == '/')
href = `http://${hostname}${href}`;
openTag('image', {href});
inImage = true;
}
break;
}
}
}
};
@@ -324,21 +369,27 @@ class BookConverter {
case 'li':
case 'p':
case 'dd':
closeTag('p');
break;
case 'h1':
case 'h2':
case 'h3':
closeTag('p');
bold = false;
break;
case 'i':
case 'em':
italic = false;
break;
case 'b':
case 'strong':
bold = false;
break;
case 'div':
if (inSubtitle) {
closeTag('subtitle');
inSubtitle = false;
isFirstPara = false;
}
if (inJustify) {
@@ -346,22 +397,29 @@ class BookConverter {
inJustify = false;
}
break;
case 'img':
if (inImage)
closeTag('image');
inImage = false;
break;
}
}
};
const onComment = (text) => {// eslint-disable-line no-unused-vars
if (text == '--------- Собственно произведение -------------')
if (text == '--------- Собственно произведение -------------') {
inText = true;
textFound = true;
}
if (text == '-----------------------------------------------')
inText = false;
};
const onTextNode = (text) => {// eslint-disable-line no-unused-vars
if (text != ' ' && text.trim() == '')
text = text.trim();
if (text && text.trim() == '')
text = (text.indexOf(' ') >= 0 ? ' ' : '');
if (text == '')
if (!text)
return;
switch (path) {
@@ -390,11 +448,15 @@ class BookConverter {
growParagraph(`${tOpen}${text}${tClose}`);
};
sax.parseSync(repSpaces(this.decode(data).toString()), {
sax.parseSync(repSpaces3(this.decode(data).toString()), {
onStartNode, onEndNode, onTextNode, onComment,
innerCut: new Set(['head', 'script', 'style'])
});
//текст не найден на странице, обрабатываем как html
if (!textFound)
return this.convertHtml(data);
const title = (titleInfo['book-title'] ? titleInfo['book-title'] : '');
let author = '';
if (titleInfo.author) {
@@ -437,21 +499,36 @@ class BookConverter {
if (node._n)
name = node._n;
let attrs = '';
if (node._attrs) {
for (let attrName in node._attrs) {
attrs += ` ${attrName}="${node._attrs[attrName]}"`;
}
}
let tOpen = '';
let tBody = '';
let tClose = '';
if (name)
out += `<${name}>`;
tOpen += `<${name}${attrs}>`;
if (node.hasOwnProperty('_t'))
out += repSpaces(node._t);
tBody += repSpaces(node._t);
for (let nodeName in node) {
if (nodeName && nodeName[0] == '_' && nodeName != '_a')
continue;
const n = node[nodeName];
out += this.formatFb2Node(n, nodeName);
tBody += this.formatFb2Node(n, nodeName);
}
if (name)
out += `</${name}>`;
tClose += `</${name}>`;
if (attrs == '' && name == 'p' && tBody.trim() == '')
out += '<empty-line/>'
else
out += `${tOpen}${tBody}${tClose}`;
}
return out;
}

View File

@@ -1,4 +1,4 @@
function getEncoding(buf) {
function getEncoding(buf, returnAll) {
const lowerCase = 3;
const upperCase = 1;
@@ -8,6 +8,7 @@ function getEncoding(buf) {
'd': 'cp866',
'i': 'ISO-8859-5',
'm': 'maccyrillic',
'u': 'utf-8',
};
let charsets = {
@@ -15,38 +16,47 @@ function getEncoding(buf) {
'w': 0,
'd': 0,
'i': 0,
'm': 0
'm': 0,
'u': 0,
};
const len = buf.length;
const blockSize = (len > 5*3000 ? 3000 : len);
let counter = 0;
let i = 0;
let totalChecked = 0;
while (i < len) {
const char = buf[i];
const nextChar = (i < len - 1 ? buf[i + 1] : 0);
totalChecked++;
i++;
//non-russian characters
if (char < 128 || char > 256)
continue;
//CP866
if ((char > 159 && char < 176) || (char > 223 && char < 242)) charsets['d'] += lowerCase;
if ((char > 127 && char < 160)) charsets['d'] += upperCase;
//UTF-8
if ((char == 208 || char == 209) && nextChar >= 128 && nextChar <= 190)
charsets['u'] += lowerCase;
else {
//CP866
if ((char > 159 && char < 176) || (char > 223 && char < 242)) charsets['d'] += lowerCase;
if ((char > 127 && char < 160)) charsets['d'] += upperCase;
//KOI8-R
if ((char > 191 && char < 223)) charsets['k'] += lowerCase;
if ((char > 222 && char < 256)) charsets['k'] += upperCase;
//KOI8-R
if ((char > 191 && char < 223)) charsets['k'] += lowerCase;
if ((char > 222 && char < 256)) charsets['k'] += upperCase;
//WIN-1251
if (char > 223 && char < 256) charsets['w'] += lowerCase;
if (char > 191 && char < 224) charsets['w'] += upperCase;
//WIN-1251
if (char > 223 && char < 256) charsets['w'] += lowerCase;
if (char > 191 && char < 224) charsets['w'] += upperCase;
//MAC
if (char > 221 && char < 255) charsets['m'] += lowerCase;
if (char > 127 && char < 160) charsets['m'] += upperCase;
//MAC
if (char > 221 && char < 255) charsets['m'] += lowerCase;
if (char > 127 && char < 160) charsets['m'] += upperCase;
//ISO-8859-5
if (char > 207 && char < 240) charsets['i'] += lowerCase;
if (char > 175 && char < 208) charsets['i'] += upperCase;
//ISO-8859-5
if (char > 207 && char < 240) charsets['i'] += lowerCase;
if (char > 175 && char < 208) charsets['i'] += upperCase;
}
counter++;
@@ -57,18 +67,24 @@ function getEncoding(buf) {
}
let sorted = Object.keys(charsets).map(function(key) {
return { codePage: codePage[key], c: charsets[key] };
return { codePage: codePage[key], c: charsets[key], totalChecked };
});
sorted.sort((a, b) => b.c - a.c);
if (sorted[0].c > 0)
if (returnAll)
return sorted;
else if (sorted[0].c > 0)
return sorted[0].codePage;
else
return 'ISO-8859-5';
}
function checkIfText(buf) {
const enc = getEncoding(buf, true);
if (enc[0].c > enc[0].totalChecked*0.9)
return true;
let spaceCount = 0;
let crCount = 0;
let lfCount = 0;

View File

@@ -8,8 +8,14 @@ class FileDownloader {
async load(url, callback) {
let errMes = '';
const options = {
encoding: null,
headers: {
'user-agent': 'Mozilla/5.0 (X11; HasCodingOs 1.0; Linux x64) AppleWebKit/637.36 (KHTML, like Gecko) Chrome/70.0.3112.101 Safari/637.36 HasBrowser/5.0'
}
};
const response = await got(url, {method: 'HEAD'});
const response = await got(url, Object.assign({}, options, {method: 'HEAD'}));
let estSize = 0;
if (response.headers['content-length']) {
@@ -17,7 +23,7 @@ class FileDownloader {
}
let prevProg = 0;
const request = got(url, {encoding: null}).on('downloadProgress', progress => {
const request = got(url, options).on('downloadProgress', progress => {
if (progress.transferred > maxDownloadSize) {
errMes = 'file too big';
request.cancel();

View File

@@ -31,6 +31,7 @@ async function init() {
}
async function main() {
log(`${config.name} v${config.version}`);
log('Initializing');
await init();