wikiparser-node 0.4.0 → 0.6.2-b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/bundle.min.js +34 -0
- package/package.json +12 -5
- package/.eslintrc.json +0 -714
- package/README.md +0 -39
- package/config/default.json +0 -769
- package/config/llwiki.json +0 -630
- package/config/moegirl.json +0 -727
- package/config/zhwiki.json +0 -1269
- package/errors/README +0 -1
- package/index.js +0 -245
- package/jsconfig.json +0 -7
- package/lib/element.js +0 -755
- package/lib/node.js +0 -585
- package/lib/ranges.js +0 -131
- package/lib/text.js +0 -146
- package/lib/title.js +0 -69
- package/mixin/attributeParent.js +0 -113
- package/mixin/fixedToken.js +0 -40
- package/mixin/hidden.js +0 -19
- package/mixin/sol.js +0 -59
- package/parser/brackets.js +0 -112
- package/parser/commentAndExt.js +0 -63
- package/parser/converter.js +0 -45
- package/parser/externalLinks.js +0 -31
- package/parser/hrAndDoubleUnderscore.js +0 -35
- package/parser/html.js +0 -42
- package/parser/links.js +0 -98
- package/parser/list.js +0 -59
- package/parser/magicLinks.js +0 -41
- package/parser/quotes.js +0 -64
- package/parser/selector.js +0 -175
- package/parser/table.js +0 -112
- package/printed/README +0 -1
- package/printed/example.json +0 -120
- package/src/arg.js +0 -169
- package/src/atom/hidden.js +0 -13
- package/src/atom/index.js +0 -41
- package/src/attribute.js +0 -422
- package/src/converter.js +0 -157
- package/src/converterFlags.js +0 -232
- package/src/converterRule.js +0 -253
- package/src/extLink.js +0 -167
- package/src/gallery.js +0 -91
- package/src/heading.js +0 -100
- package/src/html.js +0 -202
- package/src/imageParameter.js +0 -254
- package/src/index.js +0 -737
- package/src/link/category.js +0 -53
- package/src/link/file.js +0 -265
- package/src/link/galleryImage.js +0 -61
- package/src/link/index.js +0 -322
- package/src/magicLink.js +0 -108
- package/src/nowiki/comment.js +0 -57
- package/src/nowiki/dd.js +0 -59
- package/src/nowiki/doubleUnderscore.js +0 -51
- package/src/nowiki/hr.js +0 -41
- package/src/nowiki/index.js +0 -44
- package/src/nowiki/list.js +0 -16
- package/src/nowiki/noinclude.js +0 -28
- package/src/nowiki/quote.js +0 -36
- package/src/onlyinclude.js +0 -54
- package/src/parameter.js +0 -187
- package/src/syntax.js +0 -83
- package/src/table/index.js +0 -967
- package/src/table/td.js +0 -308
- package/src/table/tr.js +0 -282
- package/src/tagPair/ext.js +0 -105
- package/src/tagPair/include.js +0 -50
- package/src/tagPair/index.js +0 -117
- package/src/transclude.js +0 -703
- package/test/api.js +0 -83
- package/test/real.js +0 -133
- package/test/test.js +0 -28
- package/test/util.js +0 -80
- package/tool/index.js +0 -918
- package/typings/api.d.ts +0 -13
- package/typings/array.d.ts +0 -28
- package/typings/event.d.ts +0 -24
- package/typings/index.d.ts +0 -94
- package/typings/node.d.ts +0 -29
- package/typings/parser.d.ts +0 -16
- package/typings/table.d.ts +0 -14
- package/typings/token.d.ts +0 -22
- package/typings/tool.d.ts +0 -11
- package/util/debug.js +0 -73
- package/util/string.js +0 -88
package/src/index.js
DELETED
|
@@ -1,737 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* PHP解析器的步骤:
|
|
5
|
-
* -1. 替换签名和`{{subst:}}`,参见Parser::preSaveTransform;这在revision中不可能保留,可以跳过
|
|
6
|
-
* 0. 移除特定字符`\0`和`\x7f`,参见Parser::parse
|
|
7
|
-
* 1. 注释/扩展标签('<'相关),参见Preprocessor_Hash::buildDomTreeArrayFromText和Sanitizer::decodeTagAttributes
|
|
8
|
-
* 2. 模板/模板变量/标题,注意rightmost法则,以及`-{`和`[[`可以破坏`{{`或`{{{`语法,
|
|
9
|
-
* 参见Preprocessor_Hash::buildDomTreeArrayFromText
|
|
10
|
-
* 3. HTML标签(允许不匹配),参见Sanitizer::internalRemoveHtmlTags
|
|
11
|
-
* 4. 表格,参见Parser::handleTables
|
|
12
|
-
* 5. 水平线和状态开关,参见Parser::internalParse
|
|
13
|
-
* 6. 内链,含文件和分类,参见Parser::handleInternalLinks2
|
|
14
|
-
* 7. `'`,参见Parser::doQuotes
|
|
15
|
-
* 8. 外链,参见Parser::handleExternalLinks
|
|
16
|
-
* 9. ISBN、RFC(未来将废弃,不予支持)和自由外链,参见Parser::handleMagicLinks
|
|
17
|
-
* 10. 段落和列表,参见BlockLevelPass::execute
|
|
18
|
-
* 11. 转换,参见LanguageConverter::recursiveConvertTopLevel
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/*
|
|
22
|
-
* \0\d+.\x7f标记Token:
|
|
23
|
-
* e: ExtToken
|
|
24
|
-
* c: CommentToken、NoIncludeToken和IncludeToken
|
|
25
|
-
* !: `{{!}}`专用
|
|
26
|
-
* {: `{{(!}}`专用
|
|
27
|
-
* }: `{{!)}}`专用
|
|
28
|
-
* -: `{{!-}}`专用
|
|
29
|
-
* +: `{{!!}}`专用
|
|
30
|
-
* ~: `{{=}}`专用
|
|
31
|
-
* t: ArgToken或TranscludeToken
|
|
32
|
-
* h: HeadingToken
|
|
33
|
-
* x: HtmlToken
|
|
34
|
-
* b: TableToken
|
|
35
|
-
* r: HrToken
|
|
36
|
-
* u: DoubleUnderscoreToken
|
|
37
|
-
* l: LinkToken
|
|
38
|
-
* q: QuoteToken
|
|
39
|
-
* w: ExtLinkToken
|
|
40
|
-
* d: ListToken
|
|
41
|
-
* v: ConverterToken
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
const {externalUse} = require('../util/debug'),
|
|
45
|
-
{text} = require('../util/string'),
|
|
46
|
-
Ranges = require('../lib/ranges'),
|
|
47
|
-
assert = require('assert/strict'),
|
|
48
|
-
Parser = require('..'),
|
|
49
|
-
AstElement = require('../lib/element'),
|
|
50
|
-
AstText = require('../lib/text');
|
|
51
|
-
const {MAX_STAGE, aliases} = Parser;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 所有节点的基类
|
|
55
|
-
* @classdesc `{childNodes: ...(AstText|Token)}`
|
|
56
|
-
*/
|
|
57
|
-
class Token extends AstElement {
|
|
58
|
-
type = 'root';
|
|
59
|
-
#stage = 0; // 解析阶段,参见顶部注释。只对plain Token有意义。
|
|
60
|
-
#config;
|
|
61
|
-
// 这个数组起两个作用:1. 数组中的Token会在build时替换`/\0\d+.\x7f/`标记;2. 数组中的Token会依次执行parseOnce和build方法。
|
|
62
|
-
#accum;
|
|
63
|
-
/** @type {Record<string, Ranges>} */ #acceptable;
|
|
64
|
-
#protectedChildren = new Ranges();
|
|
65
|
-
/** @type {boolean} */ #include;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 保护部分子节点不被移除
|
|
69
|
-
* @param {...string|number|Range} args 子节点范围
|
|
70
|
-
*/
|
|
71
|
-
#protectChildren = (...args) => {
|
|
72
|
-
this.#protectedChildren.push(...new Ranges(args));
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* 将维基语法替换为占位符
|
|
77
|
-
* @param {number} n 解析阶段
|
|
78
|
-
* @param {boolean} include 是否嵌入
|
|
79
|
-
*/
|
|
80
|
-
#parseOnce = (n = this.#stage, include = false) => {
|
|
81
|
-
if (n < this.#stage || !this.isPlain() || this.childNodes.length === 0) {
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
switch (n) {
|
|
85
|
-
case 0:
|
|
86
|
-
if (this.type === 'root') {
|
|
87
|
-
this.#accum.shift();
|
|
88
|
-
}
|
|
89
|
-
this.#parseCommentAndExt(include);
|
|
90
|
-
break;
|
|
91
|
-
case 1:
|
|
92
|
-
this.#parseBrackets();
|
|
93
|
-
break;
|
|
94
|
-
case 2:
|
|
95
|
-
this.#parseHtml();
|
|
96
|
-
break;
|
|
97
|
-
case 3:
|
|
98
|
-
this.#parseTable();
|
|
99
|
-
break;
|
|
100
|
-
case 4:
|
|
101
|
-
this.#parseHrAndDoubleUndescore();
|
|
102
|
-
break;
|
|
103
|
-
case 5:
|
|
104
|
-
this.#parseLinks();
|
|
105
|
-
break;
|
|
106
|
-
case 6:
|
|
107
|
-
this.#parseQuotes();
|
|
108
|
-
break;
|
|
109
|
-
|
|
110
|
-
case 7:
|
|
111
|
-
this.#parseExternalLinks();
|
|
112
|
-
break;
|
|
113
|
-
case 8:
|
|
114
|
-
this.#parseMagicLinks();
|
|
115
|
-
break;
|
|
116
|
-
case 9:
|
|
117
|
-
this.#parseList();
|
|
118
|
-
break;
|
|
119
|
-
case 10:
|
|
120
|
-
this.#parseConverter();
|
|
121
|
-
// no default
|
|
122
|
-
}
|
|
123
|
-
if (this.type === 'root') {
|
|
124
|
-
for (const token of this.#accum) {
|
|
125
|
-
token.getAttribute('parseOnce')(n, include);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
this.#stage++;
|
|
129
|
-
return this;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* 重建wikitext
|
|
134
|
-
* @param {string} str 半解析的字符串
|
|
135
|
-
* @complexity `n`
|
|
136
|
-
* @returns {(Token|AstText)[]}
|
|
137
|
-
*/
|
|
138
|
-
#buildFromStr = str => str.split(/[\0\x7F]/u).map((s, i) => {
|
|
139
|
-
if (i % 2 === 0) {
|
|
140
|
-
return new AstText(s);
|
|
141
|
-
} else if (isNaN(s.at(-1))) {
|
|
142
|
-
return this.#accum[Number(s.slice(0, -1))];
|
|
143
|
-
}
|
|
144
|
-
throw new Error(`解析错误!未正确标记的 Token:${s}`);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
/** 所有图片,包括图库 */
|
|
148
|
-
get images() {
|
|
149
|
-
return this.querySelectorAll('file, gallery-image');
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** 所有内链、外链和自由外链 */
|
|
153
|
-
get links() {
|
|
154
|
-
return this.querySelectorAll('link, ext-link, free-ext-link');
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** 所有模板和模块 */
|
|
158
|
-
get embeds() {
|
|
159
|
-
return this.querySelectorAll('template, magic-word#invoke');
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* @param {string} wikitext wikitext
|
|
164
|
-
* @param {accum} accum
|
|
165
|
-
* @param {acceptable} acceptable 可接受的子节点设置
|
|
166
|
-
*/
|
|
167
|
-
constructor(wikitext, config = Parser.getConfig(), halfParsed = false, accum = [], acceptable = null) {
|
|
168
|
-
super();
|
|
169
|
-
if (typeof wikitext === 'string') {
|
|
170
|
-
this.appendChild(halfParsed ? wikitext : wikitext.replaceAll('\0', '').replaceAll('\x7F', ''));
|
|
171
|
-
}
|
|
172
|
-
this.#config = config;
|
|
173
|
-
this.#accum = accum;
|
|
174
|
-
this.setAttribute('acceptable', acceptable);
|
|
175
|
-
accum.push(this);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* 深拷贝所有子节点
|
|
180
|
-
* @complexity `n`
|
|
181
|
-
* @returns {(AstText|Token)[]}
|
|
182
|
-
*/
|
|
183
|
-
cloneChildNodes() {
|
|
184
|
-
return this.childNodes.map(child => child.cloneNode());
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* 深拷贝节点
|
|
189
|
-
* @complexity `n`
|
|
190
|
-
* @throws `Error` 未定义复制方法
|
|
191
|
-
*/
|
|
192
|
-
cloneNode() {
|
|
193
|
-
if (!this.isPlain()) {
|
|
194
|
-
throw new Error(`未定义 ${this.constructor.name} 的复制方法!`);
|
|
195
|
-
}
|
|
196
|
-
const cloned = this.cloneChildNodes();
|
|
197
|
-
return Parser.run(() => {
|
|
198
|
-
const token = new Token(undefined, this.#config, false, [], this.#acceptable);
|
|
199
|
-
token.type = this.type;
|
|
200
|
-
token.append(...cloned);
|
|
201
|
-
token.getAttribute('protectChildren')(...this.#protectedChildren);
|
|
202
|
-
return token;
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* @override
|
|
208
|
-
* @template {string} T
|
|
209
|
-
* @param {T} key 属性键
|
|
210
|
-
* @returns {TokenAttribute<T>}
|
|
211
|
-
*/
|
|
212
|
-
getAttribute(key) {
|
|
213
|
-
switch (key) {
|
|
214
|
-
case 'stage':
|
|
215
|
-
return this.#stage;
|
|
216
|
-
case 'config':
|
|
217
|
-
return structuredClone(this.#config);
|
|
218
|
-
case 'accum':
|
|
219
|
-
return this.#accum;
|
|
220
|
-
case 'acceptable':
|
|
221
|
-
return this.#acceptable ? {...this.#acceptable} : null;
|
|
222
|
-
case 'protectChildren':
|
|
223
|
-
return this.#protectChildren;
|
|
224
|
-
case 'protectedChildren':
|
|
225
|
-
return new Ranges(this.#protectedChildren);
|
|
226
|
-
case 'parseOnce':
|
|
227
|
-
return this.#parseOnce;
|
|
228
|
-
case 'buildFromStr':
|
|
229
|
-
return this.#buildFromStr;
|
|
230
|
-
case 'include': {
|
|
231
|
-
if (this.#include !== undefined) {
|
|
232
|
-
return this.#include;
|
|
233
|
-
}
|
|
234
|
-
const root = this.getRootNode();
|
|
235
|
-
if (root.type === 'root' && root !== this) {
|
|
236
|
-
return root.getAttribute('include');
|
|
237
|
-
}
|
|
238
|
-
const includeToken = root.querySelector('include');
|
|
239
|
-
if (includeToken) {
|
|
240
|
-
return includeToken.name === 'noinclude';
|
|
241
|
-
}
|
|
242
|
-
const noincludeToken = root.querySelector('noinclude');
|
|
243
|
-
return Boolean(noincludeToken) && !/^<\/?noinclude(?:\s[^>]*)?\/?>$/iu.test(String(noincludeToken));
|
|
244
|
-
}
|
|
245
|
-
default:
|
|
246
|
-
return super.getAttribute(key);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* @override
|
|
252
|
-
* @template {string} T
|
|
253
|
-
* @param {T} key 属性键
|
|
254
|
-
* @param {TokenAttribute<T>} value 属性值
|
|
255
|
-
* @throws `RangeError` 禁止手动指定私有属性
|
|
256
|
-
*/
|
|
257
|
-
setAttribute(key, value) {
|
|
258
|
-
if (key === 'include' || !Parser.running && (key === 'config' || key === 'accum')) {
|
|
259
|
-
throw new RangeError(`禁止手动指定私有的 #${key} 属性!`);
|
|
260
|
-
} else if (!Parser.debugging && (key === 'stage' || key === 'acceptable' || key === 'protectedChildren')
|
|
261
|
-
&& externalUse('setAttribute')
|
|
262
|
-
) {
|
|
263
|
-
throw new RangeError(`使用 ${this.constructor.name}.setAttribute 方法设置私有属性 #${key} 仅用于代码调试!`);
|
|
264
|
-
}
|
|
265
|
-
switch (key) {
|
|
266
|
-
case 'stage':
|
|
267
|
-
if (this.#stage === 0 && this.type === 'root') {
|
|
268
|
-
this.#accum.shift();
|
|
269
|
-
}
|
|
270
|
-
this.#stage = value;
|
|
271
|
-
return this;
|
|
272
|
-
case 'config':
|
|
273
|
-
this.#config = value;
|
|
274
|
-
return this;
|
|
275
|
-
case 'accum':
|
|
276
|
-
this.#accum = value;
|
|
277
|
-
return this;
|
|
278
|
-
case 'protectedChildren':
|
|
279
|
-
this.#protectedChildren = value;
|
|
280
|
-
return this;
|
|
281
|
-
case 'acceptable': {
|
|
282
|
-
const /** @type {acceptable} */ acceptable = {};
|
|
283
|
-
if (value) {
|
|
284
|
-
for (const [k, v] of Object.entries(value)) {
|
|
285
|
-
if (k.startsWith('Stage-')) {
|
|
286
|
-
for (let i = 0; i <= Number(k.slice(6)); i++) {
|
|
287
|
-
for (const type of aliases[i]) {
|
|
288
|
-
acceptable[type] = new Ranges(v);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
} else if (k[0] === '!') { // `!`项必须放在最后
|
|
292
|
-
delete acceptable[k.slice(1)];
|
|
293
|
-
} else {
|
|
294
|
-
acceptable[k] = new Ranges(v);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
this.#acceptable = value && acceptable;
|
|
299
|
-
return this;
|
|
300
|
-
}
|
|
301
|
-
default:
|
|
302
|
-
return super.setAttribute(key, value);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/** 是否是普通节点 */
|
|
307
|
-
isPlain() {
|
|
308
|
-
return this.constructor === Token;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* @override
|
|
313
|
-
* @param {number} i 移除位置
|
|
314
|
-
* @returns {Token}
|
|
315
|
-
* @complexity `n`
|
|
316
|
-
* @throws `Error` 不可移除的子节点
|
|
317
|
-
*/
|
|
318
|
-
removeAt(i) {
|
|
319
|
-
if (typeof i !== 'number') {
|
|
320
|
-
this.typeError('removeAt', 'Number');
|
|
321
|
-
}
|
|
322
|
-
const iPos = i < 0 ? i + this.childNodes.length : i;
|
|
323
|
-
if (!Parser.running) {
|
|
324
|
-
const protectedIndices = this.#protectedChildren.applyTo(this.childNodes);
|
|
325
|
-
if (protectedIndices.includes(iPos)) {
|
|
326
|
-
throw new Error(`${this.constructor.name} 的第 ${i} 个子节点不可移除!`);
|
|
327
|
-
} else if (this.#acceptable) {
|
|
328
|
-
const acceptableIndices = Object.fromEntries(
|
|
329
|
-
Object.entries(this.#acceptable)
|
|
330
|
-
.map(([str, ranges]) => [str, ranges.applyTo(this.childNodes.length - 1)]),
|
|
331
|
-
),
|
|
332
|
-
nodesAfter = i === -1 ? [] : this.childNodes.slice(i + 1);
|
|
333
|
-
if (nodesAfter.some(({constructor: {name}}, j) => !acceptableIndices[name].includes(i + j))) {
|
|
334
|
-
throw new Error(`移除 ${this.constructor.name} 的第 ${i} 个子节点会破坏规定的顺序!`);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
return super.removeAt(i);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* @override
|
|
343
|
-
* @template {string|Token} T
|
|
344
|
-
* @param {T} token 待插入的子节点
|
|
345
|
-
* @param {number} i 插入位置
|
|
346
|
-
* @complexity `n`
|
|
347
|
-
* @returns {T extends Token ? Token : AstText}
|
|
348
|
-
* @throws `RangeError` 不可插入的子节点
|
|
349
|
-
*/
|
|
350
|
-
insertAt(token, i = this.childNodes.length) {
|
|
351
|
-
if (typeof token === 'string') {
|
|
352
|
-
token = new AstText(token);
|
|
353
|
-
}
|
|
354
|
-
if (!Parser.running && this.#acceptable) {
|
|
355
|
-
const acceptableIndices = Object.fromEntries(
|
|
356
|
-
Object.entries(this.#acceptable)
|
|
357
|
-
.map(([str, ranges]) => [str, ranges.applyTo(this.childNodes.length + 1)]),
|
|
358
|
-
),
|
|
359
|
-
nodesAfter = this.childNodes.slice(i),
|
|
360
|
-
{constructor: {name: insertedName}} = token,
|
|
361
|
-
k = i < 0 ? i + this.childNodes.length : i;
|
|
362
|
-
if (!acceptableIndices[insertedName].includes(k)) {
|
|
363
|
-
throw new RangeError(`${this.constructor.name} 的第 ${k} 个子节点不能为 ${insertedName}!`);
|
|
364
|
-
} else if (nodesAfter.some(({constructor: {name}}, j) => !acceptableIndices[name].includes(k + j + 1))) {
|
|
365
|
-
throw new Error(`${this.constructor.name} 插入新的第 ${k} 个子节点会破坏规定的顺序!`);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
super.insertAt(token, i);
|
|
369
|
-
if (token.type === 'root') {
|
|
370
|
-
token.type = 'plain';
|
|
371
|
-
}
|
|
372
|
-
return token;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* 替换为同类节点
|
|
377
|
-
* @param {Token} token 待替换的节点
|
|
378
|
-
* @complexity `n`
|
|
379
|
-
* @throws `Error` 不存在父节点
|
|
380
|
-
* @throws `Error` 待替换的节点具有不同属性
|
|
381
|
-
*/
|
|
382
|
-
safeReplaceWith(token) {
|
|
383
|
-
const {parentNode} = this;
|
|
384
|
-
if (!parentNode) {
|
|
385
|
-
throw new Error('不存在父节点!');
|
|
386
|
-
} else if (token.constructor !== this.constructor) {
|
|
387
|
-
this.typeError('safeReplaceWith', this.constructor.name);
|
|
388
|
-
}
|
|
389
|
-
try {
|
|
390
|
-
assert.deepEqual(token.getAttribute('acceptable'), this.#acceptable);
|
|
391
|
-
} catch (e) {
|
|
392
|
-
if (e instanceof assert.AssertionError) {
|
|
393
|
-
throw new Error(`待替换的 ${this.constructor.name} 带有不同的 #acceptable 属性!`);
|
|
394
|
-
}
|
|
395
|
-
throw e;
|
|
396
|
-
}
|
|
397
|
-
const i = parentNode.childNodes.indexOf(this);
|
|
398
|
-
super.removeAt.call(parentNode, i);
|
|
399
|
-
super.insertAt.call(parentNode, token, i);
|
|
400
|
-
if (token.type === 'root') {
|
|
401
|
-
token.type = 'plain';
|
|
402
|
-
}
|
|
403
|
-
const e = new Event('replace', {bubbles: true});
|
|
404
|
-
token.dispatchEvent(e, {position: i, oldToken: this, newToken: token});
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* 创建HTML注释
|
|
409
|
-
* @param {string} data 注释内容
|
|
410
|
-
*/
|
|
411
|
-
createComment(data = '') {
|
|
412
|
-
if (typeof data === 'string') {
|
|
413
|
-
const CommentToken = require('./nowiki/comment');
|
|
414
|
-
const config = this.getAttribute('config');
|
|
415
|
-
return Parser.run(() => new CommentToken(data.replaceAll('-->', '-->'), true, config));
|
|
416
|
-
}
|
|
417
|
-
return this.typeError('createComment', 'String');
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
/**
|
|
421
|
-
* 创建标签
|
|
422
|
-
* @param {string} tagName 标签名
|
|
423
|
-
* @param {{selfClosing: boolean, closing: boolean}} options 选项
|
|
424
|
-
* @throws `RangeError` 非法的标签名
|
|
425
|
-
*/
|
|
426
|
-
createElement(tagName, {selfClosing, closing} = {}) {
|
|
427
|
-
if (typeof tagName !== 'string') {
|
|
428
|
-
this.typeError('createElement', 'String');
|
|
429
|
-
}
|
|
430
|
-
const config = this.getAttribute('config'),
|
|
431
|
-
include = this.getAttribute('include');
|
|
432
|
-
if (tagName === (include ? 'noinclude' : 'includeonly')) {
|
|
433
|
-
const IncludeToken = require('./tagPair/include');
|
|
434
|
-
return Parser.run(
|
|
435
|
-
() => new IncludeToken(tagName, '', undefined, selfClosing ? undefined : tagName, config),
|
|
436
|
-
);
|
|
437
|
-
} else if (config.ext.includes(tagName)) {
|
|
438
|
-
const ExtToken = require('./tagPair/ext');
|
|
439
|
-
return Parser.run(() => new ExtToken(tagName, '', '', selfClosing ? undefined : '', config));
|
|
440
|
-
} else if (config.html.flat().includes(tagName)) {
|
|
441
|
-
const HtmlToken = require('./html');
|
|
442
|
-
return Parser.run(() => new HtmlToken(tagName, '', closing, selfClosing, config));
|
|
443
|
-
}
|
|
444
|
-
throw new RangeError(`非法的标签名!${tagName}`);
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
/**
|
|
448
|
-
* 判断标题是否是跨维基链接
|
|
449
|
-
* @param {string} title 标题
|
|
450
|
-
*/
|
|
451
|
-
isInterwiki(title) {
|
|
452
|
-
return Parser.isInterwiki(title, this.#config);
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* 规范化页面标题
|
|
457
|
-
* @param {string} title 标题(含或不含命名空间前缀)
|
|
458
|
-
* @param {number} defaultNs 命名空间
|
|
459
|
-
*/
|
|
460
|
-
normalizeTitle(title, defaultNs = 0, halfParsed = false) {
|
|
461
|
-
return Parser.normalizeTitle(title, defaultNs, this.#include, this.#config, halfParsed);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
/**
|
|
465
|
-
* 获取全部章节
|
|
466
|
-
* @complexity `n`
|
|
467
|
-
*/
|
|
468
|
-
sections() {
|
|
469
|
-
if (this.type !== 'root') {
|
|
470
|
-
return undefined;
|
|
471
|
-
}
|
|
472
|
-
const {childNodes} = this,
|
|
473
|
-
headings = [...childNodes.entries()].filter(([, {type}]) => type === 'heading')
|
|
474
|
-
.map(([i, {name}]) => [i, Number(name)]),
|
|
475
|
-
lastHeading = [-1, -1, -1, -1, -1, -1],
|
|
476
|
-
/** @type {(AstText|Token)[][]} */ sections = new Array(headings.length);
|
|
477
|
-
for (let i = 0; i < headings.length; i++) {
|
|
478
|
-
const [index, level] = headings[i];
|
|
479
|
-
for (let j = level; j < 6; j++) {
|
|
480
|
-
const last = lastHeading[j];
|
|
481
|
-
if (last >= 0) {
|
|
482
|
-
sections[last] = childNodes.slice(headings[last][0], index);
|
|
483
|
-
}
|
|
484
|
-
lastHeading[j] = j === level ? i : -1;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
for (const last of lastHeading) {
|
|
488
|
-
if (last >= 0) {
|
|
489
|
-
sections[last] = childNodes.slice(headings[last][0]);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
sections.unshift(childNodes.slice(0, headings[0]?.[0]));
|
|
493
|
-
return sections;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
/**
|
|
497
|
-
* 获取指定章节
|
|
498
|
-
* @param {number} n 章节序号
|
|
499
|
-
* @complexity `n`
|
|
500
|
-
*/
|
|
501
|
-
section(n) {
|
|
502
|
-
return typeof n === 'number' ? this.sections()?.[n] : this.typeError('section', 'Number');
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
/**
|
|
506
|
-
* 获取指定的外层HTML标签
|
|
507
|
-
* @param {string|undefined} tag HTML标签名
|
|
508
|
-
* @returns {[Token, Token]}
|
|
509
|
-
* @complexity `n`
|
|
510
|
-
* @throws `RangeError` 非法的标签或空标签
|
|
511
|
-
*/
|
|
512
|
-
findEnclosingHtml(tag) {
|
|
513
|
-
if (tag !== undefined && typeof tag !== 'string') {
|
|
514
|
-
this.typeError('findEnclosingHtml', 'String');
|
|
515
|
-
}
|
|
516
|
-
tag = tag?.toLowerCase();
|
|
517
|
-
if (tag !== undefined && !this.#config.html.slice(0, 2).flat().includes(tag)) {
|
|
518
|
-
throw new RangeError(`非法的标签或空标签:${tag}`);
|
|
519
|
-
}
|
|
520
|
-
const {parentNode} = this;
|
|
521
|
-
if (!parentNode) {
|
|
522
|
-
return undefined;
|
|
523
|
-
}
|
|
524
|
-
const {children} = parentNode,
|
|
525
|
-
index = children.indexOf(this);
|
|
526
|
-
let i;
|
|
527
|
-
for (i = index - 1; i >= 0; i--) {
|
|
528
|
-
if (children[i].matches(`html${tag && '#'}${tag ?? ''}[selfClosing=false][closing=false]`)) {
|
|
529
|
-
break;
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
if (i === -1) {
|
|
533
|
-
return parentNode.findEnclosingHtml(tag);
|
|
534
|
-
}
|
|
535
|
-
const opening = children[i],
|
|
536
|
-
{name} = opening;
|
|
537
|
-
for (i = index + 1; i < children.length; i++) {
|
|
538
|
-
if (children[i].matches(`html#${name}[selfClosing=false][closing=true]`)) {
|
|
539
|
-
break;
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
return i === children.length
|
|
543
|
-
? parentNode.findEnclosingHtml(tag)
|
|
544
|
-
: [opening, children[i]];
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
/**
|
|
548
|
-
* 获取全部分类
|
|
549
|
-
* @complexity `n`
|
|
550
|
-
*/
|
|
551
|
-
getCategories() {
|
|
552
|
-
return this.querySelectorAll('category').map(({name, sortkey}) => [name, sortkey]);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
/**
|
|
556
|
-
* 重新解析单引号
|
|
557
|
-
* @throws `Error` 不接受QuoteToken作为子节点
|
|
558
|
-
*/
|
|
559
|
-
redoQuotes() {
|
|
560
|
-
const acceptable = this.getAttribute('acceptable');
|
|
561
|
-
if (acceptable && !acceptable.QuoteToken?.some(
|
|
562
|
-
range => typeof range !== 'number' && range.start === 0 && range.end === Infinity && range.step === 1,
|
|
563
|
-
)) {
|
|
564
|
-
throw new Error(`${this.constructor.name} 不接受 QuoteToken 作为子节点!`);
|
|
565
|
-
}
|
|
566
|
-
for (const quote of this.childNodes) {
|
|
567
|
-
if (quote.type === 'quote') {
|
|
568
|
-
quote.replaceWith(String(quote));
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
this.normalize();
|
|
572
|
-
/** @type {[number, AstText][]} */
|
|
573
|
-
const textNodes = [...this.childNodes.entries()].filter(([, {type}]) => type === 'text'),
|
|
574
|
-
indices = textNodes.map(([i]) => this.getRelativeIndex(i)),
|
|
575
|
-
token = Parser.run(() => {
|
|
576
|
-
const root = new Token(text(textNodes.map(([, str]) => str)), this.getAttribute('config'));
|
|
577
|
-
return root.setAttribute('stage', 6).parse(7);
|
|
578
|
-
});
|
|
579
|
-
for (const quote of token.children.reverse()) {
|
|
580
|
-
if (quote.type === 'quote') {
|
|
581
|
-
const index = quote.getRelativeIndex(),
|
|
582
|
-
n = indices.findLastIndex(textIndex => textIndex <= index);
|
|
583
|
-
this.childNodes[n].splitText(index - indices[n]);
|
|
584
|
-
this.childNodes[n + 1].splitText(Number(quote.name));
|
|
585
|
-
this.removeAt(n + 1);
|
|
586
|
-
this.insertAt(quote, n + 1);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
this.normalize();
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
/**
|
|
593
|
-
* 将占位符替换为子Token
|
|
594
|
-
* @complexity `n`
|
|
595
|
-
*/
|
|
596
|
-
build() {
|
|
597
|
-
if (!Parser.debugging && externalUse('build')) {
|
|
598
|
-
this.debugOnly('build');
|
|
599
|
-
}
|
|
600
|
-
this.#stage = MAX_STAGE;
|
|
601
|
-
const {childNodes: {length}, firstChild} = this,
|
|
602
|
-
str = String(firstChild);
|
|
603
|
-
if (length === 1 && firstChild.type === 'text' && str.includes('\0')) {
|
|
604
|
-
this.replaceChildren(...this.#buildFromStr(str));
|
|
605
|
-
this.normalize();
|
|
606
|
-
if (this.type === 'root') {
|
|
607
|
-
for (const token of this.#accum) {
|
|
608
|
-
token.build();
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
return this;
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
/** 生成部分Token的`name`属性 */
|
|
616
|
-
afterBuild() {
|
|
617
|
-
if (!Parser.debugging && externalUse('afterBuild')) {
|
|
618
|
-
this.debugOnly('afterBuild');
|
|
619
|
-
} else if (this.type === 'root') {
|
|
620
|
-
for (const token of this.#accum) {
|
|
621
|
-
token.afterBuild();
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
return this;
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
/**
|
|
628
|
-
* 解析、重构、生成部分Token的`name`属性
|
|
629
|
-
* @param {number} n 最大解析层级
|
|
630
|
-
* @param {boolean} include 是否嵌入
|
|
631
|
-
*/
|
|
632
|
-
parse(n = MAX_STAGE, include = false) {
|
|
633
|
-
if (typeof n !== 'number') {
|
|
634
|
-
this.typeError('parse', 'Number');
|
|
635
|
-
} else if (n < MAX_STAGE && !Parser.debugging && Parser.warning && externalUse('parse')) {
|
|
636
|
-
Parser.warn('指定解析层级的方法仅供熟练用户使用!');
|
|
637
|
-
}
|
|
638
|
-
this.#include = Boolean(include);
|
|
639
|
-
while (this.#stage < n) {
|
|
640
|
-
this.#parseOnce(this.#stage, include);
|
|
641
|
-
}
|
|
642
|
-
return n ? this.build().afterBuild() : this;
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
* 解析HTML注释和扩展标签
|
|
647
|
-
* @param {boolean} includeOnly 是否嵌入
|
|
648
|
-
*/
|
|
649
|
-
#parseCommentAndExt(includeOnly) {
|
|
650
|
-
const parseCommentAndExt = require('../parser/commentAndExt');
|
|
651
|
-
this.setText(parseCommentAndExt(String(this), this.#config, this.#accum, includeOnly));
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
/** 解析花括号 */
|
|
655
|
-
#parseBrackets() {
|
|
656
|
-
const parseBrackets = require('../parser/brackets');
|
|
657
|
-
this.setText(parseBrackets(String(this), this.#config, this.#accum));
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
/** 解析HTML标签 */
|
|
661
|
-
#parseHtml() {
|
|
662
|
-
const parseHtml = require('../parser/html');
|
|
663
|
-
this.setText(parseHtml(String(this), this.#config, this.#accum));
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
/** 解析表格 */
|
|
667
|
-
#parseTable() {
|
|
668
|
-
const parseTable = require('../parser/table'),
|
|
669
|
-
TableToken = require('./table');
|
|
670
|
-
this.setText(parseTable(this, this.#config, this.#accum));
|
|
671
|
-
for (const table of this.#accum) {
|
|
672
|
-
if (table instanceof TableToken && table.type !== 'td') {
|
|
673
|
-
table.normalize();
|
|
674
|
-
const {childNodes: [, child]} = table;
|
|
675
|
-
if (typeof child === 'string' && child.includes('\0')) {
|
|
676
|
-
table.removeAt(1);
|
|
677
|
-
const inner = new Token(child, this.#config, true, this.#accum);
|
|
678
|
-
table.insertAt(inner, 1);
|
|
679
|
-
inner.setAttribute('stage', 4);
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
/** 解析\<hr\>和状态开关 */
|
|
686
|
-
#parseHrAndDoubleUndescore() {
|
|
687
|
-
const parseHrAndDoubleUnderscore = require('../parser/hrAndDoubleUnderscore');
|
|
688
|
-
this.setText(parseHrAndDoubleUnderscore(this, this.#config, this.#accum));
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
/** 解析内部链接 */
|
|
692
|
-
#parseLinks() {
|
|
693
|
-
const parseLinks = require('../parser/links');
|
|
694
|
-
this.setText(parseLinks(String(this), this.#config, this.#accum));
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
/** 解析单引号 */
|
|
698
|
-
#parseQuotes() {
|
|
699
|
-
const parseQuotes = require('../parser/quotes');
|
|
700
|
-
const lines = String(this).split('\n');
|
|
701
|
-
for (let i = 0; i < lines.length; i++) {
|
|
702
|
-
lines[i] = parseQuotes(lines[i], this.#config, this.#accum);
|
|
703
|
-
}
|
|
704
|
-
this.setText(lines.join('\n'));
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
/** 解析外部链接 */
|
|
708
|
-
#parseExternalLinks() {
|
|
709
|
-
const parseExternalLinks = require('../parser/externalLinks');
|
|
710
|
-
this.setText(parseExternalLinks(String(this), this.#config, this.#accum));
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
/** 解析自由外链 */
|
|
714
|
-
#parseMagicLinks() {
|
|
715
|
-
const parseMagicLinks = require('../parser/magicLinks');
|
|
716
|
-
this.setText(parseMagicLinks(String(this), this.#config, this.#accum));
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
/** 解析列表 */
|
|
720
|
-
#parseList() {
|
|
721
|
-
const parseList = require('../parser/list');
|
|
722
|
-
const lines = String(this).split('\n');
|
|
723
|
-
for (let i = this.type === 'root' ? 0 : 1; i < lines.length; i++) {
|
|
724
|
-
lines[i] = parseList(lines[i], this.#config, this.#accum);
|
|
725
|
-
}
|
|
726
|
-
this.setText(lines.join('\n'));
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
/** 解析语言变体转换 */
|
|
730
|
-
#parseConverter() {
|
|
731
|
-
const parseConverter = require('../parser/converter');
|
|
732
|
-
this.setText(parseConverter(String(this), this.#config, this.#accum));
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
Parser.classes.Token = __filename;
|
|
737
|
-
module.exports = Token;
|