-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbook.js
More file actions
190 lines (174 loc) · 6.64 KB
/
book.js
File metadata and controls
190 lines (174 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
const Nzh = require('nzh');
const nzhcn = require('nzh/cn');
const nzhhk = require('nzh/hk');
const SEP_CHAR = "`!@#$%^&*()+={}\\[\\]\\\\|;':\\\",.<>\/?~·!¥…()【】「」、;:,。《》?● ";
const EXP_SEP = new RegExp("[" + SEP_CHAR + "]", "gm");
const FULL_WITH_NUM = String.fromCodePoint(65296,65297,65298,65299,65300,65301,65302,65303,65304,65305); // 0-9
const SEQ_PATTERN = "第?\\s*([0-9" + FULL_WITH_NUM + "零〇一二三四五六七八九十百千万]+)\\s*[章节篇回]?";
const CH_NUM = "零〇一二三四五六七八九"
const CH_UNIT = "十百千万";
class Book {
constructor(dom, title) {
this.title = this.parseTitle(dom, title);
this.chapters = new Map();
this.deletedChapters = new Map();
this.parseChapters(dom);
}
parseTitle(dom, defaultTitle) {
let title = dom.querySelector("h1");
if (title) {
return title;
} else {
return defaultTitle;
}
}
parseAuthor(html) {
let match = html.match(/作者[::]?(.*)<br>/);
if (match && match.length > 1) {
this.author = match[1];
} else {
this.author = "SIS001";
}
}
parseDate(html) {
let match = html.match(/(\d+)年(\d+)月(?:(\d+)日)?|(\d+)[\-\/]{1}(\d+)[\-\/]{1}(\d+)/);
if (match) {
let y = match[1] || match[4], m = match[2] || match [5], d = match[3] || match[6];
if (!d) d = 0;
if (match.length > 3)
d = match[3];
this.date = new Date(y + "-" + m + "-" + d);
}
}
parsePublisher(html) {
let match = html.match(/(?:首发于|发表于)[::]?(.*)<br>/);
if (match) {
this.publisher = match[1];
if (this.publisher.toLowerCase() == "sis001") {
this.publisher = "第一会所";
}
}
}
parseChapters(dom) {
let tocdiv = dom.querySelector("div.toc");
Array.from(tocdiv.querySelectorAll("tr")).forEach((tr, i) => {
let link = tr.cells[0].querySelector("a");
if (link) {
let t = {};
t.id = link.hash.substring(1);
this.chapters.set(t.id, t); // put it first, in case content has sub chapter
t.indent = 0;
let span = link.parentNode.querySelector("span");
if (span) {
let indentx = span.classList.toString().match(/indent(\d)?/);
if (indentx) {
if (indentx[1] == null)
t.indent = 1;
else
t.indent = parseInt(indentx[1], 10);
if (t.indent == NaN)
t.indent = 1;
}
}
t.title = this.normalizeChapterTitle(link.innerText);
let div = dom.querySelector("div[id='" + t.id + "']");
if (!this.author) {
this.parseAuthor(div.innerHTML);
}
if (!this.date) {
this.parseDate(div.innerHTML);
}
if (!this.publisher) {
this.parsePublisher(div.innerHTML);
}
t.content = this.normalize(t, div.innerHTML);
t.ignore = t.indent && t.content.length < 200;
t.order = i * 10000 + this.parseOrder(t.title);
}
});
let sortedChapters = new Map();
Array.from(this.chapters.values()).sort((a, b) => a.order - b.order).map(c => sortedChapters.set(c.id, c));
this.chapters = sortedChapters;
}
get(id) {
this.chapters.get(id);
}
delete(id) {
this.deletedChapters.set(id, this.chapters.get(id));
this.chapters.delete(id);
}
normalize(chapter, html) {
html = html.replace(/<div class="title">.*<\/div>/g, "");
html = html.replace(/<a href="#top">Back<\/a>/g, "");
html = html.replace(/\[.*本帖最后由.*编辑.*\]/g, "");
html = html.replace(/<\/?\w+\s*[^>]*>/g, "");
html = html.replace(/ /g, "");
html = html.trim().replace(/^\s*$/mg, "</p><p>");
html = html.replace(/[\r\n]+/g, "");
html = html.replace(/\s*<\/p>/g, "</p>");
html = html.replace(/<p>\s*/g, "<p>");
html = html.replace(/<p><\/p>/g, "");
html = html.replace(/<\/p><p>/g, "</p>\n<p>");
html = "<p>" + html + "</p>";
// find possible chapters in content
let result = null;
let match, lastMatch;
let cnt = 1;
let exp = new RegExp("^<p>" + SEQ_PATTERN + "<\\/p>$", "gm");
while((match = exp.exec(html)) !== null) {
if (lastMatch) {
let t = {};
t.id = chapter.id + cnt++;
t.indent = chapter.indent + 1;
t.title = lastMatch[0].replace(/<\/?p>/g, "");
t.order = chapter.order + 1;
t.content = html.substring(lastMatch.index, match.index).replace(lastMatch[0] + "\n", "");
t.ignore = t.indent && t.content.length < 200;
this.chapters.set(t.id, t);
} else {
result = html.substring(0, match.index);
}
lastMatch = match;
}
if (lastMatch) {
let t = {};
t.id = chapter.id + cnt++;
t.indent = chapter.indent + 1;
t.title = lastMatch[0].replace(/<\/?p>/g, "");;
t.order = chapter.order + 1;
t.content = html.substring(lastMatch.index);
t.ignore = t.indent && t.content.length < 200;
this.chapters.set(t.id, t);
}
if (result == null)
result = html;
return result;
}
normalizeChapterTitle(title) {
let reg = new RegExp("[" + SEP_CHAR + "]?" + this.title + "\\1");
return title.replace(reg, "").replace(EXP_SEP, ' ').trim();
}
parseOrder(title) {
let match = title.match(new RegExp(SEQ_PATTERN, "g"));
if (match) {
if (match.length > 0) {
let seq = match[0];
let i = nzhcn.decodeS(seq);
if (i == 0)
i = nzhcn.decodeB(seq);
if (i == 0)
i = nzhhk.decodeS(seq);
if (i == 0)
i = nzhhk.decodeB(seq);
if (i == 0)
i = parseInt(seq, 10);
if (i == NaN)
i = 0;
return i;
}
}
return 0;
}
}
module.exports = Book;