wikiparser-node 0.10.0 → 0.11.0-m
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/README.md +44 -32
- package/config/.schema.json +1 -8
- package/config/default.json +1 -2
- package/config/llwiki.json +1 -36
- package/config/moegirl.json +1 -45
- package/config/zhwiki.json +1 -467
- package/dist/index.d.ts +110 -0
- package/dist/lib/element.d.ts +48 -0
- package/dist/lib/node.d.ts +108 -0
- package/dist/lib/text.d.ts +30 -0
- package/dist/lib/title.d.ts +15 -0
- package/dist/mixin/hidden.d.ts +8 -0
- package/dist/parser/brackets.d.ts +12 -0
- package/dist/parser/commentAndExt.d.ts +8 -0
- package/dist/parser/converter.d.ts +7 -0
- package/dist/parser/externalLinks.d.ts +7 -0
- package/dist/parser/hrAndDoubleUnderscore.d.ts +11 -0
- package/dist/parser/html.d.ts +7 -0
- package/dist/parser/links.d.ts +7 -0
- package/dist/parser/list.d.ts +7 -0
- package/dist/parser/magicLinks.d.ts +7 -0
- package/dist/parser/quotes.d.ts +7 -0
- package/dist/parser/table.d.ts +11 -0
- package/dist/src/arg.d.ts +25 -0
- package/dist/src/atom/hidden.d.ts +5 -0
- package/dist/src/atom/index.d.ts +14 -0
- package/dist/src/attribute.d.ts +42 -0
- package/dist/src/attributes.d.ts +33 -0
- package/dist/src/converter.d.ts +29 -0
- package/dist/src/converterFlags.d.ts +27 -0
- package/dist/src/converterRule.d.ts +29 -0
- package/dist/src/extLink.d.ts +22 -0
- package/dist/src/gallery.d.ts +19 -0
- package/dist/src/hasNowiki/index.d.ts +14 -0
- package/dist/src/hasNowiki/pre.d.ts +13 -0
- package/dist/src/heading.d.ts +24 -0
- package/dist/src/html.d.ts +33 -0
- package/dist/src/imageParameter.d.ts +23 -0
- package/dist/src/imagemap.d.ts +26 -0
- package/dist/src/imagemapLink.d.ts +16 -0
- package/dist/src/index.d.ts +53 -0
- package/dist/src/link/category.d.ts +8 -0
- package/dist/src/link/file.d.ts +27 -0
- package/dist/src/link/galleryImage.d.ts +15 -0
- package/dist/src/link/index.d.ts +29 -0
- package/dist/src/magicLink.d.ts +14 -0
- package/dist/src/nested/choose.d.ts +13 -0
- package/dist/src/nested/combobox.d.ts +13 -0
- package/dist/src/nested/index.d.ts +20 -0
- package/dist/src/nested/references.d.ts +13 -0
- package/dist/src/nowiki/comment.d.ts +19 -0
- package/dist/src/nowiki/dd.d.ts +8 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +12 -0
- package/dist/src/nowiki/hr.d.ts +13 -0
- package/dist/src/nowiki/index.d.ts +18 -0
- package/dist/src/nowiki/list.d.ts +8 -0
- package/dist/src/nowiki/noinclude.d.ts +8 -0
- package/dist/src/nowiki/quote.d.ts +20 -0
- package/dist/src/onlyinclude.d.ts +17 -0
- package/dist/src/paramTag/index.d.ts +24 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -0
- package/dist/src/parameter.d.ts +28 -0
- package/dist/src/syntax.d.ts +15 -0
- package/dist/src/table/index.d.ts +16 -0
- package/dist/src/table/td.d.ts +39 -0
- package/dist/src/table/tr.d.ts +17 -0
- package/dist/src/tagPair/ext.d.ts +17 -0
- package/dist/src/tagPair/include.d.ts +16 -0
- package/dist/src/tagPair/index.d.ts +28 -0
- package/dist/src/transclude.d.ts +75 -0
- package/dist/util/lint.d.ts +28 -0
- package/dist/util/string.d.ts +31 -0
- package/i18n/zh-hans.json +1 -1
- package/i18n/zh-hant.json +1 -1
- package/index.js +5 -257
- package/lib/element.js +7 -482
- package/lib/node.js +11 -540
- package/lib/text.js +3 -96
- package/lib/title.js +1 -28
- package/mixin/hidden.js +0 -3
- package/package.json +11 -5
- package/parser/brackets.js +3 -2
- package/parser/commentAndExt.js +8 -6
- package/parser/converter.js +1 -2
- package/parser/externalLinks.js +1 -2
- package/parser/hrAndDoubleUnderscore.js +1 -2
- package/parser/html.js +1 -2
- package/parser/links.js +5 -6
- package/parser/list.js +1 -2
- package/parser/magicLinks.js +1 -2
- package/parser/quotes.js +1 -2
- package/parser/table.js +1 -2
- package/src/arg.js +4 -118
- package/src/atom/hidden.js +0 -2
- package/src/atom/index.js +1 -18
- package/src/attribute.js +6 -190
- package/src/attributes.js +5 -308
- package/src/converter.js +3 -109
- package/src/converterFlags.js +1 -188
- package/src/converterRule.js +2 -184
- package/src/extLink.js +2 -122
- package/src/gallery.js +2 -56
- package/src/hasNowiki/index.js +1 -13
- package/src/hasNowiki/pre.js +1 -13
- package/src/heading.js +4 -55
- package/src/html.js +5 -120
- package/src/imageParameter.js +3 -165
- package/src/imagemap.js +3 -62
- package/src/imagemapLink.js +2 -14
- package/src/index.js +9 -530
- package/src/link/category.js +1 -32
- package/src/link/file.js +3 -158
- package/src/link/galleryImage.js +3 -61
- package/src/link/index.js +4 -273
- package/src/magicLink.js +6 -87
- package/src/nested/choose.js +1 -2
- package/src/nested/combobox.js +1 -2
- package/src/nested/index.js +8 -32
- package/src/nested/references.js +1 -2
- package/src/nowiki/comment.js +2 -26
- package/src/nowiki/dd.js +1 -47
- package/src/nowiki/doubleUnderscore.js +1 -31
- package/src/nowiki/hr.js +2 -21
- package/src/nowiki/index.js +2 -24
- package/src/nowiki/list.js +2 -5
- package/src/nowiki/noinclude.js +0 -14
- package/src/nowiki/quote.js +2 -16
- package/src/onlyinclude.js +2 -27
- package/src/paramTag/index.js +2 -25
- package/src/paramTag/inputbox.js +2 -5
- package/src/parameter.js +6 -148
- package/src/syntax.js +1 -69
- package/src/table/index.js +2 -939
- package/src/table/td.js +6 -226
- package/src/table/tr.js +3 -248
- package/src/tagPair/ext.js +4 -23
- package/src/tagPair/include.js +1 -25
- package/src/tagPair/index.js +3 -52
- package/src/transclude.js +6 -513
- package/typings/api.d.ts +9 -0
- package/typings/index.d.ts +51 -0
- package/typings/node.d.ts +16 -0
- package/typings/parser.d.ts +5 -0
- package/typings/token.d.ts +28 -0
- package/util/lint.js +2 -0
- package/util/string.js +0 -51
- package/lib/ranges.js +0 -130
- package/mixin/attributeParent.js +0 -117
- package/mixin/fixedToken.js +0 -40
- package/mixin/singleLine.js +0 -31
- package/mixin/sol.js +0 -54
- package/parser/selector.js +0 -177
- package/src/charinsert.js +0 -97
- package/tool/index.js +0 -1202
- package/util/debug.js +0 -73
package/lib/text.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/** @typedef {import('../typings/token').LintError} LintError */
|
|
4
|
+
|
|
3
5
|
const Parser = require('..'),
|
|
4
6
|
AstNode = require('./node'),
|
|
5
7
|
AstElement = require('./element');
|
|
@@ -64,19 +66,10 @@ class AstText extends AstNode {
|
|
|
64
66
|
type = 'text';
|
|
65
67
|
/** @type {string} */ data;
|
|
66
68
|
|
|
67
|
-
/** 文本长度 */
|
|
68
|
-
get length() {
|
|
69
|
-
return this.data.length;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
69
|
/** @param {string} text 包含文本 */
|
|
73
70
|
constructor(text = '') {
|
|
74
71
|
super();
|
|
75
|
-
|
|
76
|
-
data: {value: text, writable: false},
|
|
77
|
-
childNodes: {enumerable: false, configurable: false},
|
|
78
|
-
type: {enumerable: false, writable: false, configurable: false},
|
|
79
|
-
});
|
|
72
|
+
this.data = text;
|
|
80
73
|
}
|
|
81
74
|
|
|
82
75
|
/** 输出字符串 */
|
|
@@ -162,12 +155,7 @@ class AstText extends AstNode {
|
|
|
162
155
|
*/
|
|
163
156
|
#setData(text) {
|
|
164
157
|
text = String(text);
|
|
165
|
-
const {data} = this,
|
|
166
|
-
e = new Event('text', {bubbles: true});
|
|
167
158
|
this.setAttribute('data', text);
|
|
168
|
-
if (data !== text) {
|
|
169
|
-
this.dispatchEvent(e, {oldText: data, newText: text});
|
|
170
|
-
}
|
|
171
159
|
}
|
|
172
160
|
|
|
173
161
|
/**
|
|
@@ -177,87 +165,6 @@ class AstText extends AstNode {
|
|
|
177
165
|
replaceData(text = '') {
|
|
178
166
|
this.#setData(text);
|
|
179
167
|
}
|
|
180
|
-
|
|
181
|
-
/** 复制 */
|
|
182
|
-
cloneNode() {
|
|
183
|
-
return new AstText(this.data);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* @override
|
|
188
|
-
* @template {string} T
|
|
189
|
-
* @param {T} key 属性键
|
|
190
|
-
* @returns {TokenAttribute<T>}
|
|
191
|
-
* @throws `Error` 文本节点没有子节点
|
|
192
|
-
*/
|
|
193
|
-
getAttribute(key) {
|
|
194
|
-
return key === 'verifyChild'
|
|
195
|
-
? () => {
|
|
196
|
-
throw new Error('文本节点没有子节点!');
|
|
197
|
-
}
|
|
198
|
-
: super.getAttribute(key);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* 在后方添加字符串
|
|
203
|
-
* @param {string} text 添加的字符串
|
|
204
|
-
*/
|
|
205
|
-
appendData(text) {
|
|
206
|
-
this.#setData(this.data + text);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* 删减字符串
|
|
211
|
-
* @param {number} offset 起始位置
|
|
212
|
-
* @param {number} count 删减字符数
|
|
213
|
-
*/
|
|
214
|
-
deleteData(offset, count) {
|
|
215
|
-
this.#setData(this.data.slice(0, offset) + this.data.slice(offset + count));
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* 插入字符串
|
|
220
|
-
* @param {number} offset 插入位置
|
|
221
|
-
* @param {string} text 待插入的字符串
|
|
222
|
-
*/
|
|
223
|
-
insertData(offset, text) {
|
|
224
|
-
this.#setData(this.data.slice(0, offset) + text + this.data.slice(offset));
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* 提取子串
|
|
229
|
-
* @param {number} offset 起始位置
|
|
230
|
-
* @param {number} count 字符数
|
|
231
|
-
*/
|
|
232
|
-
substringData(offset, count) {
|
|
233
|
-
return this.data.slice(offset, offset + count);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* 将文本子节点分裂为两部分
|
|
238
|
-
* @param {number} offset 分裂位置
|
|
239
|
-
* @throws `RangeError` 错误的断开位置
|
|
240
|
-
* @throws `Error` 没有父节点
|
|
241
|
-
*/
|
|
242
|
-
splitText(offset) {
|
|
243
|
-
if (!Number.isInteger(offset)) {
|
|
244
|
-
this.typeError('splitText', 'Number');
|
|
245
|
-
} else if (offset > this.length || offset < -this.length) {
|
|
246
|
-
throw new RangeError(`错误的断开位置!${offset}`);
|
|
247
|
-
}
|
|
248
|
-
const {parentNode, data} = this;
|
|
249
|
-
if (!parentNode) {
|
|
250
|
-
throw new Error('待分裂的文本节点没有父节点!');
|
|
251
|
-
}
|
|
252
|
-
const newText = new AstText(data.slice(offset)),
|
|
253
|
-
childNodes = [...parentNode.childNodes];
|
|
254
|
-
this.setAttribute('data', data.slice(0, offset));
|
|
255
|
-
childNodes.splice(childNodes.indexOf(this) + 1, 0, newText);
|
|
256
|
-
newText.setAttribute('parentNode', parentNode);
|
|
257
|
-
parentNode.setAttribute('childNodes', childNodes);
|
|
258
|
-
return newText;
|
|
259
|
-
}
|
|
260
168
|
}
|
|
261
169
|
|
|
262
|
-
Parser.classes.AstText = __filename;
|
|
263
170
|
module.exports = AstText;
|
package/lib/title.js
CHANGED
|
@@ -9,10 +9,6 @@ class Title {
|
|
|
9
9
|
ns = 0;
|
|
10
10
|
fragment;
|
|
11
11
|
encoded = false;
|
|
12
|
-
title = '';
|
|
13
|
-
main = '';
|
|
14
|
-
prefix = '';
|
|
15
|
-
interwiki = '';
|
|
16
12
|
|
|
17
13
|
/**
|
|
18
14
|
* @param {string} title 标题(含或不含命名空间前缀)
|
|
@@ -36,11 +32,6 @@ class Title {
|
|
|
36
32
|
namespace = '';
|
|
37
33
|
title = title.slice(1).trim();
|
|
38
34
|
}
|
|
39
|
-
const iw = defaultNs ? undefined : Parser.isInterwiki(title, config);
|
|
40
|
-
if (iw) {
|
|
41
|
-
this.interwiki = iw[1].toLowerCase();
|
|
42
|
-
title = title.slice(iw[0].length);
|
|
43
|
-
}
|
|
44
35
|
const m = title.split(':');
|
|
45
36
|
if (m.length > 1) {
|
|
46
37
|
const id = namespaces[nsid[m[0].trim().toLowerCase()]];
|
|
@@ -54,30 +45,12 @@ class Title {
|
|
|
54
45
|
let fragment;
|
|
55
46
|
if (i !== -1) {
|
|
56
47
|
fragment = title.slice(i + 1).trimEnd();
|
|
57
|
-
if (fragment.includes('%')) {
|
|
58
|
-
try {
|
|
59
|
-
fragment = decodeURIComponent(fragment);
|
|
60
|
-
} catch {}
|
|
61
|
-
} else if (fragment.includes('.')) {
|
|
62
|
-
try {
|
|
63
|
-
fragment = decodeURIComponent(fragment.replaceAll('.', '%'));
|
|
64
|
-
} catch {}
|
|
65
|
-
}
|
|
66
48
|
title = title.slice(0, i).trim();
|
|
67
49
|
}
|
|
68
|
-
this.valid = Boolean(title || selfLink && fragment !== undefined
|
|
50
|
+
this.valid = Boolean(title || selfLink && fragment !== undefined)
|
|
69
51
|
&& !/\0\d+[eh!+-]\x7F|[<>[\]{}|]|%[\da-f]{2}/iu.test(title);
|
|
70
52
|
this.fragment = fragment;
|
|
71
|
-
this.main = title && `${title[0].toUpperCase()}${title.slice(1)}`;
|
|
72
|
-
this.prefix = `${namespace}${namespace && ':'}`;
|
|
73
|
-
this.title = `${iw ? `${this.interwiki}:` : ''}${this.prefix}${this.main.replaceAll(' ', '_')}`;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** @override */
|
|
77
|
-
toString() {
|
|
78
|
-
return `${this.title}${this.fragment === undefined ? '' : `#${this.fragment}`}`;
|
|
79
53
|
}
|
|
80
54
|
}
|
|
81
55
|
|
|
82
|
-
Parser.classes.Title = __filename;
|
|
83
56
|
module.exports = Title;
|
package/mixin/hidden.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wikiparser-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0-m",
|
|
4
4
|
"description": "A Node.js parser for MediaWiki markup with AST",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mediawiki",
|
|
@@ -21,17 +21,20 @@
|
|
|
21
21
|
"/mixin/",
|
|
22
22
|
"/parser/",
|
|
23
23
|
"/src/",
|
|
24
|
-
"/
|
|
25
|
-
"/
|
|
24
|
+
"/util/",
|
|
25
|
+
"/typings/",
|
|
26
|
+
"/dist/"
|
|
26
27
|
],
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
27
29
|
"repository": {
|
|
28
30
|
"type": "git",
|
|
29
31
|
"url": "git+https://github.com/bhsd-harry/wikiparser-node.git"
|
|
30
32
|
},
|
|
31
33
|
"scripts": {
|
|
32
|
-
"
|
|
34
|
+
"build": "rm -rf dist/ && tsc && grep -rl '/typings' dist/ | xargs gsed -i 's|/typings|/../typings|g' && node ./bin/tsc.js",
|
|
35
|
+
"test": "eslint . && ajv -s config/.schema.json -d 'config/*.json' --strict=true --strict-required=false",
|
|
33
36
|
"real": "node test/real.js",
|
|
34
|
-
"single": "node
|
|
37
|
+
"single": "node test/single.js"
|
|
35
38
|
},
|
|
36
39
|
"devDependencies": {
|
|
37
40
|
"@types/node": "^17.0.23",
|
|
@@ -42,6 +45,9 @@
|
|
|
42
45
|
"eslint-plugin-jsdoc": "^1.0.0",
|
|
43
46
|
"eslint-plugin-json-es": "^1.5.7",
|
|
44
47
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
48
|
+
"typescript": "^4.6.3",
|
|
49
|
+
"@typescript-eslint/eslint-plugin": "^5.47.1",
|
|
50
|
+
"@typescript-eslint/parser": "^5.47.1",
|
|
45
51
|
"ajv-cli": "^5.0.0",
|
|
46
52
|
"request": "^2.88.2"
|
|
47
53
|
},
|
package/parser/brackets.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/** @typedef {import('../typings/parser').BracketExecArray} BracketExecArray */
|
|
4
|
+
|
|
3
5
|
const {removeComment} = require('../util/string'),
|
|
4
6
|
Parser = require('..'),
|
|
5
7
|
HeadingToken = require('../src/heading'),
|
|
@@ -9,7 +11,7 @@ const {removeComment} = require('../util/string'),
|
|
|
9
11
|
/**
|
|
10
12
|
* 解析花括号
|
|
11
13
|
* @param {string} text wikitext
|
|
12
|
-
* @param {accum} accum
|
|
14
|
+
* @param {import('../typings/token').accum} accum
|
|
13
15
|
* @throws TranscludeToken.constructor()
|
|
14
16
|
*/
|
|
15
17
|
const parseBrackets = (text, config = Parser.getConfig(), accum = []) => {
|
|
@@ -122,5 +124,4 @@ const parseBrackets = (text, config = Parser.getConfig(), accum = []) => {
|
|
|
122
124
|
return text;
|
|
123
125
|
};
|
|
124
126
|
|
|
125
|
-
Parser.parsers.parseBrackets = __filename;
|
|
126
127
|
module.exports = parseBrackets;
|
package/parser/commentAndExt.js
CHANGED
|
@@ -10,7 +10,7 @@ const Parser = require('..'),
|
|
|
10
10
|
/**
|
|
11
11
|
* 解析HTML注释和扩展标签
|
|
12
12
|
* @param {string} text wikitext
|
|
13
|
-
* @param {accum} accum
|
|
13
|
+
* @param {import('../typings/token').accum} accum
|
|
14
14
|
* @param {boolean} includeOnly 是否嵌入
|
|
15
15
|
*/
|
|
16
16
|
const parseCommentAndExt = (text, config = Parser.getConfig(), accum = [], includeOnly = false) => {
|
|
@@ -20,10 +20,13 @@ const parseCommentAndExt = (text, config = Parser.getConfig(), accum = [], inclu
|
|
|
20
20
|
const str = `\0${accum.length}e\x7F`;
|
|
21
21
|
new OnlyincludeToken(inner, config, accum);
|
|
22
22
|
return str;
|
|
23
|
-
}).replace(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
}).replace(
|
|
24
|
+
/(?<=^|\0\d+e\x7F)[^\0]+(?=$|\0\d+e\x7F)/gu,
|
|
25
|
+
substr => {
|
|
26
|
+
new NoincludeToken(substr, config, accum);
|
|
27
|
+
return `\0${accum.length - 1}c\x7F`;
|
|
28
|
+
},
|
|
29
|
+
);
|
|
27
30
|
}
|
|
28
31
|
const ext = config.ext.join('|'),
|
|
29
32
|
includeRegex = includeOnly ? 'includeonly' : '(?:no|only)include',
|
|
@@ -55,5 +58,4 @@ const parseCommentAndExt = (text, config = Parser.getConfig(), accum = [], inclu
|
|
|
55
58
|
);
|
|
56
59
|
};
|
|
57
60
|
|
|
58
|
-
Parser.parsers.parseCommentAndExt = __filename;
|
|
59
61
|
module.exports = parseCommentAndExt;
|
package/parser/converter.js
CHANGED
|
@@ -6,7 +6,7 @@ const Parser = require('..'),
|
|
|
6
6
|
/**
|
|
7
7
|
* 解析语言变体转换
|
|
8
8
|
* @param {string} wikitext wikitext
|
|
9
|
-
* @param {accum} accum
|
|
9
|
+
* @param {import('../typings/token').accum} accum
|
|
10
10
|
*/
|
|
11
11
|
const parseConverter = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
12
12
|
const regex1 = /-\{/gu,
|
|
@@ -42,5 +42,4 @@ const parseConverter = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
|
42
42
|
return wikitext;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
Parser.parsers.parseConverter = __filename;
|
|
46
45
|
module.exports = parseConverter;
|
package/parser/externalLinks.js
CHANGED
|
@@ -7,7 +7,7 @@ const {extUrlChar, extUrlCharFirst} = require('../util/string'),
|
|
|
7
7
|
/**
|
|
8
8
|
* 解析外部链接
|
|
9
9
|
* @param {string} wikitext wikitext
|
|
10
|
-
* @param {accum} accum
|
|
10
|
+
* @param {import('../typings/token').accum} accum
|
|
11
11
|
*/
|
|
12
12
|
const parseExternalLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
13
13
|
const regex = new RegExp(
|
|
@@ -29,5 +29,4 @@ const parseExternalLinks = (wikitext, config = Parser.getConfig(), accum = []) =
|
|
|
29
29
|
});
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
Parser.parsers.parseExternalLinks = __filename;
|
|
33
32
|
module.exports = parseExternalLinks;
|
|
@@ -10,7 +10,7 @@ const Parser = require('..'),
|
|
|
10
10
|
/**
|
|
11
11
|
* 解析\<hr\>和状态开关
|
|
12
12
|
* @param {Token & {firstChild: AstText}} root 根节点
|
|
13
|
-
* @param {accum} accum
|
|
13
|
+
* @param {import('../typings/token').accum} accum
|
|
14
14
|
*/
|
|
15
15
|
const parseHrAndDoubleUnderscore = ({firstChild: {data}, type, name}, config = Parser.getConfig(), accum = []) => {
|
|
16
16
|
const {doubleUnderscore} = config,
|
|
@@ -45,5 +45,4 @@ const parseHrAndDoubleUnderscore = ({firstChild: {data}, type, name}, config = P
|
|
|
45
45
|
return type === 'root' || type === 'ext-inner' && name === 'poem' ? data : data.slice(1);
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
Parser.parsers.parseHrAndDoubleUnderscore = __filename;
|
|
49
48
|
module.exports = parseHrAndDoubleUnderscore;
|
package/parser/html.js
CHANGED
|
@@ -7,7 +7,7 @@ const Parser = require('..'),
|
|
|
7
7
|
/**
|
|
8
8
|
* 解析HTML标签
|
|
9
9
|
* @param {string} wikitext wikitext
|
|
10
|
-
* @param {accum} accum
|
|
10
|
+
* @param {import('../typings/token').accum} accum
|
|
11
11
|
*/
|
|
12
12
|
const parseHtml = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
13
13
|
const regex = /^(\/?)([a-z][^\s/>]*)((?:\s|\/(?!>))[^>]*?)?(\/?>)([^<]*)$/iu,
|
|
@@ -38,5 +38,4 @@ const parseHtml = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
|
38
38
|
return text;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
-
Parser.parsers.parseHtml = __filename;
|
|
42
41
|
module.exports = parseHtml;
|
package/parser/links.js
CHANGED
|
@@ -8,7 +8,7 @@ const Parser = require('..'),
|
|
|
8
8
|
/**
|
|
9
9
|
* 解析内部链接
|
|
10
10
|
* @param {string} wikitext wikitext
|
|
11
|
-
* @param {accum} accum
|
|
11
|
+
* @param {import('../typings/token').accum} accum
|
|
12
12
|
*/
|
|
13
13
|
const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
14
14
|
const parseQuotes = require('./quotes.js');
|
|
@@ -44,12 +44,12 @@ const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
|
44
44
|
continue;
|
|
45
45
|
}
|
|
46
46
|
const title = Parser.normalizeTitle(link, 0, false, config, true, true, true),
|
|
47
|
-
{ns,
|
|
47
|
+
{ns, valid} = title;
|
|
48
48
|
if (!valid) {
|
|
49
49
|
s += `[[${x}`;
|
|
50
50
|
continue;
|
|
51
51
|
} else if (mightBeImg) {
|
|
52
|
-
if (
|
|
52
|
+
if (ns !== 6) {
|
|
53
53
|
s += `[[${x}`;
|
|
54
54
|
continue;
|
|
55
55
|
}
|
|
@@ -79,9 +79,9 @@ const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
|
79
79
|
s += `\0${accum.length}l\x7F${after}`;
|
|
80
80
|
let SomeLinkToken = LinkToken;
|
|
81
81
|
if (!force) {
|
|
82
|
-
if (
|
|
82
|
+
if (ns === 6) {
|
|
83
83
|
SomeLinkToken = FileToken;
|
|
84
|
-
} else if (
|
|
84
|
+
} else if (ns === 14) {
|
|
85
85
|
SomeLinkToken = CategoryToken;
|
|
86
86
|
}
|
|
87
87
|
}
|
|
@@ -90,5 +90,4 @@ const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
|
90
90
|
return s;
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
-
Parser.parsers.parseLinks = __filename;
|
|
94
93
|
module.exports = parseLinks;
|
package/parser/list.js
CHANGED
|
@@ -7,7 +7,7 @@ const Parser = require('..'),
|
|
|
7
7
|
/**
|
|
8
8
|
* 解析列表
|
|
9
9
|
* @param {string} text wikitext
|
|
10
|
-
* @param {accum} accum
|
|
10
|
+
* @param {import('../typings/token').accum} accum
|
|
11
11
|
*/
|
|
12
12
|
const parseList = (text, config = Parser.getConfig(), accum = []) => {
|
|
13
13
|
const mt = /^((?:\0\d+c\x7F)*)([;:*#]+)/u.exec(text);
|
|
@@ -55,5 +55,4 @@ const parseList = (text, config = Parser.getConfig(), accum = []) => {
|
|
|
55
55
|
return text;
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
Parser.parsers.parseList = __filename;
|
|
59
58
|
module.exports = parseList;
|
package/parser/magicLinks.js
CHANGED
|
@@ -7,7 +7,7 @@ const {extUrlChar, extUrlCharFirst} = require('../util/string'),
|
|
|
7
7
|
/**
|
|
8
8
|
* 解析自由外链
|
|
9
9
|
* @param {string} wikitext wikitext
|
|
10
|
-
* @param {accum} accum
|
|
10
|
+
* @param {import('../typings/token').accum} accum
|
|
11
11
|
*/
|
|
12
12
|
const parseMagicLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
13
13
|
const regex = new RegExp(`(?<![\\p{L}\\d_])(?:${config.protocol})(${extUrlCharFirst}${extUrlChar})`, 'giu');
|
|
@@ -37,5 +37,4 @@ const parseMagicLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
|
37
37
|
});
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
-
Parser.parsers.parseMagicLinks = __filename;
|
|
41
40
|
module.exports = parseMagicLinks;
|
package/parser/quotes.js
CHANGED
|
@@ -6,7 +6,7 @@ const Parser = require('..'),
|
|
|
6
6
|
/**
|
|
7
7
|
* 解析单引号
|
|
8
8
|
* @param {string} text wikitext
|
|
9
|
-
* @param {accum} accum
|
|
9
|
+
* @param {import('../typings/token').accum} accum
|
|
10
10
|
*/
|
|
11
11
|
const parseQuotes = (text, config = Parser.getConfig(), accum = []) => {
|
|
12
12
|
const arr = text.split(/('{2,})/u),
|
|
@@ -60,5 +60,4 @@ const parseQuotes = (text, config = Parser.getConfig(), accum = []) => {
|
|
|
60
60
|
return arr.join('');
|
|
61
61
|
};
|
|
62
62
|
|
|
63
|
-
Parser.parsers.parseQuotes = __filename;
|
|
64
63
|
module.exports = parseQuotes;
|
package/parser/table.js
CHANGED
|
@@ -11,7 +11,7 @@ const Parser = require('..'),
|
|
|
11
11
|
/**
|
|
12
12
|
* 解析表格,注意`tr`和`td`包含开头的换行
|
|
13
13
|
* @param {Token & {firstChild: AstText}} root 根节点
|
|
14
|
-
* @param {accum} accum
|
|
14
|
+
* @param {import('../typings/token').accum} accum
|
|
15
15
|
*/
|
|
16
16
|
const parseTable = ({firstChild: {data}, type, name}, config = Parser.getConfig(), accum = []) => {
|
|
17
17
|
const /** @type {TrToken[]} */ stack = [],
|
|
@@ -110,5 +110,4 @@ const parseTable = ({firstChild: {data}, type, name}, config = Parser.getConfig(
|
|
|
110
110
|
return out.slice(1);
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
Parser.parsers.parseTable = __filename;
|
|
114
113
|
module.exports = parseTable;
|
package/src/arg.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {text
|
|
3
|
+
const {text} = require('../util/string'),
|
|
4
4
|
{generateForSelf, generateForChild} = require('../util/lint'),
|
|
5
5
|
Parser = require('..'),
|
|
6
6
|
Token = require('.');
|
|
@@ -19,18 +19,16 @@ class ArgToken extends Token {
|
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* @param {string[]} parts 以'|'分隔的各部分
|
|
22
|
-
* @param {accum} accum
|
|
22
|
+
* @param {import('../typings/token').accum} accum
|
|
23
23
|
* @complexity `n`
|
|
24
24
|
*/
|
|
25
25
|
constructor(parts, config = Parser.getConfig(), accum = []) {
|
|
26
26
|
super(undefined, config, true, accum, {
|
|
27
|
-
AtomToken: 0, Token: 1, HiddenToken: '2:',
|
|
28
27
|
});
|
|
29
28
|
for (let i = 0; i < parts.length; i++) {
|
|
30
29
|
if (i === 0 || i > 1) {
|
|
31
30
|
const AtomToken = i === 0 ? require('./atom') : require('./atom/hidden');
|
|
32
31
|
const token = new AtomToken(parts[i], i === 0 ? 'arg-name' : undefined, config, accum, {
|
|
33
|
-
'Stage-2': ':', '!HeadingToken': '',
|
|
34
32
|
});
|
|
35
33
|
super.insertAt(token);
|
|
36
34
|
} else {
|
|
@@ -39,15 +37,13 @@ class ArgToken extends Token {
|
|
|
39
37
|
super.insertAt(token.setAttribute('stage', 2));
|
|
40
38
|
}
|
|
41
39
|
}
|
|
42
|
-
this.getAttribute('protectChildren')(0);
|
|
43
40
|
}
|
|
44
41
|
|
|
45
42
|
/**
|
|
46
43
|
* @override
|
|
47
|
-
* @param {string} selector
|
|
48
44
|
*/
|
|
49
45
|
toString(selector) {
|
|
50
|
-
return
|
|
46
|
+
return `{{{${super.toString(selector, '|')}}}}`;
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
/**
|
|
@@ -68,15 +64,10 @@ class ArgToken extends Token {
|
|
|
68
64
|
return 1;
|
|
69
65
|
}
|
|
70
66
|
|
|
71
|
-
/** @override */
|
|
72
|
-
print() {
|
|
73
|
-
return super.print({pre: '{{{', post: '}}}', sep: '|'});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
67
|
/**
|
|
77
68
|
* @override
|
|
78
69
|
* @param {number} start 起始位置
|
|
79
|
-
* @returns {LintError[]}
|
|
70
|
+
* @returns {import('../typings/token').LintError[]}
|
|
80
71
|
*/
|
|
81
72
|
lint(start = this.getAbsoluteIndex()) {
|
|
82
73
|
if (!this.getAttribute('include')) {
|
|
@@ -97,111 +88,6 @@ class ArgToken extends Token {
|
|
|
97
88
|
}
|
|
98
89
|
return errors;
|
|
99
90
|
}
|
|
100
|
-
|
|
101
|
-
/** @override */
|
|
102
|
-
cloneNode() {
|
|
103
|
-
const [name, ...cloned] = this.cloneChildNodes();
|
|
104
|
-
return Parser.run(() => {
|
|
105
|
-
const token = new ArgToken([''], this.getAttribute('config'));
|
|
106
|
-
token.firstChild.safeReplaceWith(name);
|
|
107
|
-
token.append(...cloned);
|
|
108
|
-
token.afterBuild();
|
|
109
|
-
return token;
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** @override */
|
|
114
|
-
afterBuild() {
|
|
115
|
-
this.setAttribute('name', this.firstChild.text().trim());
|
|
116
|
-
const /** @type {AstListener} */ argListener = ({prevTarget}) => {
|
|
117
|
-
if (prevTarget === this.firstChild) {
|
|
118
|
-
this.setAttribute('name', prevTarget.text().trim());
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* 移除无效部分
|
|
126
|
-
* @complexity `n`
|
|
127
|
-
*/
|
|
128
|
-
removeRedundant() {
|
|
129
|
-
Parser.run(() => {
|
|
130
|
-
for (let i = this.length - 1; i > 1; i--) {
|
|
131
|
-
super.removeAt(i);
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* 移除子节点,且在移除`arg-default`子节点时自动移除全部多余子节点
|
|
138
|
-
* @param {number} i 移除位置
|
|
139
|
-
* @returns {Token}
|
|
140
|
-
*/
|
|
141
|
-
removeAt(i) {
|
|
142
|
-
if (i === 1) {
|
|
143
|
-
this.removeRedundant();
|
|
144
|
-
}
|
|
145
|
-
return super.removeAt(i);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* @override
|
|
150
|
-
* @param {Token} token 待插入的子节点
|
|
151
|
-
* @param {number} i 插入位置
|
|
152
|
-
* @throws `RangeError` 不可插入多余子节点
|
|
153
|
-
*/
|
|
154
|
-
insertAt(token, i = this.length) {
|
|
155
|
-
const j = i < 0 ? i + this.length : i;
|
|
156
|
-
if (j > 1) {
|
|
157
|
-
throw new RangeError(`${this.constructor.name} 不可插入多余的子节点!`);
|
|
158
|
-
}
|
|
159
|
-
super.insertAt(token, i);
|
|
160
|
-
if (j === 1) {
|
|
161
|
-
token.type = 'arg-default';
|
|
162
|
-
}
|
|
163
|
-
return token;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* 设置参数名
|
|
168
|
-
* @param {string} name 新参数名
|
|
169
|
-
* @throws `SyntaxError` 非法的参数名
|
|
170
|
-
*/
|
|
171
|
-
setName(name) {
|
|
172
|
-
name = String(name);
|
|
173
|
-
const root = Parser.parse(`{{{${name}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
174
|
-
{length, firstChild: arg} = root;
|
|
175
|
-
if (length !== 1 || arg.type !== 'arg' || arg.length !== 1) {
|
|
176
|
-
throw new SyntaxError(`非法的参数名称:${noWrap(name)}`);
|
|
177
|
-
}
|
|
178
|
-
const {firstChild} = arg;
|
|
179
|
-
arg.destroy(true);
|
|
180
|
-
this.firstChild.safeReplaceWith(firstChild);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* 设置预设值
|
|
185
|
-
* @param {string} value 预设值
|
|
186
|
-
* @throws `SyntaxError` 非法的参数预设值
|
|
187
|
-
*/
|
|
188
|
-
setDefault(value) {
|
|
189
|
-
value = String(value);
|
|
190
|
-
const root = Parser.parse(`{{{|${value}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
191
|
-
{length, firstChild: arg} = root;
|
|
192
|
-
if (length !== 1 || arg.type !== 'arg' || arg.length !== 2) {
|
|
193
|
-
throw new SyntaxError(`非法的参数预设值:${noWrap(value)}`);
|
|
194
|
-
}
|
|
195
|
-
const {childNodes: [, oldDefault]} = this,
|
|
196
|
-
{lastChild} = arg;
|
|
197
|
-
arg.destroy(true);
|
|
198
|
-
if (oldDefault) {
|
|
199
|
-
oldDefault.safeReplaceWith(lastChild);
|
|
200
|
-
} else {
|
|
201
|
-
this.insertAt(lastChild);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
91
|
}
|
|
205
92
|
|
|
206
|
-
Parser.classes.ArgToken = __filename;
|
|
207
93
|
module.exports = ArgToken;
|
package/src/atom/hidden.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const hidden = require('../../mixin/hidden'),
|
|
4
|
-
Parser = require('../..'),
|
|
5
4
|
AtomToken = require('.');
|
|
6
5
|
|
|
7
6
|
/** 不可见的节点 */
|
|
@@ -9,5 +8,4 @@ class HiddenToken extends hidden(AtomToken) {
|
|
|
9
8
|
type = 'hidden';
|
|
10
9
|
}
|
|
11
10
|
|
|
12
|
-
Parser.classes.HiddenToken = __filename;
|
|
13
11
|
module.exports = HiddenToken;
|
package/src/atom/index.js
CHANGED
|
@@ -13,8 +13,7 @@ class AtomToken extends Token {
|
|
|
13
13
|
/**
|
|
14
14
|
* @param {string} wikitext wikitext
|
|
15
15
|
* @param {string|undefined} type Token.type
|
|
16
|
-
* @param {accum} accum
|
|
17
|
-
* @param {acceptable} acceptable 可接受的子节点设置
|
|
16
|
+
* @param {import('../../typings/token').accum} accum
|
|
18
17
|
*/
|
|
19
18
|
constructor(wikitext, type, config = Parser.getConfig(), accum = [], acceptable = undefined) {
|
|
20
19
|
super(wikitext, config, true, accum, acceptable);
|
|
@@ -22,22 +21,6 @@ class AtomToken extends Token {
|
|
|
22
21
|
this.type = type;
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @override
|
|
28
|
-
* @this {AtomToken & {constructor: typeof AtomToken}}
|
|
29
|
-
*/
|
|
30
|
-
cloneNode() {
|
|
31
|
-
const cloned = this.cloneChildNodes(),
|
|
32
|
-
config = this.getAttribute('config'),
|
|
33
|
-
acceptable = this.getAttribute('acceptable');
|
|
34
|
-
return Parser.run(() => {
|
|
35
|
-
const token = new this.constructor(undefined, this.type, config, [], acceptable);
|
|
36
|
-
token.append(...cloned);
|
|
37
|
-
return token;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
24
|
}
|
|
41
25
|
|
|
42
|
-
Parser.classes.AtomToken = __filename;
|
|
43
26
|
module.exports = AtomToken;
|