wikiparser-node 0.7.0-b → 0.7.0
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 +39 -0
- package/config/default.json +832 -0
- package/config/llwiki.json +630 -0
- package/config/moegirl.json +728 -0
- package/config/zhwiki.json +1269 -0
- package/index.js +321 -0
- package/lib/element.js +611 -0
- package/lib/node.js +772 -0
- package/lib/ranges.js +130 -0
- package/lib/text.js +215 -0
- package/lib/title.js +80 -0
- package/mixin/attributeParent.js +117 -0
- package/mixin/fixedToken.js +40 -0
- package/mixin/hidden.js +21 -0
- package/mixin/singleLine.js +31 -0
- package/mixin/sol.js +65 -0
- package/package.json +11 -9
- package/parser/brackets.js +120 -0
- package/parser/commentAndExt.js +62 -0
- package/parser/converter.js +46 -0
- package/parser/externalLinks.js +33 -0
- package/parser/hrAndDoubleUnderscore.js +38 -0
- package/parser/html.js +42 -0
- package/parser/links.js +94 -0
- package/parser/list.js +59 -0
- package/parser/magicLinks.js +41 -0
- package/parser/quotes.js +64 -0
- package/parser/selector.js +177 -0
- package/parser/table.js +114 -0
- package/src/arg.js +203 -0
- package/src/atom/hidden.js +13 -0
- package/src/atom/index.js +43 -0
- package/src/attribute.js +420 -0
- package/src/attributes.js +452 -0
- package/src/charinsert.js +97 -0
- package/src/converter.js +176 -0
- package/src/converterFlags.js +284 -0
- package/src/converterRule.js +258 -0
- package/src/extLink.js +179 -0
- package/src/gallery.js +151 -0
- package/src/hasNowiki/index.js +44 -0
- package/src/hasNowiki/pre.js +40 -0
- package/src/heading.js +134 -0
- package/src/html.js +248 -0
- package/src/imageParameter.js +277 -0
- package/src/imagemap.js +199 -0
- package/src/imagemapLink.js +41 -0
- package/src/index.js +913 -0
- package/src/link/category.js +49 -0
- package/src/link/file.js +282 -0
- package/src/link/galleryImage.js +120 -0
- package/src/link/index.js +383 -0
- package/src/magicLink.js +149 -0
- package/src/nested/choose.js +24 -0
- package/src/nested/combobox.js +23 -0
- package/src/nested/index.js +96 -0
- package/src/nested/references.js +23 -0
- package/src/nowiki/comment.js +71 -0
- package/src/nowiki/dd.js +59 -0
- package/src/nowiki/doubleUnderscore.js +56 -0
- package/src/nowiki/hr.js +41 -0
- package/src/nowiki/index.js +56 -0
- package/src/nowiki/list.js +16 -0
- package/src/nowiki/noinclude.js +28 -0
- package/src/nowiki/quote.js +69 -0
- package/src/onlyinclude.js +64 -0
- package/src/paramTag/index.js +89 -0
- package/src/paramTag/inputbox.js +44 -0
- package/src/parameter.js +239 -0
- package/src/syntax.js +91 -0
- package/src/table/index.js +984 -0
- package/src/table/td.js +339 -0
- package/src/table/tr.js +319 -0
- package/src/tagPair/ext.js +138 -0
- package/src/tagPair/include.js +60 -0
- package/src/tagPair/index.js +126 -0
- package/src/transclude.js +824 -0
- package/tool/index.js +1202 -0
- package/util/base.js +17 -0
- package/util/debug.js +73 -0
- package/util/diff.js +76 -0
- package/util/lint.js +54 -0
- package/util/string.js +107 -0
- package/bundle/bundle.min.js +0 -40
package/src/parameter.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {noWrap, extUrlChar, extUrlCharFirst} = require('../util/string'),
|
|
4
|
+
{generateForChild} = require('../util/lint'),
|
|
5
|
+
fixedToken = require('../mixin/fixedToken'),
|
|
6
|
+
Parser = require('..'),
|
|
7
|
+
Token = require('.');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 模板或魔术字参数
|
|
11
|
+
* @classdesc `{childNodes: [Token, Token]}`
|
|
12
|
+
*/
|
|
13
|
+
class ParameterToken extends fixedToken(Token) {
|
|
14
|
+
type = 'parameter';
|
|
15
|
+
|
|
16
|
+
/** 是否是匿名参数 */
|
|
17
|
+
get anon() {
|
|
18
|
+
return this.firstChild.length === 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** getValue()的getter */
|
|
22
|
+
get value() {
|
|
23
|
+
return this.getValue();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
set value(value) {
|
|
27
|
+
this.setValue(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 是否是重复参数
|
|
32
|
+
* @this {ParameterToken & {parentNode: TranscludeToken}}
|
|
33
|
+
*/
|
|
34
|
+
get duplicated() {
|
|
35
|
+
const TranscludeToken = require('./transclude');
|
|
36
|
+
try {
|
|
37
|
+
return Boolean(this.parentNode?.getDuplicatedArgs()?.some(([key]) => key === this.name));
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {string|number} key 参数名
|
|
45
|
+
* @param {string} value 参数值
|
|
46
|
+
* @param {accum} accum
|
|
47
|
+
*/
|
|
48
|
+
constructor(key, value, config = Parser.getConfig(), accum = []) {
|
|
49
|
+
super(undefined, config, true, accum);
|
|
50
|
+
const keyToken = new Token(typeof key === 'number' ? undefined : key, config, true, accum, {
|
|
51
|
+
'Stage-11': ':', '!HeadingToken': '',
|
|
52
|
+
}),
|
|
53
|
+
token = new Token(value, config, true, accum);
|
|
54
|
+
keyToken.type = 'parameter-key';
|
|
55
|
+
token.type = 'parameter-value';
|
|
56
|
+
this.append(keyToken, token.setAttribute('stage', 2));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @override */
|
|
60
|
+
afterBuild() {
|
|
61
|
+
if (!this.anon) {
|
|
62
|
+
const TranscludeToken = require('./transclude');
|
|
63
|
+
const name = this.firstChild.toString('comment, noinclude, include')
|
|
64
|
+
.replace(/^[ \t\n\0\v]+|(?<=[^ \t\n\0\v])[ \t\n\0\v]+$/gu, ''),
|
|
65
|
+
{parentNode} = this;
|
|
66
|
+
this.setAttribute('name', name);
|
|
67
|
+
if (parentNode instanceof TranscludeToken) {
|
|
68
|
+
parentNode.getAttribute('keys').add(name);
|
|
69
|
+
parentNode.getArgs(name, false, false).add(this);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const /** @type {AstListener} */ parameterListener = ({prevTarget}, data) => {
|
|
73
|
+
if (!this.anon) { // 匿名参数不管怎么变动还是匿名
|
|
74
|
+
const {firstChild, name} = this;
|
|
75
|
+
if (prevTarget === firstChild) {
|
|
76
|
+
const newKey = firstChild.toString('comment, noinclude, include')
|
|
77
|
+
.replace(/^[ \t\n\0\v]+|(?<=[^ \t\n\0\v])[ \t\n\0\v]+$/gu, '');
|
|
78
|
+
data.oldKey = name;
|
|
79
|
+
data.newKey = newKey;
|
|
80
|
+
this.setAttribute('name', newKey);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
this.addEventListener(['remove', 'insert', 'replace', 'text'], parameterListener);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @override
|
|
89
|
+
* @param {string} selector
|
|
90
|
+
* @returns {string}
|
|
91
|
+
*/
|
|
92
|
+
toString(selector) {
|
|
93
|
+
return this.anon && !(selector && this.matches(selector))
|
|
94
|
+
? this.lastChild.toString(selector)
|
|
95
|
+
: super.toString(selector, '=');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @override
|
|
100
|
+
* @returns {string}
|
|
101
|
+
*/
|
|
102
|
+
text() {
|
|
103
|
+
return this.anon ? this.lastChild.text() : super.text('=');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** @override */
|
|
107
|
+
getGaps() {
|
|
108
|
+
return this.anon ? 0 : 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** @override */
|
|
112
|
+
print() {
|
|
113
|
+
return super.print({sep: this.anon ? '' : '='});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @override
|
|
118
|
+
* @param {number} start 起始位置
|
|
119
|
+
*/
|
|
120
|
+
lint(start = 0) {
|
|
121
|
+
const errors = super.lint(start),
|
|
122
|
+
{firstChild, lastChild} = this,
|
|
123
|
+
link = new RegExp(`https?://${extUrlCharFirst}${extUrlChar}$`, 'iu')
|
|
124
|
+
.exec(firstChild.toString('comment, noinclude, include'))?.[0];
|
|
125
|
+
if (link && new URL(link).search) {
|
|
126
|
+
const e = generateForChild(firstChild, {start}, '匿名参数中未转义的查询参数');
|
|
127
|
+
errors.push({
|
|
128
|
+
...e,
|
|
129
|
+
startIndex: e.endIndex,
|
|
130
|
+
endIndex: e.endIndex + 1,
|
|
131
|
+
startLine: e.endLine,
|
|
132
|
+
startCol: e.endCol,
|
|
133
|
+
endCol: e.endCol + 1,
|
|
134
|
+
excerpt: `${String(firstChild).slice(-25)}=${String(lastChild).slice(0, 25)}`,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return errors;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 获取参数值
|
|
142
|
+
* @this {ParameterToken & {parentNode: TranscludeToken}}
|
|
143
|
+
*/
|
|
144
|
+
getValue() {
|
|
145
|
+
const TranscludeToken = require('./transclude');
|
|
146
|
+
const value = this.lastChild.text();
|
|
147
|
+
return this.anon && this.parentNode?.isTemplate() ? value : value.trim();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** @override */
|
|
151
|
+
cloneNode() {
|
|
152
|
+
const [key, value] = this.cloneChildNodes(),
|
|
153
|
+
config = this.getAttribute('config');
|
|
154
|
+
return Parser.run(() => {
|
|
155
|
+
const token = new ParameterToken(this.anon ? Number(this.name) : undefined, undefined, config);
|
|
156
|
+
token.firstChild.safeReplaceWith(key);
|
|
157
|
+
token.lastChild.safeReplaceWith(value);
|
|
158
|
+
token.afterBuild();
|
|
159
|
+
return token;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @override
|
|
165
|
+
* @param {ParameterToken} token 待替换的节点
|
|
166
|
+
* @complexity `n`
|
|
167
|
+
*/
|
|
168
|
+
safeReplaceWith(token) {
|
|
169
|
+
Parser.warn(`${this.constructor.name}.safeReplaceWith 方法退化到 replaceWith。`);
|
|
170
|
+
return this.replaceWith(token);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 设置参数值
|
|
175
|
+
* @this {ParameterToken & {parentNode: TranscludeToken}}
|
|
176
|
+
* @param {string} value 参数值
|
|
177
|
+
* @throws `SyntaxError` 非法的模板参数
|
|
178
|
+
*/
|
|
179
|
+
setValue(value) {
|
|
180
|
+
value = String(value);
|
|
181
|
+
const TranscludeToken = require('./transclude');
|
|
182
|
+
const templateLike = this.parentNode?.isTemplate(),
|
|
183
|
+
wikitext = `{{${templateLike ? ':T|' : 'lc:'}${this.anon ? '' : '1='}${value}}}`,
|
|
184
|
+
root = Parser.parse(wikitext, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
185
|
+
{length, firstChild: transclude} = root,
|
|
186
|
+
/** @type {Token & {lastChild: ParameterToken}} */
|
|
187
|
+
{lastChild: parameter, type, name, length: transcludeLength} = transclude,
|
|
188
|
+
targetType = templateLike ? 'template' : 'magic-word',
|
|
189
|
+
targetName = templateLike ? 'T' : 'lc';
|
|
190
|
+
if (length !== 1 || type !== targetType || name !== targetName || transcludeLength !== 2
|
|
191
|
+
|| parameter.anon !== this.anon || parameter.name !== '1'
|
|
192
|
+
) {
|
|
193
|
+
throw new SyntaxError(`非法的模板参数:${noWrap(value)}`);
|
|
194
|
+
}
|
|
195
|
+
const {lastChild} = parameter;
|
|
196
|
+
parameter.destroy(true);
|
|
197
|
+
this.lastChild.safeReplaceWith(lastChild);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 修改参数名
|
|
202
|
+
* @this {ParameterToken & {parentNode: TranscludeToken}}
|
|
203
|
+
* @param {string} key 新参数名
|
|
204
|
+
* @param {boolean} force 是否无视冲突命名
|
|
205
|
+
* @throws `Error` 仅用于模板参数
|
|
206
|
+
* @throws `SyntaxError` 非法的模板参数名
|
|
207
|
+
* @throws `RangeError` 更名造成重复参数
|
|
208
|
+
*/
|
|
209
|
+
rename(key, force) {
|
|
210
|
+
key = String(key);
|
|
211
|
+
const TranscludeToken = require('./transclude');
|
|
212
|
+
const {parentNode} = this;
|
|
213
|
+
// 必须检测是否是TranscludeToken
|
|
214
|
+
if (!parentNode?.isTemplate() || !(parentNode instanceof TranscludeToken)) {
|
|
215
|
+
throw new Error(`${this.constructor.name}.rename 方法仅用于模板参数!`);
|
|
216
|
+
}
|
|
217
|
+
const root = Parser.parse(`{{:T|${key}=}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
218
|
+
{length, firstChild: template} = root,
|
|
219
|
+
{type, name, lastChild: parameter, length: templateLength} = template;
|
|
220
|
+
if (length !== 1 || type !== 'template' || name !== 'T' || templateLength !== 2) {
|
|
221
|
+
throw new SyntaxError(`非法的模板参数名:${key}`);
|
|
222
|
+
}
|
|
223
|
+
const {name: parameterName, firstChild} = parameter;
|
|
224
|
+
if (this.name === parameterName) {
|
|
225
|
+
Parser.warn('未改变实际参数名', parameterName);
|
|
226
|
+
} else if (parentNode.hasArg(parameterName)) {
|
|
227
|
+
if (force) {
|
|
228
|
+
Parser.warn('参数更名造成重复参数', parameterName);
|
|
229
|
+
} else {
|
|
230
|
+
throw new RangeError(`参数更名造成重复参数:${parameterName}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
parameter.destroy(true);
|
|
234
|
+
this.firstChild.safeReplaceWith(firstChild);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
Parser.classes.ParameterToken = __filename;
|
|
239
|
+
module.exports = ParameterToken;
|
package/src/syntax.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..'),
|
|
4
|
+
{undo} = require('../util/debug'),
|
|
5
|
+
{text} = require('../util/string'),
|
|
6
|
+
Token = require('.');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 满足特定语法格式的plain Token
|
|
10
|
+
* @classdesc `{childNodes: ...AstText|Token}`
|
|
11
|
+
*/
|
|
12
|
+
class SyntaxToken extends Token {
|
|
13
|
+
#pattern;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {string} wikitext 语法wikitext
|
|
17
|
+
* @param {RegExp} pattern 语法正则
|
|
18
|
+
* @param {string} type Token.type
|
|
19
|
+
* @param {accum} accum
|
|
20
|
+
* @param {acceptable} acceptable 可接受的子节点设置
|
|
21
|
+
* @throws `RangeError` 含有g修饰符的语法正则
|
|
22
|
+
*/
|
|
23
|
+
constructor(wikitext, pattern, type = 'plain', config = Parser.getConfig(), accum = [], acceptable = undefined) {
|
|
24
|
+
if (pattern.global) {
|
|
25
|
+
throw new RangeError(`SyntaxToken 的语法正则不能含有 g 修饰符:${pattern}`);
|
|
26
|
+
}
|
|
27
|
+
super(wikitext, config, true, accum, acceptable);
|
|
28
|
+
this.type = type;
|
|
29
|
+
this.#pattern = pattern;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @override */
|
|
33
|
+
cloneNode() {
|
|
34
|
+
const cloned = this.cloneChildNodes(),
|
|
35
|
+
config = this.getAttribute('config'),
|
|
36
|
+
acceptable = this.getAttribute('acceptable');
|
|
37
|
+
return Parser.run(() => {
|
|
38
|
+
const token = new SyntaxToken(undefined, this.#pattern, this.type, config, [], acceptable);
|
|
39
|
+
token.append(...cloned);
|
|
40
|
+
token.afterBuild();
|
|
41
|
+
return token;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @override */
|
|
46
|
+
afterBuild() {
|
|
47
|
+
const /** @type {AstListener} */ syntaxListener = (e, data) => {
|
|
48
|
+
const pattern = this.#pattern;
|
|
49
|
+
if (!Parser.running && !pattern.test(this.text())) {
|
|
50
|
+
undo(e, data);
|
|
51
|
+
Parser.error(`不可修改 ${this.constructor.name} 的语法!`, pattern);
|
|
52
|
+
throw new Error(`不可修改 ${this.constructor.name} 的语法!`);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
this.addEventListener(['remove', 'insert', 'replace', 'text'], syntaxListener);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @override
|
|
60
|
+
* @template {string} T
|
|
61
|
+
* @param {T} key 属性键
|
|
62
|
+
* @returns {TokenAttribute<T>}
|
|
63
|
+
*/
|
|
64
|
+
getAttribute(key) {
|
|
65
|
+
return key === 'pattern' ? this.#pattern : super.getAttribute(key);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @override
|
|
70
|
+
* @param {PropertyKey} key 属性键
|
|
71
|
+
*/
|
|
72
|
+
hasAttribute(key) {
|
|
73
|
+
return key === 'pattern' || super.hasAttribute(key);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @override
|
|
78
|
+
* @param {...Token} elements 待替换的子节点
|
|
79
|
+
* @complexity `n`
|
|
80
|
+
*/
|
|
81
|
+
replaceChildren(...elements) {
|
|
82
|
+
if (this.#pattern.test(text(elements))) {
|
|
83
|
+
Parser.run(() => {
|
|
84
|
+
super.replaceChildren(...elements);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
Parser.classes.SyntaxToken = __filename;
|
|
91
|
+
module.exports = SyntaxToken;
|