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