Compare commits
71 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2df88f4280 | ||
|
|
156a1b4aa8 | ||
|
|
962eda7860 | ||
|
|
954ce9e85c | ||
|
|
ff27ddd442 | ||
|
|
f9cc2ad70a | ||
|
|
4bcd45a795 | ||
|
|
9e7ccd6e20 | ||
|
|
631c5930e9 | ||
|
|
0ddb182642 | ||
|
|
de2f0e74c8 | ||
|
|
ad4ee6ccc9 | ||
|
|
1670df02db | ||
|
|
0268e647cd | ||
|
|
4b9315c13c | ||
|
|
14a9948dd2 | ||
|
|
060ec98b0c | ||
|
|
9c1efe381e | ||
|
|
6a1b052d16 | ||
|
|
d6151d541e | ||
|
|
3e90277e1e | ||
|
|
641cbdfe85 | ||
|
|
c36e9b36d8 | ||
|
|
ecc3acce93 | ||
|
|
33a2ca55f0 | ||
|
|
9d0bbec4b3 | ||
|
|
4de0b3cffd | ||
|
|
06221a474b | ||
|
|
e99a42b7af | ||
|
|
37822e8409 | ||
|
|
2e477e6c99 | ||
|
|
360ee98d8d | ||
|
|
69afd7720a | ||
|
|
a75590c493 | ||
|
|
2acb65f6b3 | ||
|
|
1e1a58b58c | ||
|
|
aeadb5aeb8 | ||
|
|
3e2f01d56d | ||
|
|
cad97e639a | ||
|
|
e2632f1802 | ||
|
|
9aa0bb2bde | ||
|
|
f015d5f7ed | ||
|
|
74f8f7f9a4 | ||
|
|
2598538de9 | ||
|
|
a78a00df2b | ||
|
|
92f6beb64e | ||
|
|
3920b71613 | ||
|
|
d661150665 | ||
|
|
ab29c80dab | ||
|
|
e5384e27e5 | ||
|
|
06cdc6eb63 | ||
|
|
da284c793e | ||
|
|
c2cef91eb3 | ||
|
|
19da1aff45 | ||
|
|
5f2206e766 | ||
|
|
e272308823 | ||
|
|
8491c40890 | ||
|
|
dfa7013cbd | ||
|
|
1a7ceb333d | ||
|
|
d3a30b87f4 | ||
|
|
dd61c04d63 | ||
|
|
5496e874c4 | ||
|
|
56d13288ff | ||
|
|
618111ab05 | ||
|
|
55d02495a3 | ||
|
|
83fc586e03 | ||
|
|
ae3dc9b22c | ||
|
|
12e0f9459b | ||
|
|
fd1dd54b99 | ||
|
|
32cbb2a82c | ||
|
|
048b7c08ca |
@@ -11,7 +11,7 @@ const workerApi = axios.create({
|
||||
|
||||
class Reader {
|
||||
async loadBook(url, callback) {
|
||||
const refreshPause = 200;
|
||||
const refreshPause = 300;
|
||||
if (!callback) callback = () => {};
|
||||
|
||||
let response = await api.post('/load-book', {type: 'url', url});
|
||||
|
||||
@@ -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) {
|
||||
@@ -210,7 +226,8 @@ class HistoryPage extends Vue {
|
||||
this.updateTableData();
|
||||
|
||||
const newRecent = bookManager.mostRecentBook();
|
||||
if (this.mostRecentBook != newRecent)
|
||||
|
||||
if (!(this.mostRecentBook && newRecent && this.mostRecentBook.key == newRecent.key))
|
||||
this.$emit('load-book', newRecent);
|
||||
|
||||
this.mostRecentBook = newRecent;
|
||||
|
||||
@@ -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>
|
||||
@@ -201,7 +201,7 @@ class Reader extends Vue {
|
||||
|
||||
mounted() {
|
||||
(async() => {
|
||||
await bookManager.init();
|
||||
await bookManager.init(this.settings);
|
||||
await restoreOldSettings(this.settings, bookManager, this.commit);
|
||||
|
||||
if (this.$root.rootRoute == '/reader') {
|
||||
@@ -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;
|
||||
@@ -655,7 +656,7 @@ class Reader extends Vue {
|
||||
|
||||
// если есть в локальном кэше
|
||||
if (bookParsed) {
|
||||
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, bookPosPercent}, bookManager.metaOnly(bookParsed)));
|
||||
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, bookPosPercent}, bookParsed));
|
||||
this.mostRecentBook();
|
||||
this.addAction(bookPos);
|
||||
this.loaderActive = false;
|
||||
@@ -698,7 +699,7 @@ class Reader extends Vue {
|
||||
});
|
||||
|
||||
// добавляем в историю
|
||||
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, bookPosPercent}, bookManager.metaOnly(addedBook)));
|
||||
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, bookPosPercent}, addedBook));
|
||||
this.mostRecentBook();
|
||||
this.addAction(bookPos);
|
||||
this.updateRoute(true);
|
||||
@@ -831,6 +832,8 @@ class Reader extends Vue {
|
||||
break;
|
||||
case 'KeyX':
|
||||
this.historyToggle();
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
break;
|
||||
case 'KeyS':
|
||||
this.settingsToggle();
|
||||
|
||||
@@ -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">
|
||||
@@ -194,6 +194,40 @@
|
||||
<el-input-number v-model="addEmptyParagraphs" :min="0" :max="2"></el-input-number>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="Изображения">
|
||||
<el-col :span="11">
|
||||
<el-checkbox v-model="showImages">Показывать</el-checkbox>
|
||||
</el-col>
|
||||
|
||||
<el-col :span="1">
|
||||
|
||||
</el-col>
|
||||
<el-col :span="11">
|
||||
<el-tooltip :open-delay="500" effect="light" placement="top">
|
||||
<template slot="content">
|
||||
Выносить все изображения в центр экрана
|
||||
</template>
|
||||
<el-checkbox v-model="showInlineImagesInCenter" @change="needReload" :disabled="!showImages">Инлайн в центр</el-checkbox>
|
||||
</el-tooltip>
|
||||
</el-col>
|
||||
</el-form-item>
|
||||
<el-form-item label="">
|
||||
<el-checkbox v-model="imageFitWidth" :disabled="!showImages">Ширина не более размера экрана</el-checkbox>
|
||||
</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>
|
||||
В случае превышения высоты, изображение будет<br>
|
||||
уменьшено с сохранением пропорций так, чтобы<br>
|
||||
помещаться в указанное количество строк.
|
||||
</template>
|
||||
<el-input-number v-model="imageHeightLines" :min="1" :max="100" :disabled="!showImages"></el-input-number>
|
||||
</el-tooltip>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<el-form :model="form" size="mini" label-width="120px" @submit.native.prevent>
|
||||
@@ -408,11 +442,12 @@ class SettingsPage extends Vue {
|
||||
'#000000',
|
||||
'#202020',
|
||||
'#ebe2c9',
|
||||
'#cfdc99',
|
||||
'#478355',
|
||||
'#a6caf0',
|
||||
'#909080',
|
||||
'#808080',
|
||||
'#c8c8c8',
|
||||
'#478355',
|
||||
'#a6caf0',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -26,10 +26,11 @@ export default class DrawHelper {
|
||||
const font = this.fontByStyle({});
|
||||
const justify = (this.textAlignJustify ? 'text-align: justify; text-align-last: justify;' : '');
|
||||
|
||||
let out = `<div class="layout" style="width: ${this.w}px; height: ${this.h}px;` +
|
||||
let out = `<div style="width: ${this.w}px; height: ${this.h + (isScrolling ? this.lineHeight : 0)}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; overflow: hidden;">`;
|
||||
|
||||
let imageDrawn = new Set();
|
||||
let len = lines.length;
|
||||
const lineCount = this.pageLineCount + (isScrolling ? 1 : 0);
|
||||
len = (len > lineCount ? lineCount : len);
|
||||
@@ -43,11 +44,13 @@ export default class DrawHelper {
|
||||
first: Boolean,
|
||||
last: Boolean,
|
||||
parts: array of {
|
||||
style: {bold: Boolean, italic: Boolean, center: Boolean}
|
||||
style: {bold: Boolean, italic: Boolean, center: Boolean},
|
||||
image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number},
|
||||
text: String,
|
||||
}
|
||||
}*/
|
||||
let sel = new Set();
|
||||
//поиск
|
||||
if (i == 0 && this.searching) {
|
||||
let pureText = '';
|
||||
for (const part of line.parts) {
|
||||
@@ -70,7 +73,9 @@ export default class DrawHelper {
|
||||
|
||||
let lineText = '';
|
||||
let center = false;
|
||||
let space = 0;
|
||||
let j = 0;
|
||||
//формируем строку
|
||||
for (const part of line.parts) {
|
||||
let tOpen = (part.style.bold ? '<b>' : '');
|
||||
tOpen += (part.style.italic ? '<i>' : '');
|
||||
@@ -86,14 +91,59 @@ export default class DrawHelper {
|
||||
} else
|
||||
text = part.text;
|
||||
|
||||
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 > space ? part.style.space : space);
|
||||
|
||||
//избражения
|
||||
//image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number, w: Number, h: Number},
|
||||
const img = part.image;
|
||||
if (img && img.id && !img.inline && !imageDrawn.has(img.paraIndex)) {
|
||||
const bin = this.parsed.binary[img.id];
|
||||
if (bin) {
|
||||
let resize = '';
|
||||
if (bin.h > img.h) {
|
||||
resize = `height: ${img.h}px`;
|
||||
}
|
||||
|
||||
const left = (this.w - img.w)/2;
|
||||
const top = ((img.lineCount*this.lineHeight - img.h)/2) + (i - img.imageLine)*this.lineHeight;
|
||||
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);
|
||||
}
|
||||
|
||||
if (img && img.id && img.inline) {
|
||||
if (img.local) {
|
||||
const bin = this.parsed.binary[img.id];
|
||||
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}"/>`;
|
||||
}
|
||||
} else {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const centerStyle = (center ? `text-align: center; text-align-last: center; width: ${this.w}px` : '')
|
||||
if (line.first)
|
||||
lineText = `<span style="display: inline-block; margin-left: ${this.p}px"></span>${lineText}`;
|
||||
if ((line.first || space) && !center) {
|
||||
let p = (line.first ? this.p : 0);
|
||||
p = (space ? p + this.p*space : p);
|
||||
lineText = `<span style="display: inline-block; margin-left: ${p}px"></span>${lineText}`;
|
||||
}
|
||||
|
||||
if (line.last || center)
|
||||
lineText = `<span style="display: inline-block; ${centerStyle}">${lineText}</span>`;
|
||||
|
||||
|
||||
@@ -209,6 +209,7 @@ class TextPage extends Vue {
|
||||
this.parsed.p = this.p;
|
||||
this.parsed.w = this.w;// px, ширина текста
|
||||
this.parsed.font = this.font;
|
||||
this.parsed.fontSize = this.fontSize;
|
||||
this.parsed.wordWrap = this.wordWrap;
|
||||
this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
|
||||
this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;
|
||||
@@ -216,6 +217,11 @@ class TextPage extends Vue {
|
||||
while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ';
|
||||
this.parsed.maxWordLength = t.length - 1;
|
||||
this.parsed.measureText = this.drawHelper.measureText.bind(this.drawHelper);
|
||||
this.parsed.lineHeight = this.lineHeight;
|
||||
this.parsed.showImages = this.showImages;
|
||||
this.parsed.showInlineImagesInCenter = this.showInlineImagesInCenter;
|
||||
this.parsed.imageHeightLines = this.imageHeightLines;
|
||||
this.parsed.imageFitWidth = this.imageFitWidth;
|
||||
}
|
||||
|
||||
//statusBar
|
||||
|
||||
@@ -5,7 +5,11 @@ import {sleep} from '../../../share/utils';
|
||||
const maxImageLineCount = 100;
|
||||
|
||||
export default class BookParser {
|
||||
constructor() {
|
||||
constructor(settings) {
|
||||
if (settings) {
|
||||
this.showInlineImagesInCenter = settings.showInlineImagesInCenter;
|
||||
}
|
||||
|
||||
// defaults
|
||||
this.p = 30;// px, отступ параграфа
|
||||
this.w = 300;// px, ширина страницы
|
||||
@@ -39,6 +43,12 @@ export default class BookParser {
|
||||
let center = false;
|
||||
let bold = false;
|
||||
let italic = false;
|
||||
let space = 0;
|
||||
let inPara = false;
|
||||
let isFirstBody = true;
|
||||
let isFirstSection = true;
|
||||
let isFirstTitlePara = false;
|
||||
|
||||
this.binary = {};
|
||||
let binaryId = '';
|
||||
let binaryType = '';
|
||||
@@ -71,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)
|
||||
@@ -78,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 = {
|
||||
@@ -140,39 +178,71 @@ 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)
|
||||
newParagraph(`<image href="${attrs.href.value}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
|
||||
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') {
|
||||
if (!isFirstSection)
|
||||
newParagraph(' ', 1);
|
||||
isFirstSection = false;
|
||||
}
|
||||
|
||||
if (tag == 'emphasis' || tag == 'strong') {
|
||||
growParagraph(`<${tag}>`, 0);
|
||||
}
|
||||
|
||||
if ((tag == 'p' || tag == 'empty-line' || tag == 'v')) {
|
||||
newParagraph(' ', 1);
|
||||
if (!(tag == 'p' && isFirstTitlePara))
|
||||
newParagraph(' ', 1);
|
||||
if (tag == 'p') {
|
||||
inPara = true;
|
||||
isFirstTitlePara = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (tag == 'subtitle') {
|
||||
newParagraph(' ', 1);
|
||||
isFirstTitlePara = true;
|
||||
bold = true;
|
||||
}
|
||||
|
||||
if (tag == 'epigraph') {
|
||||
italic = true;
|
||||
space += 1;
|
||||
}
|
||||
|
||||
if (tag == 'poem') {
|
||||
@@ -180,7 +250,8 @@ export default class BookParser {
|
||||
}
|
||||
|
||||
if (tag == 'text-author') {
|
||||
newParagraph(' <s> <s> <s> ', 4);
|
||||
newParagraph(' ', 1);
|
||||
space += 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -193,6 +264,7 @@ export default class BookParser {
|
||||
|
||||
if (path.indexOf('/fictionbook/body') == 0) {
|
||||
if (tag == 'title') {
|
||||
isFirstTitlePara = false;
|
||||
bold = false;
|
||||
center = false;
|
||||
}
|
||||
@@ -201,17 +273,27 @@ export default class BookParser {
|
||||
growParagraph(`</${tag}>`, 0);
|
||||
}
|
||||
|
||||
if (tag == 'p') {
|
||||
inPara = false;
|
||||
}
|
||||
|
||||
if (tag == 'subtitle') {
|
||||
isFirstTitlePara = false;
|
||||
bold = false;
|
||||
}
|
||||
|
||||
if (tag == 'epigraph') {
|
||||
italic = false;
|
||||
space -= 1;
|
||||
}
|
||||
|
||||
if (tag == 'stanza') {
|
||||
newParagraph(' ', 1);
|
||||
}
|
||||
|
||||
if (tag == 'text-author') {
|
||||
space -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
path = path.substr(0, path.length - tag.length - 1);
|
||||
@@ -229,13 +311,13 @@ export default class BookParser {
|
||||
text = text.replace(/>/g, '>');
|
||||
text = text.replace(/</g, '<');
|
||||
|
||||
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, ' ');
|
||||
text = text.replace(/[\t\n\r\xa0]/g, ' ');
|
||||
|
||||
switch (path) {
|
||||
case '/fictionbook/description/title-info/author/first-name':
|
||||
@@ -273,7 +355,9 @@ export default class BookParser {
|
||||
let tOpen = (center ? '<center>' : '');
|
||||
tOpen += (bold ? '<strong>' : '');
|
||||
tOpen += (italic ? '<emphasis>' : '');
|
||||
let tClose = (italic ? '</emphasis>' : '');
|
||||
tOpen += (space ? `<space w="${space}">` : '');
|
||||
let tClose = (space ? '</space>' : '');
|
||||
tClose += (italic ? '</emphasis>' : '');
|
||||
tClose += (bold ? '</strong>' : '');
|
||||
tClose += (center ? '</center>' : '');
|
||||
|
||||
@@ -282,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) {
|
||||
@@ -348,18 +426,13 @@ export default class BookParser {
|
||||
|
||||
splitToStyle(s) {
|
||||
let result = [];/*array of {
|
||||
style: {bold: Boolean, italic: Boolean, center: Boolean},
|
||||
image: Boolean,
|
||||
imageId: String,
|
||||
style: {bold: Boolean, italic: Boolean, center: Boolean, space: Number},
|
||||
image: {local: Boolean, inline: Boolean, id: String},
|
||||
text: String,
|
||||
}*/
|
||||
let style = {};
|
||||
let image = {};
|
||||
|
||||
/*let attrs = sax.getAttrsSync(tail);
|
||||
if (attrs.href.value)
|
||||
newParagraph(' '.repeat(maxImageLineCount) + `<image href="${attrs.href.value}" />`, maxImageLineCount);
|
||||
*/
|
||||
const onTextNode = async(text) => {// eslint-disable-line no-unused-vars
|
||||
result.push({
|
||||
style: Object.assign({}, style),
|
||||
@@ -379,9 +452,42 @@ export default class BookParser {
|
||||
case 'center':
|
||||
style.center = true;
|
||||
break;
|
||||
case 'image':
|
||||
image = {};
|
||||
case 'space': {
|
||||
let attrs = sax.getAttrsSync(tail);
|
||||
if (attrs.w && attrs.w.value)
|
||||
style.space = attrs.w.value;
|
||||
break;
|
||||
}
|
||||
case 'image': {
|
||||
let attrs = sax.getAttrsSync(tail);
|
||||
if (attrs.href && attrs.href.value) {
|
||||
let id = attrs.href.value;
|
||||
let local = false;
|
||||
if (id[0] == '#') {
|
||||
id = id.substr(1);
|
||||
local = true;
|
||||
}
|
||||
image = {local, inline: false, id};
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'image-inline': {
|
||||
let attrs = sax.getAttrsSync(tail);
|
||||
if (attrs.href && attrs.href.value) {
|
||||
let id = attrs.href.value;
|
||||
let local = false;
|
||||
if (id[0] == '#') {
|
||||
id = id.substr(1);
|
||||
local = true;
|
||||
}
|
||||
result.push({
|
||||
style: Object.assign({}, style),
|
||||
image: {local, inline: true, id},
|
||||
text: ''
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -396,9 +502,14 @@ export default class BookParser {
|
||||
case 'center':
|
||||
style.center = false;
|
||||
break;
|
||||
case 'space':
|
||||
style.space = 0;
|
||||
break;
|
||||
case 'image':
|
||||
image = {};
|
||||
break;
|
||||
case 'image-inline':
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -412,20 +523,22 @@ export default class BookParser {
|
||||
result = [];
|
||||
for (const part of parts) {
|
||||
let p = part;
|
||||
let i = 0;
|
||||
let spaceIndex = -1;
|
||||
while (i < p.text.length) {
|
||||
if (p.text[i] == ' ')
|
||||
spaceIndex = i;
|
||||
if (!p.image.id) {
|
||||
let i = 0;
|
||||
let spaceIndex = -1;
|
||||
while (i < p.text.length) {
|
||||
if (p.text[i] == ' ')
|
||||
spaceIndex = i;
|
||||
|
||||
if (i - spaceIndex >= maxWordLength && i < p.text.length - 1 &&
|
||||
this.measureText(p.text.substr(spaceIndex + 1, i - spaceIndex), p.style) >= this.w - this.p) {
|
||||
result.push({style: p.style, image: p.image, text: p.text.substr(0, i + 1)});
|
||||
p = {style: p.style, text: p.text.substr(i + 1)};
|
||||
spaceIndex = -1;
|
||||
i = -1;
|
||||
if (i - spaceIndex >= maxWordLength && i < p.text.length - 1 &&
|
||||
this.measureText(p.text.substr(spaceIndex + 1, i - spaceIndex), p.style) >= this.w - this.p) {
|
||||
result.push({style: p.style, image: p.image, text: p.text.substr(0, i + 1)});
|
||||
p = {style: p.style, image: p.image, text: p.text.substr(i + 1)};
|
||||
spaceIndex = -1;
|
||||
i = -1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
result.push(p);
|
||||
}
|
||||
@@ -494,7 +607,10 @@ export default class BookParser {
|
||||
para.parsed.maxWordLength === this.maxWordLength &&
|
||||
para.parsed.font === this.font &&
|
||||
para.parsed.cutEmptyParagraphs === this.cutEmptyParagraphs &&
|
||||
para.parsed.addEmptyParagraphs === this.addEmptyParagraphs
|
||||
para.parsed.addEmptyParagraphs === this.addEmptyParagraphs &&
|
||||
para.parsed.showImages === this.showImages &&
|
||||
para.parsed.imageHeightLines === this.imageHeightLines &&
|
||||
para.parsed.imageFitWidth === this.imageFitWidth
|
||||
)
|
||||
return para.parsed;
|
||||
|
||||
@@ -506,6 +622,9 @@ export default class BookParser {
|
||||
font: this.font,
|
||||
cutEmptyParagraphs: this.cutEmptyParagraphs,
|
||||
addEmptyParagraphs: this.addEmptyParagraphs,
|
||||
showImages: this.showImages,
|
||||
imageHeightLines: this.imageHeightLines,
|
||||
imageFitWidth: this.imageFitWidth,
|
||||
visible: !(
|
||||
(this.cutEmptyParagraphs && para.cut) ||
|
||||
(para.addIndex > this.addEmptyParagraphs)
|
||||
@@ -521,6 +640,7 @@ export default class BookParser {
|
||||
last: Boolean,
|
||||
parts: array of {
|
||||
style: {bold: Boolean, italic: Boolean, center: Boolean},
|
||||
image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number, w: Number, h: Number},
|
||||
text: String,
|
||||
}
|
||||
}*/
|
||||
@@ -531,16 +651,79 @@ export default class BookParser {
|
||||
|
||||
let str = '';//измеряемая строка
|
||||
let prevStr = '';//строка без крайнего слова
|
||||
let prevW = 0;
|
||||
let j = 0;//номер строки
|
||||
let style = {};
|
||||
let ofs = 0;//смещение от начала параграфа para.offset
|
||||
|
||||
// тут начинается самый замес, перенос по слогам и стилизация
|
||||
let imgW = 0;
|
||||
// тут начинается самый замес, перенос по слогам и стилизация, а также изображения
|
||||
for (const part of parts) {
|
||||
const words = part.text.split(' ');
|
||||
style = part.style;
|
||||
|
||||
//изображения
|
||||
if (part.image.id && !part.image.inline) {
|
||||
parsed.visible = this.showImages;
|
||||
let bin = this.binary[part.image.id];
|
||||
if (!bin)
|
||||
bin = {h: 1, w: 1};
|
||||
|
||||
let lineCount = this.imageHeightLines;
|
||||
let c = Math.ceil(bin.h/this.lineHeight);
|
||||
|
||||
const maxH = lineCount*this.lineHeight;
|
||||
let maxH2 = maxH;
|
||||
if (this.imageFitWidth && bin.w > this.w) {
|
||||
maxH2 = bin.h*this.w/bin.w;
|
||||
c = Math.ceil(maxH2/this.lineHeight);
|
||||
}
|
||||
lineCount = (c < lineCount ? c : lineCount);
|
||||
|
||||
let imageHeight = (maxH2 < maxH ? maxH2 : maxH);
|
||||
imageHeight = (imageHeight <= bin.h ? imageHeight : bin.h);
|
||||
|
||||
let imageWidth = (bin.h > imageHeight ? bin.w*imageHeight/bin.h : bin.w);
|
||||
|
||||
let i = 0;
|
||||
for (; i < lineCount - 1; i++) {
|
||||
line.end = para.offset + ofs;
|
||||
line.first = (j == 0);
|
||||
line.last = false;
|
||||
line.parts.push({style, text: ' ', image: {
|
||||
local: part.image.local,
|
||||
inline: false,
|
||||
id: part.image.id,
|
||||
imageLine: i,
|
||||
lineCount,
|
||||
paraIndex,
|
||||
w: imageWidth,
|
||||
h: imageHeight,
|
||||
}});
|
||||
lines.push(line);
|
||||
line = {begin: line.end + 1, parts: []};
|
||||
ofs++;
|
||||
j++;
|
||||
}
|
||||
line.first = (j == 0);
|
||||
line.last = true;
|
||||
line.parts.push({style, text: ' ',
|
||||
image: {local: part.image.local, inline: false, id: part.image.id,
|
||||
imageLine: i, lineCount, paraIndex, w: imageWidth, h: imageHeight}
|
||||
});
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (part.image.id && part.image.inline && this.showImages) {
|
||||
const bin = this.binary[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(' ');
|
||||
|
||||
let sp1 = '';
|
||||
let sp2 = '';
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
@@ -552,7 +735,8 @@ export default class BookParser {
|
||||
|
||||
str += sp1 + word;
|
||||
|
||||
let p = (j == 0 ? parsed.p : 0);
|
||||
let p = (j == 0 ? parsed.p : 0) + imgW;
|
||||
p = (style.space ? p + parsed.p*style.space : p);
|
||||
let w = this.measureText(str, style) + p;
|
||||
let wordTail = word;
|
||||
if (w > parsed.w && prevStr != '') {
|
||||
@@ -578,7 +762,6 @@ export default class BookParser {
|
||||
}
|
||||
|
||||
if (pw) {
|
||||
prevW = pw;
|
||||
partText += ss + (ss[ss.length - 1] == '-' ? '' : '-');
|
||||
wordTail = slogi.join('');
|
||||
}
|
||||
@@ -592,7 +775,6 @@ export default class BookParser {
|
||||
let t = line.parts[line.parts.length - 1].text;
|
||||
if (t[t.length - 1] == ' ') {
|
||||
line.parts[line.parts.length - 1].text = t.trimRight();
|
||||
prevW -= this.measureText(' ', style);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -600,7 +782,6 @@ export default class BookParser {
|
||||
if (line.end - line.begin < 0)
|
||||
console.error(`Parse error, empty line in paragraph ${paraIndex}`);
|
||||
|
||||
line.width = prevW;
|
||||
line.first = (j == 0);
|
||||
line.last = false;
|
||||
lines.push(line);
|
||||
@@ -609,6 +790,7 @@ export default class BookParser {
|
||||
partText = '';
|
||||
sp2 = '';
|
||||
str = wordTail;
|
||||
imgW = 0;
|
||||
j++;
|
||||
}
|
||||
|
||||
@@ -616,7 +798,6 @@ export default class BookParser {
|
||||
partText += sp2 + wordTail;
|
||||
sp1 = ' ';
|
||||
sp2 = ' ';
|
||||
prevW = w;
|
||||
}
|
||||
|
||||
if (partText != '')
|
||||
@@ -628,14 +809,12 @@ export default class BookParser {
|
||||
let t = line.parts[line.parts.length - 1].text;
|
||||
if (t[t.length - 1] == ' ') {
|
||||
line.parts[line.parts.length - 1].text = t.trimRight();
|
||||
prevW -= this.measureText(' ', style);
|
||||
}
|
||||
|
||||
line.end = para.offset + para.length - 1;
|
||||
if (line.end - line.begin < 0)
|
||||
console.error(`Parse error, empty line in paragraph ${paraIndex}`);
|
||||
|
||||
line.width = prevW;
|
||||
line.first = (j == 0);
|
||||
line.last = true;
|
||||
lines.push(line);
|
||||
|
||||
@@ -5,6 +5,10 @@ import BookParser from './BookParser';
|
||||
|
||||
const maxDataSize = 500*1024*1024;//chars, not bytes
|
||||
|
||||
const bmCacheStore = localForage.createInstance({
|
||||
name: 'bmCacheStore'
|
||||
});
|
||||
|
||||
const bmMetaStore = localForage.createInstance({
|
||||
name: 'bmMetaStore'
|
||||
});
|
||||
@@ -18,12 +22,33 @@ const bmRecentStore = localForage.createInstance({
|
||||
});
|
||||
|
||||
class BookManager {
|
||||
async init() {
|
||||
this.books = {};
|
||||
this.recent = {};
|
||||
async init(settings) {
|
||||
this.settings = settings;
|
||||
|
||||
//this.booksCached нужен только для ускорения загрузки читалки
|
||||
this.booksCached = await bmCacheStore.getItem('books');
|
||||
if (!this.booksCached)
|
||||
this.booksCached = {};
|
||||
this.recent = await bmCacheStore.getItem('recent');
|
||||
this.books = Object.assign({}, this.booksCached);
|
||||
|
||||
this.recentChanged1 = true;
|
||||
this.recentChanged2 = true;
|
||||
|
||||
if (!this.books || !this.recent) {
|
||||
this.books = {};
|
||||
this.recent = {};
|
||||
await this.loadMeta(true);
|
||||
} else {
|
||||
this.loadMeta(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async loadMeta(immediate) {
|
||||
if (!immediate)
|
||||
await utils.sleep(2000);
|
||||
|
||||
let len = await bmMetaStore.length();
|
||||
for (let i = 0; i < len; i++) {
|
||||
const key = await bmMetaStore.key(i);
|
||||
@@ -44,6 +69,12 @@ class BookManager {
|
||||
}
|
||||
|
||||
await this.cleanBooks();
|
||||
|
||||
this.booksCached = {};
|
||||
for (const key in this.books) {
|
||||
this.booksCached[key] = this.metaOnly(this.books[key]);
|
||||
}
|
||||
await bmCacheStore.setItem('books', this.booksCached);
|
||||
}
|
||||
|
||||
async cleanBooks() {
|
||||
@@ -79,9 +110,11 @@ class BookManager {
|
||||
const result = await this.parseBook(meta, newBook.data, callback);
|
||||
|
||||
this.books[meta.key] = result;
|
||||
this.booksCached[meta.key] = this.metaOnly(result);
|
||||
|
||||
await bmMetaStore.setItem(`bmMeta-${meta.key}`, this.metaOnly(result));
|
||||
await bmDataStore.setItem(`bmData-${meta.key}`, result.data);
|
||||
await bmCacheStore.setItem('books', this.booksCached);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -126,12 +159,16 @@ class BookManager {
|
||||
await bmDataStore.removeItem(`bmData-${meta.key}`);
|
||||
|
||||
delete this.books[meta.key];
|
||||
delete this.booksCached[meta.key];
|
||||
|
||||
await bmCacheStore.setItem('books', this.booksCached);
|
||||
}
|
||||
|
||||
async parseBook(meta, data, callback) {
|
||||
if (!this.books)
|
||||
await this.init();
|
||||
const parsed = new BookParser();
|
||||
|
||||
const parsed = new BookParser(this.settings);
|
||||
|
||||
const parsedMeta = await parsed.parse(data, callback);
|
||||
const result = Object.assign({}, meta, parsedMeta, {
|
||||
@@ -158,7 +195,7 @@ class BookManager {
|
||||
async setRecentBook(value, noTouch) {
|
||||
if (!this.recent)
|
||||
await this.init();
|
||||
const result = Object.assign({}, value);
|
||||
const result = this.metaOnly(value);
|
||||
if (!noTouch)
|
||||
Object.assign(result, {touchTime: Date.now()});
|
||||
|
||||
@@ -169,6 +206,7 @@ class BookManager {
|
||||
|
||||
await bmRecentStore.setItem(result.key, result);
|
||||
await this.cleanRecentBooks();
|
||||
await bmCacheStore.setItem('recent', this.recent);
|
||||
|
||||
this.recentChanged1 = true;
|
||||
this.recentChanged2 = true;
|
||||
@@ -187,6 +225,8 @@ class BookManager {
|
||||
|
||||
await bmRecentStore.removeItem(value.key);
|
||||
delete this.recent[value.key];
|
||||
await bmCacheStore.setItem('recent', this.recent);
|
||||
|
||||
this.recentChanged1 = true;
|
||||
this.recentChanged2 = true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import Vue from 'vue';
|
||||
|
||||
const fonts = [
|
||||
{name: 'ReaderDefault', label: 'По-умолчанию', fontVertShift: 0},
|
||||
{name: 'GEO_1', label: 'BPG Arial', fontVertShift: 10},
|
||||
@@ -163,6 +161,10 @@ const settingDefaults = {
|
||||
cutEmptyParagraphs: false,
|
||||
addEmptyParagraphs: 0,
|
||||
blinkCachedLoad: true,
|
||||
showImages: true,
|
||||
showInlineImagesInCenter: false,
|
||||
imageHeightLines: 100,
|
||||
imageFitWidth: true,
|
||||
|
||||
fontShifts: {},
|
||||
};
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
siteroot = 'http://omnireader.ru:11080/';
|
||||
siteroot = 'http://old.omnireader.ru/';
|
||||
doRedirect = '';
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
$siteroot = 'http://omnireader.ru:11080/';
|
||||
$siteroot = 'http://old.omnireader.ru/';
|
||||
$use_gzip = false;
|
||||
$tmp_dir = '/tmp';
|
||||
?>
|
||||
|
||||
@@ -156,7 +156,7 @@
|
||||
Ïîääåðæèâàåìûå ôîðìàòû: <strong>html, txt, fb2, fb2.zip</strong>
|
||||
|
||||
<br><br>Âû ìîæåòå äîáàâèòü â ñâîé áðàóçåð çàêëàäêó, óêàçàâ â åå ñâîéñòâàõ âìåñòî àäðåñà ñëåäóþùèé êîä:
|
||||
<br><p><strong>javascript:location.href='http://omnireader.ru:11080/?url='+location.href;</strong>
|
||||
<br><p><strong>javascript:location.href='http://old.omnireader.ru/?url='+location.href;</strong>
|
||||
<br>Òîãäà, íàæàâ íà ïîëó÷èâøóþñÿ êíîïêó íà ëþáîé ñòðàíèöå èíòåðíåòà, âû àâòîìàòè÷åñêè îòêðîåòå åå â Omni Reader.
|
||||
|
||||
<br><br>Äëÿ Chrome íà Android ìîæíî âûçûâàòü çàêëàäêó ïî åå èìåíè (èìÿ ñòîèò ñäåëàòü ïîïðîùå) â àäðåñíîé ñòðîêå áðàóçåðà, ïîñêîëüêó ñòàíäàðòíûé âûçîâ òàêîé çàêëàäêè íå ðàáîòàåò.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Liberama",
|
||||
"version": "0.3.4",
|
||||
"version": "0.4.7",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
|
||||
96
server/core/BookConverter/ConvertBase.js
Normal file
96
server/core/BookConverter/ConvertBase.js
Normal file
@@ -0,0 +1,96 @@
|
||||
const iconv = require('iconv-lite');
|
||||
const chardet = require('chardet');
|
||||
const textUtils = require('./textUtils');
|
||||
|
||||
class ConvertBase {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
run(data, opts) {// eslint-disable-line no-unused-vars
|
||||
//override
|
||||
}
|
||||
|
||||
decode(data) {
|
||||
let selected = textUtils.getEncoding(data);
|
||||
|
||||
if (selected == 'ISO-8859-5') {
|
||||
const charsetAll = chardet.detectAll(data.slice(0, 20000));
|
||||
for (const charset of charsetAll) {
|
||||
if (charset.name.indexOf('ISO-8859') < 0) {
|
||||
selected = charset.name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (selected.toLowerCase() != 'utf-8')
|
||||
return iconv.decode(data, selected);
|
||||
else
|
||||
return data;
|
||||
}
|
||||
|
||||
repSpaces(text) {
|
||||
return text.replace(/ |[\t\n\r]/g, ' ');
|
||||
}
|
||||
|
||||
formatFb2(fb2) {
|
||||
let out = '<?xml version="1.0" encoding="utf-8"?>';
|
||||
out += '<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">';
|
||||
out += this.formatFb2Node(fb2);
|
||||
out += '</FictionBook>';
|
||||
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') {
|
||||
if (name)
|
||||
out += `<${name}>${this.repSpaces(node)}</${name}>`;
|
||||
else
|
||||
out += this.repSpaces(node);
|
||||
} else {
|
||||
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)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConvertBase;
|
||||
41
server/core/BookConverter/ConvertFb2.js
Normal file
41
server/core/BookConverter/ConvertFb2.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const ConvertBase = require('./ConvertBase');
|
||||
const iconv = require('iconv-lite');
|
||||
|
||||
class ConvertFb2 extends ConvertBase {
|
||||
check(data, opts) {
|
||||
const {fileType} = opts;
|
||||
|
||||
return (fileType && fileType.ext == 'xml' && data.toString().indexOf('<FictionBook') >= 0);
|
||||
}
|
||||
|
||||
run(data, opts) {
|
||||
if (!this.check(data, opts))
|
||||
return false;
|
||||
|
||||
return this.checkEncoding(data);
|
||||
}
|
||||
|
||||
checkEncoding(data) {
|
||||
let result = data;
|
||||
|
||||
const left = data.indexOf('<?xml version="1.0"');
|
||||
if (left >= 0) {
|
||||
const right = data.indexOf('?>', left);
|
||||
if (right >= 0) {
|
||||
const head = data.slice(left, right + 2).toString();
|
||||
const m = head.match(/encoding="(.*)"/);
|
||||
if (m) {
|
||||
let encoding = m[1].toLowerCase();
|
||||
if (encoding != 'utf-8') {
|
||||
result = iconv.decode(data, encoding);
|
||||
result = Buffer.from(result.toString().replace(m[0], 'encoding="utf-8"'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConvertFb2;
|
||||
162
server/core/BookConverter/ConvertHtml.js
Normal file
162
server/core/BookConverter/ConvertHtml.js
Normal file
@@ -0,0 +1,162 @@
|
||||
const ConvertBase = require('./ConvertBase');
|
||||
const sax = require('./sax');
|
||||
const textUtils = require('./textUtils');
|
||||
|
||||
class ConvertHtml extends ConvertBase {
|
||||
check(data, opts) {
|
||||
const {fileType} = opts;
|
||||
|
||||
if (fileType && (fileType.ext == 'html' || fileType.ext == 'xml'))
|
||||
return {isText: false};
|
||||
|
||||
//может это чистый текст?
|
||||
if (textUtils.checkIfText(data)) {
|
||||
return {isText: true};
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
run(data, opts) {
|
||||
const checkResult = this.check(data, opts);
|
||||
if (!checkResult)
|
||||
return false;
|
||||
|
||||
let {isText} = checkResult;
|
||||
let titleInfo = {};
|
||||
let desc = {_n: 'description', 'title-info': titleInfo};
|
||||
let pars = [];
|
||||
let body = {_n: 'body', section: {_a: []}};
|
||||
let fb2 = [desc, body];
|
||||
|
||||
let title = '';
|
||||
let inTitle = false;
|
||||
|
||||
let spaceCounter = [];
|
||||
|
||||
const repCrLfTab = (text) => text.replace(/[\n\r]/g, '').replace(/\t/g, ' ');
|
||||
|
||||
const newParagraph = () => {
|
||||
pars.push({_n: 'p', _t: ''});
|
||||
};
|
||||
|
||||
const growParagraph = (text) => {
|
||||
if (!pars.length)
|
||||
newParagraph();
|
||||
|
||||
const l = pars.length;
|
||||
if (pars[l - 1]._t == '')
|
||||
text = text.trimLeft();
|
||||
pars[l - 1]._t += text;
|
||||
|
||||
//посчитаем отступы у текста, чтобы выделить потом параграфы
|
||||
const lines = text.split('\n');
|
||||
for (let line of lines) {
|
||||
line = repCrLfTab(line)
|
||||
|
||||
let l = 0;
|
||||
while (l < line.length && line[l] == ' ') {
|
||||
l++;
|
||||
}
|
||||
if (!spaceCounter[l])
|
||||
spaceCounter[l] = 0;
|
||||
spaceCounter[l]++;
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
if (!cutCounter) {
|
||||
growParagraph(text);
|
||||
}
|
||||
|
||||
if (inTitle && !title)
|
||||
title = text;
|
||||
};
|
||||
|
||||
const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||
if (!cutCounter) {
|
||||
if (newPara.has(tag))
|
||||
newParagraph();
|
||||
}
|
||||
|
||||
if (tag == 'title')
|
||||
inTitle = true;
|
||||
};
|
||||
|
||||
const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||
if (tag == 'title')
|
||||
inTitle = false;
|
||||
};
|
||||
|
||||
let buf = this.decode(data).toString();
|
||||
|
||||
sax.parseSync(buf, {
|
||||
onStartNode, onEndNode, onTextNode,
|
||||
innerCut: new Set(['head', 'script', 'style', 'binary'])
|
||||
});
|
||||
|
||||
titleInfo['book-title'] = title;
|
||||
|
||||
//подозрение на чистый текст, надо разбить на параграфы
|
||||
if (isText || pars.length < buf.length/2000) {
|
||||
let total = 0;
|
||||
for (let i = 0; i < spaceCounter.length; i++) {
|
||||
total += (spaceCounter[i] ? spaceCounter[i] : 0);
|
||||
}
|
||||
total /= 10;
|
||||
let i = spaceCounter.length - 1;
|
||||
while (i > 0 && (!spaceCounter[i] || spaceCounter[i] < total)) i--;
|
||||
|
||||
const parIndent = (i > 0 ? i : 0);
|
||||
|
||||
let newPars = [];
|
||||
const newPar = () => {
|
||||
newPars.push({_n: 'p', _t: ''});
|
||||
};
|
||||
|
||||
const growPar = (text) => {
|
||||
if (!newPars.length)
|
||||
newPar();
|
||||
|
||||
const l = newPars.length;
|
||||
newPars[l - 1]._t += text;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (const par of pars) {
|
||||
if (i > 0)
|
||||
newPar();
|
||||
i++;
|
||||
|
||||
const lines = par._t.split('\n');
|
||||
for (let line of lines) {
|
||||
line = repCrLfTab(line);
|
||||
|
||||
let l = 0;
|
||||
while (l < line.length && line[l] == ' ') {
|
||||
l++;
|
||||
}
|
||||
|
||||
if (l >= parIndent)
|
||||
newPar();
|
||||
growPar(line.trim() + ' ');
|
||||
}
|
||||
}
|
||||
|
||||
body.section._a[0] = newPars;
|
||||
} else {
|
||||
body.section._a[0] = pars;
|
||||
}
|
||||
|
||||
//убираем лишнее
|
||||
for (let i = 0; i < pars.length; i++)
|
||||
pars[i]._t = this.repSpaces(pars[i]._t).trim();
|
||||
|
||||
return this.formatFb2(fb2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = ConvertHtml;
|
||||
275
server/core/BookConverter/ConvertSamlib.js
Normal file
275
server/core/BookConverter/ConvertSamlib.js
Normal file
@@ -0,0 +1,275 @@
|
||||
const _ = require('lodash');
|
||||
const URL = require('url').URL;
|
||||
|
||||
const sax = require('./sax');
|
||||
const ConvertBase = require('./ConvertBase');
|
||||
|
||||
class ConvertSamlib extends ConvertBase {
|
||||
check(data, opts) {
|
||||
const {url} = opts;
|
||||
|
||||
const parsedUrl = new URL(url);
|
||||
if (parsedUrl.hostname == 'samlib.ru' ||
|
||||
parsedUrl.hostname == 'budclub.ru' ||
|
||||
parsedUrl.hostname == 'zhurnal.lib.ru') {
|
||||
return {hostname: parsedUrl.hostname};
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
run(data, opts) {
|
||||
const checkResult = this.check(data, opts);
|
||||
if (!checkResult)
|
||||
return false;
|
||||
|
||||
const {hostname} = checkResult;
|
||||
let titleInfo = {};
|
||||
let desc = {_n: 'description', 'title-info': titleInfo};
|
||||
let pars = [];
|
||||
let body = {_n: 'body', section: {_a: pars}};
|
||||
let fb2 = [desc, body];
|
||||
|
||||
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, attrs) => {
|
||||
if (name == 'p')
|
||||
inPara = true;
|
||||
let n = {_n: name, _attrs: attrs, _a: [], _p: node};
|
||||
node._a.push(n);
|
||||
node = n;
|
||||
};
|
||||
|
||||
const closeTag = (name) => {
|
||||
if (name == 'p')
|
||||
inPara = false;
|
||||
if (node._p) {
|
||||
const exact = (node._n == name);
|
||||
node = node._p;
|
||||
if (!exact)
|
||||
closeTag(name);
|
||||
}
|
||||
};
|
||||
|
||||
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});
|
||||
};
|
||||
|
||||
const onStartNode = (elemName, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||
if (elemName == '')
|
||||
return;
|
||||
if (!inText) {
|
||||
path += '/' + elemName;
|
||||
tag = elemName;
|
||||
} else {
|
||||
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) {
|
||||
openTag('p');
|
||||
inJustify = true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const onEndNode = (elemName, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||
if (!inText) {
|
||||
const oldPath = path;
|
||||
let t = '';
|
||||
do {
|
||||
let i = path.lastIndexOf('/');
|
||||
t = path.substr(i + 1);
|
||||
path = path.substr(0, i);
|
||||
} while (t != elemName && path);
|
||||
|
||||
if (t != elemName) {
|
||||
path = oldPath;
|
||||
}
|
||||
|
||||
let i = path.lastIndexOf('/');
|
||||
tag = path.substr(i + 1);
|
||||
} else {
|
||||
switch (elemName) {
|
||||
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) {
|
||||
closeTag('p');
|
||||
inJustify = false;
|
||||
}
|
||||
break;
|
||||
case 'img':
|
||||
if (inImage)
|
||||
closeTag('image');
|
||||
inImage = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const onComment = (text) => {// eslint-disable-line no-unused-vars
|
||||
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.indexOf(' ') >= 0 ? ' ' : '');
|
||||
|
||||
if (!text)
|
||||
return;
|
||||
|
||||
switch (path) {
|
||||
case '/html/body/center/h2':
|
||||
titleInfo['book-title'] = text;
|
||||
return;
|
||||
case '/html/body/div/h3':
|
||||
if (!titleInfo.author)
|
||||
titleInfo.author = {};
|
||||
text = text.replace(':', '').trim().split(' ');
|
||||
if (text[0])
|
||||
titleInfo.author['last-name'] = text[0];
|
||||
if (text[1])
|
||||
titleInfo.author['first-name'] = text[1];
|
||||
if (text[2])
|
||||
titleInfo.author['middle-name'] = text[2];
|
||||
return;
|
||||
}
|
||||
|
||||
let tOpen = (bold ? '<strong>' : '');
|
||||
tOpen += (italic ? '<emphasis>' : '');
|
||||
let tClose = (italic ? '</emphasis>' : '');
|
||||
tClose += (bold ? '</strong>' : '');
|
||||
|
||||
if (inText)
|
||||
growParagraph(`${tOpen}${text}${tClose}`);
|
||||
};
|
||||
|
||||
sax.parseSync(this.decode(data).toString().replace(/ /g, ' '), {
|
||||
onStartNode, onEndNode, onTextNode, onComment,
|
||||
innerCut: new Set(['head', 'script', 'style'])
|
||||
});
|
||||
|
||||
//текст не найден на странице, обработать корректно не получилось
|
||||
if (!textFound)
|
||||
return false;
|
||||
|
||||
const title = (titleInfo['book-title'] ? titleInfo['book-title'] : '');
|
||||
let author = '';
|
||||
if (titleInfo.author) {
|
||||
author = _.compact([
|
||||
(titleInfo.author['last-name'] ? titleInfo.author['last-name'] : ''),
|
||||
(titleInfo.author['first-name'] ? titleInfo.author['first-name'] : ''),
|
||||
(titleInfo.author['middle-name'] ? titleInfo.author['middle-name'] : ''),
|
||||
]).join(' ');
|
||||
}
|
||||
|
||||
pars.unshift({_n: 'title', _a: [
|
||||
{_n: 'p', _t: author}, {_n: 'p', _t: ''},
|
||||
{_n: 'p', _t: title}, {_n: 'p', _t: ''},
|
||||
]})
|
||||
|
||||
return this.formatFb2(fb2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = ConvertSamlib;
|
||||
@@ -1,459 +1,45 @@
|
||||
const fs = require('fs-extra');
|
||||
const URL = require('url').URL;
|
||||
const iconv = require('iconv-lite');
|
||||
const chardet = require('chardet');
|
||||
const _ = require('lodash');
|
||||
const sax = require('./sax');
|
||||
const textUtils = require('./textUtils');
|
||||
|
||||
const FileDetector = require('../FileDetector');
|
||||
|
||||
const repSpaces = (text) => text.replace(/ |[\t\n\r]/g, ' ');
|
||||
//порядок важен
|
||||
const convertClassFactory = [
|
||||
require('./ConvertFb2'),
|
||||
require('./ConvertSamlib'),
|
||||
require('./ConvertHtml'),
|
||||
];
|
||||
|
||||
class BookConverter {
|
||||
constructor() {
|
||||
constructor(config) {
|
||||
this.detector = new FileDetector();
|
||||
|
||||
this.convertFactory = [];
|
||||
for (const convertClass of convertClassFactory) {
|
||||
this.convertFactory.push(new convertClass(config));
|
||||
}
|
||||
}
|
||||
|
||||
async convertToFb2(inputFile, outputFile, url, callback) {
|
||||
const fileType = await this.detector.detectFile(inputFile);
|
||||
|
||||
const data = await fs.readFile(inputFile);
|
||||
callback(100);
|
||||
|
||||
if (fileType && (fileType.ext == 'html' || fileType.ext == 'xml')) {
|
||||
if (data.toString().indexOf('<FictionBook') >= 0) {
|
||||
await fs.writeFile(outputFile, this.checkEncoding(data));
|
||||
return;
|
||||
let result = false;
|
||||
for (const convert of this.convertFactory) {
|
||||
result = convert.run(data, {inputFile, url, callback, fileType});
|
||||
if (result) {
|
||||
await fs.writeFile(outputFile, result);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const parsedUrl = new URL(url);
|
||||
if (parsedUrl.hostname == 'samlib.ru' ||
|
||||
parsedUrl.hostname == 'budclub.ru' ||
|
||||
parsedUrl.hostname == 'zhurnal.lib.ru') {
|
||||
await fs.writeFile(outputFile, this.convertSamlib(data));
|
||||
return;
|
||||
}
|
||||
|
||||
await fs.writeFile(outputFile, this.convertHtml(data));
|
||||
return;
|
||||
} else {
|
||||
if (!result) {
|
||||
if (fileType)
|
||||
throw new Error(`Этот формат файла не поддерживается: ${fileType.mime}`);
|
||||
else {
|
||||
//может это чистый текст?
|
||||
if (textUtils.checkIfText(data)) {
|
||||
await fs.writeFile(outputFile, this.convertHtml(data, true));
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(`Не удалось определить формат файла: ${url}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decode(data) {
|
||||
let selected = textUtils.getEncoding(data);
|
||||
|
||||
if (selected == 'ISO-8859-5') {
|
||||
const charsetAll = chardet.detectAll(data.slice(0, 20000));
|
||||
for (const charset of charsetAll) {
|
||||
if (charset.name.indexOf('ISO-8859') < 0) {
|
||||
selected = charset.name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return iconv.decode(data, selected);
|
||||
}
|
||||
|
||||
checkEncoding(data) {
|
||||
let result = data;
|
||||
|
||||
const left = data.indexOf('<?xml version="1.0"');
|
||||
if (left >= 0) {
|
||||
const right = data.indexOf('?>', left);
|
||||
if (right >= 0) {
|
||||
const head = data.slice(left, right + 2).toString();
|
||||
const m = head.match(/encoding="(.*)"/);
|
||||
if (m) {
|
||||
let encoding = m[1].toLowerCase();
|
||||
if (encoding != 'utf-8') {
|
||||
result = iconv.decode(data, encoding);
|
||||
result = Buffer.from(result.toString().replace(m[0], 'encoding="utf-8"'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
convertHtml(data, isText) {
|
||||
let titleInfo = {};
|
||||
let desc = {_n: 'description', 'title-info': titleInfo};
|
||||
let pars = [];
|
||||
let body = {_n: 'body', section: {_a: []}};
|
||||
let fb2 = [desc, body];
|
||||
|
||||
let title = '';
|
||||
let inTitle = false;
|
||||
|
||||
let spaceCounter = [];
|
||||
|
||||
const newParagraph = () => {
|
||||
pars.push({_n: 'p', _t: ''});
|
||||
};
|
||||
|
||||
const growParagraph = (text) => {
|
||||
const l = pars.length;
|
||||
if (l) {
|
||||
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(' ');
|
||||
let l = 0;
|
||||
while (l < sp.length && sp[l].trim() == '') {
|
||||
l++;
|
||||
}
|
||||
if (!spaceCounter[l])
|
||||
spaceCounter[l] = 0;
|
||||
spaceCounter[l]++;
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
if (!cutCounter) {
|
||||
growParagraph(text);
|
||||
}
|
||||
|
||||
if (inTitle && !title)
|
||||
title = text;
|
||||
};
|
||||
|
||||
const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||
if (!cutCounter) {
|
||||
if (newPara.has(tag))
|
||||
newParagraph();
|
||||
}
|
||||
|
||||
if (tag == 'title')
|
||||
inTitle = true;
|
||||
};
|
||||
|
||||
const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||
if (tag == 'title')
|
||||
inTitle = false;
|
||||
};
|
||||
|
||||
let buf = this.decode(data).toString();
|
||||
|
||||
sax.parseSync(buf, {
|
||||
onStartNode, onEndNode, onTextNode,
|
||||
innerCut: new Set(['head', 'script', 'style'])
|
||||
});
|
||||
|
||||
titleInfo['book-title'] = title;
|
||||
|
||||
//подозрение на чистый текст, надо разбить на параграфы
|
||||
if (isText || pars.length < buf.length/2000) {
|
||||
let total = 0;
|
||||
for (let i = 0; i < spaceCounter.length; i++) {
|
||||
total += (spaceCounter[i] ? spaceCounter[i] : 0);
|
||||
}
|
||||
total /= 10;
|
||||
let i = spaceCounter.length - 1;
|
||||
while (i > 0 && (!spaceCounter[i] || spaceCounter[i] < total)) i--;
|
||||
|
||||
const parIndent = (i > 0 ? i : 0);
|
||||
|
||||
let newPars = [];
|
||||
const newPar = () => {
|
||||
newPars.push({_n: 'p', _t: ''});
|
||||
};
|
||||
|
||||
const growPar = (text) => {
|
||||
const l = newPars.length;
|
||||
if (l) {
|
||||
newPars[l - 1]._t += text;
|
||||
}
|
||||
}
|
||||
|
||||
for (const par of pars) {
|
||||
newPar();
|
||||
|
||||
const lines = par._t.split('\n');
|
||||
for (const line of lines) {
|
||||
const sp = line.split(' ');
|
||||
let l = 0;
|
||||
while (l < sp.length && sp[l].trim() == '') {
|
||||
l++;
|
||||
}
|
||||
if (l >= parIndent)
|
||||
newPar();
|
||||
growPar(line.trim() + ' ');
|
||||
}
|
||||
}
|
||||
|
||||
body.section._a[0] = newPars;
|
||||
} else {
|
||||
body.section._a[0] = pars;
|
||||
}
|
||||
|
||||
//убираем лишнее
|
||||
for (let i = 0; i < pars.length; i++)
|
||||
pars[i]._t = repSpaces(pars[i]._t).trim();
|
||||
|
||||
return this.formatFb2(fb2);
|
||||
}
|
||||
|
||||
convertSamlib(data) {
|
||||
let titleInfo = {};
|
||||
let desc = {_n: 'description', 'title-info': titleInfo};
|
||||
let pars = [];
|
||||
let body = {_n: 'body', section: {_a: pars}};
|
||||
let fb2 = [desc, body];
|
||||
|
||||
let inSubtitle = false;
|
||||
let inJustify = true;
|
||||
let path = '';
|
||||
let tag = '';// eslint-disable-line no-unused-vars
|
||||
|
||||
let inText = false;
|
||||
let node = {_a: pars};
|
||||
|
||||
let inPara = false;
|
||||
let italic = false;
|
||||
let bold = false;
|
||||
|
||||
const openTag = (name) => {
|
||||
if (name == 'p')
|
||||
inPara = true;
|
||||
let n = {_n: name, _a: [], _p: node};
|
||||
node._a.push(n);
|
||||
node = n;
|
||||
};
|
||||
|
||||
const closeTag = (name) => {
|
||||
if (name == 'p')
|
||||
inPara = false;
|
||||
if (node._p) {
|
||||
const exact = (node._n == name);
|
||||
node = node._p;
|
||||
if (!exact)
|
||||
closeTag(name);
|
||||
}
|
||||
};
|
||||
|
||||
const growParagraph = (text) => {
|
||||
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;
|
||||
if (!inText) {
|
||||
path += '/' + elemName;
|
||||
tag = elemName;
|
||||
} else {
|
||||
if (inPara && elemName != 'i' && elemName != 'b')
|
||||
closeTag('p');
|
||||
|
||||
switch (elemName) {
|
||||
case 'li':
|
||||
case 'p':
|
||||
case 'dd':
|
||||
case 'h1':
|
||||
case 'h2':
|
||||
case 'h3':
|
||||
openTag('p');
|
||||
break;
|
||||
case 'i':
|
||||
italic = true;
|
||||
break;
|
||||
case 'b':
|
||||
bold = true;
|
||||
break;
|
||||
case 'div':
|
||||
if (tail.indexOf('align="center"') >= 0) {
|
||||
openTag('subtitle');
|
||||
inSubtitle = true;
|
||||
}
|
||||
|
||||
if (tail.indexOf('align="justify"') >= 0) {
|
||||
openTag('p');
|
||||
inJustify = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const onEndNode = (elemName, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||
if (!inText) {
|
||||
const oldPath = path;
|
||||
let t = '';
|
||||
do {
|
||||
let i = path.lastIndexOf('/');
|
||||
t = path.substr(i + 1);
|
||||
path = path.substr(0, i);
|
||||
} while (t != elemName && path);
|
||||
|
||||
if (t != elemName) {
|
||||
path = oldPath;
|
||||
}
|
||||
|
||||
let i = path.lastIndexOf('/');
|
||||
tag = path.substr(i + 1);
|
||||
} else {
|
||||
switch (elemName) {
|
||||
case 'li':
|
||||
case 'p':
|
||||
case 'dd':
|
||||
case 'h1':
|
||||
case 'h2':
|
||||
case 'h3':
|
||||
closeTag('p');
|
||||
break;
|
||||
case 'i':
|
||||
italic = false;
|
||||
break;
|
||||
case 'b':
|
||||
bold = false;
|
||||
break;
|
||||
case 'div':
|
||||
if (inSubtitle) {
|
||||
closeTag('subtitle');
|
||||
inSubtitle = false;
|
||||
}
|
||||
|
||||
if (inJustify) {
|
||||
closeTag('p');
|
||||
inJustify = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const onComment = (text) => {// eslint-disable-line no-unused-vars
|
||||
if (text == '--------- Собственно произведение -------------')
|
||||
inText = true;
|
||||
if (text == '-----------------------------------------------')
|
||||
inText = false;
|
||||
};
|
||||
|
||||
const onTextNode = (text) => {// eslint-disable-line no-unused-vars
|
||||
if (text != ' ' && text.trim() == '')
|
||||
text = text.trim();
|
||||
|
||||
if (text == '')
|
||||
return;
|
||||
|
||||
switch (path) {
|
||||
case '/html/body/center/h2':
|
||||
titleInfo['book-title'] = text;
|
||||
return;
|
||||
case '/html/body/div/h3':
|
||||
if (!titleInfo.author)
|
||||
titleInfo.author = {};
|
||||
text = text.replace(':', '').trim().split(' ');
|
||||
if (text[0])
|
||||
titleInfo.author['last-name'] = text[0];
|
||||
if (text[1])
|
||||
titleInfo.author['first-name'] = text[1];
|
||||
if (text[2])
|
||||
titleInfo.author['middle-name'] = text[2];
|
||||
return;
|
||||
}
|
||||
|
||||
let tOpen = (bold ? '<strong>' : '');
|
||||
tOpen += (italic ? '<emphasis>' : '');
|
||||
let tClose = (italic ? '</emphasis>' : '');
|
||||
tClose += (bold ? '</strong>' : '');
|
||||
|
||||
if (inText)
|
||||
growParagraph(`${tOpen}${text}${tClose}`);
|
||||
};
|
||||
|
||||
sax.parseSync(repSpaces(this.decode(data).toString()), {
|
||||
onStartNode, onEndNode, onTextNode, onComment,
|
||||
innerCut: new Set(['head', 'script', 'style'])
|
||||
});
|
||||
|
||||
const title = (titleInfo['book-title'] ? titleInfo['book-title'] : '');
|
||||
let author = '';
|
||||
if (titleInfo.author) {
|
||||
author = _.compact([
|
||||
(titleInfo.author['last-name'] ? titleInfo.author['last-name'] : ''),
|
||||
(titleInfo.author['first-name'] ? titleInfo.author['first-name'] : ''),
|
||||
(titleInfo.author['middle-name'] ? titleInfo.author['middle-name'] : ''),
|
||||
]).join(' ');
|
||||
}
|
||||
|
||||
pars.unshift({_n: 'title', _a: [
|
||||
{_n: 'p', _t: author}, {_n: 'p', _t: ''},
|
||||
{_n: 'p', _t: title}, {_n: 'p', _t: ''},
|
||||
]})
|
||||
|
||||
return this.formatFb2(fb2);
|
||||
}
|
||||
|
||||
formatFb2(fb2) {
|
||||
let out = '<?xml version="1.0" encoding="utf-8"?>';
|
||||
out += '<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">';
|
||||
out += this.formatFb2Node(fb2);
|
||||
out += '</FictionBook>';
|
||||
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') {
|
||||
if (name)
|
||||
out += `<${name}>${repSpaces(node)}</${name}>`;
|
||||
else
|
||||
out += repSpaces(node);
|
||||
} else {
|
||||
if (node._n)
|
||||
name = node._n;
|
||||
|
||||
if (name)
|
||||
out += `<${name}>`;
|
||||
if (node.hasOwnProperty('_t'))
|
||||
out += repSpaces(node._t);
|
||||
|
||||
for (let nodeName in node) {
|
||||
if (nodeName && nodeName[0] == '_' && nodeName != '_a')
|
||||
continue;
|
||||
|
||||
const n = node[nodeName];
|
||||
out += this.formatFb2Node(n, nodeName);
|
||||
}
|
||||
|
||||
if (name)
|
||||
out += `</${name}>`;
|
||||
}
|
||||
return out;
|
||||
callback(100);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -9,18 +9,18 @@ detect.addSignature(
|
||||
"rules": [
|
||||
{ "type": "or", "rules":
|
||||
[
|
||||
{ "type": "contains", "bytes": "3c68746d6c" },
|
||||
{ "type": "contains", "bytes": "3c00680074006d006c00" },
|
||||
{ "type": "equal", "end": 5, "bytes": "3c68746d6c" },
|
||||
{ "type": "equal", "end": 10, "bytes": "3c00680074006d006c00" },
|
||||
|
||||
{ "type": "contains", "bytes": "3c21646f6374797065" },
|
||||
{ "type": "contains", "bytes": "3c626f6479" },
|
||||
{ "type": "contains", "bytes": "3c68656164" },
|
||||
{ "type": "contains", "bytes": "3c696672616d65" },
|
||||
{ "type": "contains", "bytes": "3c696d67" },
|
||||
{ "type": "contains", "bytes": "3c6f626a656374" },
|
||||
{ "type": "contains", "bytes": "3c736372697074" },
|
||||
{ "type": "contains", "bytes": "3c7461626c65" },
|
||||
{ "type": "contains", "bytes": "3c7469746c65" },
|
||||
{ "type": "equal", "end": 9, "bytes": "3c21646f6374797065" },
|
||||
{ "type": "equal", "end": 5, "bytes": "3c626f6479" },
|
||||
{ "type": "equal", "end": 5, "bytes": "3c68656164" },
|
||||
{ "type": "equal", "end": 7, "bytes": "3c696672616d65" },
|
||||
{ "type": "equal", "end": 4, "bytes": "3c696d67" },
|
||||
{ "type": "equal", "end": 7, "bytes": "3c6f626a656374" },
|
||||
{ "type": "equal", "end": 7, "bytes": "3c736372697074" },
|
||||
{ "type": "equal", "end": 6, "bytes": "3c7461626c65" },
|
||||
{ "type": "equal", "end": 6, "bytes": "3c7469746c65" },
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -36,7 +36,7 @@ detect.addSignature(
|
||||
"rules": [
|
||||
{ "type": "or", "rules":
|
||||
[
|
||||
{ "type": "contains", "bytes": "3c3f786d6c2076657273696f6e3d22312e3022" },
|
||||
{ "type": "equal", "end": 19, "bytes": "3c3f786d6c2076657273696f6e3d22312e3022" },
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -22,7 +22,7 @@ class ReaderWorker {
|
||||
|
||||
this.down = new FileDownloader();
|
||||
this.decomp = new FileDecompressor();
|
||||
this.bookConverter = new BookConverter();
|
||||
this.bookConverter = new BookConverter(this.config);
|
||||
|
||||
if (!singleCleanExecute) {
|
||||
this.periodicCleanDir(this.config.tempPublicDir, this.config.maxTempPublicDirSize, 60*60*1000);//1 раз в час
|
||||
@@ -66,14 +66,14 @@ class ReaderWorker {
|
||||
const decompFilename = await this.decomp.decompressFile(downloadedFilename, decompDir);
|
||||
wState.set({progress: 100});
|
||||
|
||||
//parse book
|
||||
//конвертирование в fb2
|
||||
wState.set({state: 'convert', step: 3, progress: 0});
|
||||
convertFilename = `${this.config.tempDownloadDir}/${tempFilename2}`;
|
||||
await this.bookConverter.convertToFb2(decompFilename, convertFilename, url, progress => {
|
||||
wState.set({progress});
|
||||
});
|
||||
|
||||
//compress file to tmp dir, if not exists with the same hashname
|
||||
//сжимаем файл в tmp, если там уже нет с тем же именем-sha256
|
||||
const compFilename = await this.decomp.gzipFileIfNotExists(convertFilename, `${this.config.tempPublicDir}`);
|
||||
|
||||
wState.set({progress: 100});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const config = require('./config');
|
||||
const {initLogger, getLog} = require('./core/getLogger');
|
||||
initLogger(config);
|
||||
const log = getLog();
|
||||
const logger = require('./core/getLogger');
|
||||
logger.initLogger(config);
|
||||
const log = logger.getLog();
|
||||
|
||||
const configSaver = require('./config/configSaver');
|
||||
const argv = require('minimist')(process.argv.slice(2));
|
||||
@@ -31,6 +31,7 @@ async function init() {
|
||||
}
|
||||
|
||||
async function main() {
|
||||
log(`${config.name} v${config.version}`);
|
||||
log('Initializing');
|
||||
await init();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user