Lightnovelpub: use regex for parseChapter (#1161)
This commit is contained in:
@@ -7,7 +7,7 @@ import dayjs from 'dayjs';
|
|||||||
class LightNovelPub implements Plugin.PagePlugin {
|
class LightNovelPub implements Plugin.PagePlugin {
|
||||||
id = 'lightnovelpub';
|
id = 'lightnovelpub';
|
||||||
name = 'LightNovelPub';
|
name = 'LightNovelPub';
|
||||||
version = '2.0.0';
|
version = '2.1.0';
|
||||||
icon = 'src/en/lightnovelpub/icon.png';
|
icon = 'src/en/lightnovelpub/icon.png';
|
||||||
site = 'https://www.lightnovelpub.com/';
|
site = 'https://www.lightnovelpub.com/';
|
||||||
headers = {
|
headers = {
|
||||||
@@ -236,102 +236,20 @@ class LightNovelPub implements Plugin.PagePlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async parseChapter(chapterPath: string): Promise<string> {
|
async parseChapter(chapterPath: string): Promise<string> {
|
||||||
const body = await fetchApi(this.site + chapterPath).then(r => r.text());
|
const html = await fetchApi(this.site + chapterPath).then(r => r.text());
|
||||||
|
const parts: string[] = [];
|
||||||
let chapterText = '';
|
const regexPatterns: RegExp[] = [
|
||||||
let isChapter = false;
|
/(<div id="chapter-container[^]+?)<div class="chapternav/,
|
||||||
let isPtag = false;
|
];
|
||||||
let isStyleText = false;
|
const extractContent = (patterns: RegExp[]) => {
|
||||||
const parser = new Parser({
|
patterns.forEach(regex => {
|
||||||
onopentag(name, attribs) {
|
const match = html.match(regex)?.[1];
|
||||||
if (isChapter && name === 'div') {
|
if (match) parts.push(match);
|
||||||
const stylediv = attribs['style'];
|
});
|
||||||
if (stylediv) {
|
};
|
||||||
chapterText += `<div style="${stylediv}">`;
|
extractContent(regexPatterns);
|
||||||
isStyleText = true;
|
const chapterText = parts.join();
|
||||||
} else {
|
return chapterText.replace(/(?<=\/p>).*?(?=<p>)/g, '');
|
||||||
chapterText += `<div>`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'table') {
|
|
||||||
const w = attribs['width'];
|
|
||||||
if (w) {
|
|
||||||
chapterText += `<table width="${w}">`;
|
|
||||||
} else {
|
|
||||||
chapterText += `<table>`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'tbody') {
|
|
||||||
chapterText += `<tbody>`;
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'tr') {
|
|
||||||
chapterText += `<tr>`;
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'td') {
|
|
||||||
const w1 = attribs['width'];
|
|
||||||
if (w1) {
|
|
||||||
chapterText += `<td width="${w1}">`;
|
|
||||||
} else {
|
|
||||||
chapterText += `<td>`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onattribute(name, value) {
|
|
||||||
if (name === 'id' && value === 'chapter-container') {
|
|
||||||
isChapter = true;
|
|
||||||
}
|
|
||||||
if (name === 'class' && value?.includes('chapternav')) {
|
|
||||||
isChapter = false;
|
|
||||||
isPtag = false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onopentagname(name) {
|
|
||||||
if (isChapter && name === 'p') {
|
|
||||||
chapterText += '<p>';
|
|
||||||
isPtag = true;
|
|
||||||
if (isStyleText) {
|
|
||||||
isStyleText = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'br') {
|
|
||||||
chapterText += `<br>`;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ontext(data) {
|
|
||||||
if (isPtag) {
|
|
||||||
chapterText += data;
|
|
||||||
}
|
|
||||||
if (isStyleText) {
|
|
||||||
chapterText += data;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onclosetag(name) {
|
|
||||||
if (name === 'p') {
|
|
||||||
isPtag = false;
|
|
||||||
chapterText += '</p>';
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'td') {
|
|
||||||
chapterText += `</td>`;
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'tr') {
|
|
||||||
chapterText += `</tr>`;
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'tbody') {
|
|
||||||
chapterText += `</tbody>`;
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'table') {
|
|
||||||
chapterText += `</table>`;
|
|
||||||
}
|
|
||||||
if (isChapter && name === 'div') {
|
|
||||||
isStyleText = false;
|
|
||||||
chapterText += `</div>`;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
parser.write(body);
|
|
||||||
parser.end();
|
|
||||||
|
|
||||||
return chapterText;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {
|
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { NovelStatus } from '@libs/novelStatus';
|
|||||||
class RoyalRoad implements Plugin.PluginBase {
|
class RoyalRoad implements Plugin.PluginBase {
|
||||||
id = 'royalroad';
|
id = 'royalroad';
|
||||||
name = 'Royal Road';
|
name = 'Royal Road';
|
||||||
version = '2.1.1';
|
version = '2.1.2';
|
||||||
icon = 'src/en/royalroad/icon.png';
|
icon = 'src/en/royalroad/icon.png';
|
||||||
site = 'https://www.royalroad.com/';
|
site = 'https://www.royalroad.com/';
|
||||||
|
|
||||||
@@ -234,10 +234,10 @@ class RoyalRoad implements Plugin.PluginBase {
|
|||||||
const html = await result.text();
|
const html = await result.text();
|
||||||
const parts: string[] = [];
|
const parts: string[] = [];
|
||||||
const regexPatterns: RegExp[] = [
|
const regexPatterns: RegExp[] = [
|
||||||
/<style>\n\s+.(.+?){[^]+speak: never;/,
|
/<style>\n\s+.(.+?){[^]+?display: none;/,
|
||||||
/(<div class="portlet solid author-note-portlet"[^]+)<div class="margin-bottom-20/,
|
/(<div class="portlet solid author-note-portlet"[^]+?)<div class="margin-bottom-20/,
|
||||||
/(<div class="chapter-inner chapter-content"[^]+)<div class="portlet light t-center-3/,
|
/(<div class="chapter-inner chapter-content"[^]+?)<div class="portlet light t-center-3/,
|
||||||
/(<\/div>\s+<div class="portlet solid author-note-portlet"[^]+)<div class="row margin-bottom-10/,
|
/(<\/div>\s+<div class="portlet solid author-note-portlet"[^]+?)<div class="row margin-bottom-10/,
|
||||||
];
|
];
|
||||||
|
|
||||||
const extractContent = (patterns: RegExp[]) => {
|
const extractContent = (patterns: RegExp[]) => {
|
||||||
@@ -247,11 +247,11 @@ class RoyalRoad implements Plugin.PluginBase {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
extractContent(regexPatterns);
|
extractContent(regexPatterns);
|
||||||
const cleanup = new RegExp(`<p class=\"${parts[0]}.+?<\/p>`, 'g');
|
const cleanup = new RegExp(`<p class="${parts[0]}.+?</p>`, 'g');
|
||||||
const chapterText = parts.slice(1).join('<hr>');
|
const chapterText = parts.slice(1).join('<hr>');
|
||||||
return chapterText
|
return chapterText
|
||||||
.replace(cleanup, '')
|
.replace(cleanup, '')
|
||||||
.replace(/<p class=\"[^><]+>/g, '<p>');
|
.replace(/<p class="[^><]+>/g, '<p>');
|
||||||
}
|
}
|
||||||
|
|
||||||
async searchNovels(
|
async searchNovels(
|
||||||
|
|||||||
Reference in New Issue
Block a user