wikiparser-node 0.3.1 → 0.5.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 +1 -1
- package/config/default.json +13 -17
- package/config/llwiki.json +11 -79
- package/config/moegirl.json +7 -1
- package/config/zhwiki.json +1269 -0
- package/index.js +130 -97
- package/lib/element.js +410 -518
- package/lib/node.js +493 -115
- package/lib/ranges.js +27 -19
- package/lib/text.js +175 -0
- package/lib/title.js +14 -6
- package/mixin/attributeParent.js +70 -24
- package/mixin/fixedToken.js +18 -10
- package/mixin/hidden.js +6 -4
- package/mixin/sol.js +39 -12
- package/package.json +17 -4
- package/parser/brackets.js +18 -18
- package/parser/commentAndExt.js +16 -14
- package/parser/converter.js +14 -13
- package/parser/externalLinks.js +12 -11
- package/parser/hrAndDoubleUnderscore.js +24 -14
- package/parser/html.js +8 -7
- package/parser/links.js +13 -13
- package/parser/list.js +12 -11
- package/parser/magicLinks.js +11 -10
- package/parser/quotes.js +6 -5
- package/parser/selector.js +175 -0
- package/parser/table.js +31 -24
- package/src/arg.js +91 -43
- package/src/atom/hidden.js +5 -2
- package/src/atom/index.js +17 -9
- package/src/attribute.js +210 -101
- package/src/converter.js +78 -43
- package/src/converterFlags.js +104 -45
- package/src/converterRule.js +136 -78
- package/src/extLink.js +81 -27
- package/src/gallery.js +63 -20
- package/src/heading.js +58 -20
- package/src/html.js +138 -48
- package/src/imageParameter.js +93 -58
- package/src/index.js +314 -186
- package/src/link/category.js +22 -54
- package/src/link/file.js +83 -32
- package/src/link/galleryImage.js +21 -7
- package/src/link/index.js +170 -81
- package/src/magicLink.js +64 -14
- package/src/nowiki/comment.js +36 -10
- package/src/nowiki/dd.js +37 -22
- package/src/nowiki/doubleUnderscore.js +21 -7
- package/src/nowiki/hr.js +11 -7
- package/src/nowiki/index.js +16 -9
- package/src/nowiki/list.js +2 -2
- package/src/nowiki/noinclude.js +8 -4
- package/src/nowiki/quote.js +38 -7
- package/src/onlyinclude.js +24 -7
- package/src/parameter.js +102 -62
- package/src/syntax.js +23 -20
- package/src/table/index.js +282 -174
- package/src/table/td.js +112 -61
- package/src/table/tr.js +135 -74
- package/src/tagPair/ext.js +30 -23
- package/src/tagPair/include.js +26 -11
- package/src/tagPair/index.js +72 -29
- package/src/transclude.js +235 -127
- package/tool/index.js +42 -32
- package/util/debug.js +21 -18
- package/util/diff.js +76 -0
- package/util/lint.js +40 -0
- package/util/string.js +56 -26
- package/.eslintrc.json +0 -319
- package/errors/README +0 -1
- package/jsconfig.json +0 -7
- package/printed/README +0 -1
- package/typings/element.d.ts +0 -28
- package/typings/index.d.ts +0 -52
- package/typings/node.d.ts +0 -23
- package/typings/parser.d.ts +0 -9
- package/typings/table.d.ts +0 -14
- package/typings/token.d.ts +0 -22
- package/typings/tool.d.ts +0 -10
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..');
|
|
4
|
+
|
|
5
|
+
const /** @type {pseudo[]} */ simplePseudos = [
|
|
6
|
+
'root',
|
|
7
|
+
'first-child',
|
|
8
|
+
'first-of-type',
|
|
9
|
+
'last-child',
|
|
10
|
+
'last-of-type',
|
|
11
|
+
'only-child',
|
|
12
|
+
'only-of-type',
|
|
13
|
+
'empty',
|
|
14
|
+
'parent',
|
|
15
|
+
'header',
|
|
16
|
+
'hidden',
|
|
17
|
+
'visible',
|
|
18
|
+
'only-whitespace',
|
|
19
|
+
'local-link',
|
|
20
|
+
'read-only',
|
|
21
|
+
'read-write',
|
|
22
|
+
'invalid',
|
|
23
|
+
'required',
|
|
24
|
+
'optional',
|
|
25
|
+
],
|
|
26
|
+
/** @type {pseudo[]} */ complexPseudos = [
|
|
27
|
+
'is',
|
|
28
|
+
'not',
|
|
29
|
+
'nth-child',
|
|
30
|
+
'nth-of-type',
|
|
31
|
+
'nth-last-child',
|
|
32
|
+
'nth-last-of-type',
|
|
33
|
+
'contains',
|
|
34
|
+
'has',
|
|
35
|
+
'lang',
|
|
36
|
+
],
|
|
37
|
+
specialChars = [
|
|
38
|
+
['[', '['],
|
|
39
|
+
[']', ']'],
|
|
40
|
+
['(', '('],
|
|
41
|
+
[')', ')'],
|
|
42
|
+
['"', '"'],
|
|
43
|
+
["'", '''],
|
|
44
|
+
[':', ':'],
|
|
45
|
+
['\\', '\'],
|
|
46
|
+
['&', '&'],
|
|
47
|
+
],
|
|
48
|
+
pseudoRegex = new RegExp(`:(${complexPseudos.join('|')})$`, 'u'),
|
|
49
|
+
regularRegex = /[[(,>+~]|\s+/u,
|
|
50
|
+
attributeRegex = /^\s*(\w+)\s*(?:([~|^$*!]?=)\s*("[^"]*"|'[^']*'|[^\s[\]]+)(?:\s+(i))?\s*)?\]/u,
|
|
51
|
+
functionRegex = /^(\s*"[^"]*"\s*|\s*'[^']*'\s*|[^()]*)\)/u;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 清理转义符号
|
|
55
|
+
* @param {string} selector
|
|
56
|
+
*/
|
|
57
|
+
const sanitize = selector => {
|
|
58
|
+
for (const [c, escaped] of specialChars) {
|
|
59
|
+
selector = selector.replaceAll(`\\${c}`, escaped);
|
|
60
|
+
}
|
|
61
|
+
return selector;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 还原转义符号
|
|
66
|
+
* @param {string|undefined} selector
|
|
67
|
+
*/
|
|
68
|
+
const desanitize = selector => {
|
|
69
|
+
if (selector === undefined) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
for (const [c, escaped] of specialChars) {
|
|
73
|
+
selector = selector.replaceAll(escaped, c);
|
|
74
|
+
}
|
|
75
|
+
return selector.trim();
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 去除首尾的引号
|
|
80
|
+
* @param {string|undefined} val 属性值或伪选择器函数的参数
|
|
81
|
+
*/
|
|
82
|
+
const deQuote = val => {
|
|
83
|
+
if (val === undefined) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const quotes = /^(["']).*\1$/u.exec(val)?.[1];
|
|
87
|
+
return quotes ? val.slice(1, -1) : val;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 解析简单伪选择器
|
|
92
|
+
* @param {SelectorArray} step 当前顶部
|
|
93
|
+
* @param {string} str 不含属性和复杂伪选择器的语句
|
|
94
|
+
* @throws `SyntaxError` 非法的选择器
|
|
95
|
+
*/
|
|
96
|
+
const pushSimple = (step, str) => {
|
|
97
|
+
const pieces = str.trim().split(':'),
|
|
98
|
+
// eslint-disable-next-line unicorn/explicit-length-check
|
|
99
|
+
i = pieces.slice(1).findIndex(pseudo => simplePseudos.includes(pseudo)) + 1 || pieces.length;
|
|
100
|
+
if (pieces.slice(i).some(pseudo => !simplePseudos.includes(pseudo))) {
|
|
101
|
+
throw new SyntaxError(`非法的选择器!\n${str}\n可能需要将':'转义为'\\:'。`);
|
|
102
|
+
}
|
|
103
|
+
step.push(desanitize(pieces.slice(0, i).join(':')), ...pieces.slice(i).map(piece => `:${piece}`));
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 解析选择器
|
|
108
|
+
* @param {string} selector
|
|
109
|
+
* @throws `SyntaxError` 非法的选择器
|
|
110
|
+
*/
|
|
111
|
+
const parseSelector = selector => {
|
|
112
|
+
selector = selector.trim();
|
|
113
|
+
const /** @type {SelectorArray[][]} */ stack = [[[]]];
|
|
114
|
+
let sanitized = sanitize(selector),
|
|
115
|
+
regex = regularRegex,
|
|
116
|
+
mt = regex.exec(sanitized),
|
|
117
|
+
[condition] = stack,
|
|
118
|
+
[step] = condition;
|
|
119
|
+
while (mt) {
|
|
120
|
+
let {0: syntax, index} = mt;
|
|
121
|
+
if (syntax.trim() === '') {
|
|
122
|
+
index += syntax.length;
|
|
123
|
+
const char = sanitized[index];
|
|
124
|
+
syntax = [',', '>', '+', '~'].includes(char) ? char : '';
|
|
125
|
+
}
|
|
126
|
+
if (syntax === ',') { // 情形1:并列
|
|
127
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
128
|
+
condition = [[]];
|
|
129
|
+
[step] = condition;
|
|
130
|
+
stack.push(condition);
|
|
131
|
+
} else if (['>', '+', '~', ''].includes(syntax)) { // 情形2:关系
|
|
132
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
133
|
+
if (!step.some(Boolean)) {
|
|
134
|
+
throw new SyntaxError(`非法的选择器!\n${selector}\n可能需要通用选择器'*'。`);
|
|
135
|
+
}
|
|
136
|
+
step.relation = syntax;
|
|
137
|
+
step = [];
|
|
138
|
+
condition.push(step);
|
|
139
|
+
} else if (syntax === '[') { // 情形3:属性开启
|
|
140
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
141
|
+
regex = attributeRegex;
|
|
142
|
+
} else if (syntax.at(-1) === ']') { // 情形4:属性闭合
|
|
143
|
+
mt[3] = desanitize(deQuote(mt[3]));
|
|
144
|
+
step.push(mt.slice(1));
|
|
145
|
+
regex = regularRegex;
|
|
146
|
+
} else if (syntax === '(') { // 情形5:伪选择器开启
|
|
147
|
+
const pseudoExec = pseudoRegex.exec(sanitized.slice(0, index));
|
|
148
|
+
if (!pseudoExec) {
|
|
149
|
+
throw new SyntaxError(`非法的选择器!\n${desanitize(sanitized)}\n请检查伪选择器是否存在。`);
|
|
150
|
+
}
|
|
151
|
+
pushSimple(step, sanitized.slice(0, pseudoExec.index));
|
|
152
|
+
step.push(pseudoExec[1]); // 临时存放复杂伪选择器
|
|
153
|
+
regex = functionRegex;
|
|
154
|
+
} else { // 情形6:伪选择器闭合
|
|
155
|
+
const /** @type {pseudo} */ pseudo = step.pop();
|
|
156
|
+
mt.push(pseudo);
|
|
157
|
+
mt[1] = deQuote(mt[1]);
|
|
158
|
+
step.push(mt.slice(1));
|
|
159
|
+
regex = regularRegex;
|
|
160
|
+
}
|
|
161
|
+
sanitized = sanitized.slice(index + syntax.length);
|
|
162
|
+
if ([',', '>', '+', '~'].includes(syntax)) {
|
|
163
|
+
sanitized = sanitized.trim();
|
|
164
|
+
}
|
|
165
|
+
mt = regex.exec(sanitized);
|
|
166
|
+
}
|
|
167
|
+
if (regex === regularRegex) {
|
|
168
|
+
pushSimple(step, sanitized);
|
|
169
|
+
return stack;
|
|
170
|
+
}
|
|
171
|
+
throw new SyntaxError(`非法的选择器!\n${selector}\n检测到未闭合的'${regex === attributeRegex ? '[' : '('}'`);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
Parser.parsers.parseSelector = __filename;
|
|
175
|
+
module.exports = parseSelector;
|
package/parser/table.js
CHANGED
|
@@ -1,29 +1,36 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const Parser = require('..'),
|
|
4
|
+
AstText = require('../lib/text'),
|
|
5
|
+
Token = require('../src'),
|
|
6
|
+
TableToken = require('../src/table'),
|
|
7
|
+
TrToken = require('../src/table/tr'),
|
|
8
|
+
TdToken = require('../src/table/td'),
|
|
9
|
+
DdToken = require('../src/nowiki/dd');
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
|
-
*
|
|
7
|
-
* @param {{firstChild:
|
|
12
|
+
* 解析表格,注意`tr`和`td`包含开头的换行
|
|
13
|
+
* @param {Token & {firstChild: AstText}} root 根节点
|
|
8
14
|
* @param {accum} accum
|
|
9
15
|
*/
|
|
10
|
-
const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = []) => {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const parseTable = ({firstChild: {data}, type, name}, config = Parser.getConfig(), accum = []) => {
|
|
17
|
+
const /** @type {TrToken[]} */ stack = [],
|
|
18
|
+
lines = data.split('\n');
|
|
19
|
+
let out = type === 'root' || type === 'ext-inner' && name === 'poem' ? '' : `\n${lines.shift()}`;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 向表格中插入纯文本
|
|
23
|
+
* @param {string} str 待插入的文本
|
|
24
|
+
* @param {TrToken} top 当前解析的表格或表格行
|
|
25
|
+
*/
|
|
26
|
+
const push = (str, top) => {
|
|
20
27
|
if (!top) {
|
|
21
28
|
out += str;
|
|
22
29
|
return;
|
|
23
30
|
}
|
|
24
|
-
const {
|
|
25
|
-
if (
|
|
26
|
-
|
|
31
|
+
const /** @type {Token & {firstChild: AstText}} */ {lastChild} = top;
|
|
32
|
+
if (lastChild.isPlain()) {
|
|
33
|
+
lastChild.firstChild.appendData(str);
|
|
27
34
|
} else {
|
|
28
35
|
const token = new Token(str, config, true, accum);
|
|
29
36
|
token.type = 'table-inter';
|
|
@@ -32,9 +39,9 @@ const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = [])
|
|
|
32
39
|
};
|
|
33
40
|
for (const outLine of lines) {
|
|
34
41
|
let top = stack.pop();
|
|
35
|
-
const [spaces] = /^(?:\s|\0\d+c\
|
|
36
|
-
|
|
37
|
-
matchesStart = /^(:*)((?:\s|\0\d+c\
|
|
42
|
+
const [spaces] = /^(?:\s|\0\d+c\x7F)*/u.exec(outLine),
|
|
43
|
+
line = outLine.slice(spaces.length),
|
|
44
|
+
matchesStart = /^(:*)((?:\s|\0\d+c\x7F)*)(\{\||\{\0\d+!\x7F|\0\d+\{\x7F)(.*)$/u.exec(line);
|
|
38
45
|
if (matchesStart) {
|
|
39
46
|
while (top && top.type !== 'td') {
|
|
40
47
|
top = stack.pop();
|
|
@@ -43,7 +50,7 @@ const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = [])
|
|
|
43
50
|
if (indent) {
|
|
44
51
|
new DdToken(indent, config, accum);
|
|
45
52
|
}
|
|
46
|
-
push(`\n${spaces}${indent && `\0${accum.length - 1}d\
|
|
53
|
+
push(`\n${spaces}${indent && `\0${accum.length - 1}d\x7F`}${moreSpaces}\0${accum.length}b\x7F`, top);
|
|
47
54
|
const table = new TableToken(tableSyntax, attr, config, accum);
|
|
48
55
|
stack.push(...top ? [top] : [], table);
|
|
49
56
|
continue;
|
|
@@ -51,8 +58,8 @@ const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = [])
|
|
|
51
58
|
out += `\n${outLine}`;
|
|
52
59
|
continue;
|
|
53
60
|
}
|
|
54
|
-
const matches
|
|
55
|
-
|
|
61
|
+
const matches = // eslint-disable-line operator-linebreak
|
|
62
|
+
/^(?:(\|\}|\0\d+!\x7F\}|\0\d+\}\x7F)|(\|-+|\0\d+!\x7F-+|\0\d+-\x7F-*)(?!-)|(!|(?:\||\0\d+!\x7F)\+?))(.*)$/u
|
|
56
63
|
.exec(line);
|
|
57
64
|
if (!matches) {
|
|
58
65
|
push(`\n${outLine}`, top);
|
|
@@ -81,8 +88,8 @@ const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = [])
|
|
|
81
88
|
top = stack.pop();
|
|
82
89
|
}
|
|
83
90
|
const regex = cell === '!'
|
|
84
|
-
? /!!|(?:\||\0\d+!\
|
|
85
|
-
: /(?:\||\0\d+!\
|
|
91
|
+
? /!!|(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu
|
|
92
|
+
: /(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu;
|
|
86
93
|
let mt = regex.exec(attr),
|
|
87
94
|
lastIndex = 0,
|
|
88
95
|
lastSyntax = `\n${spaces}${cell}`;
|
package/src/arg.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {text, noWrap} = require('../util/string'),
|
|
4
|
-
|
|
4
|
+
{generateForChild} = require('../util/lint'),
|
|
5
|
+
Parser = require('..'),
|
|
5
6
|
Token = require('.');
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -12,68 +13,86 @@ class ArgToken extends Token {
|
|
|
12
13
|
type = 'arg';
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
|
-
* @param {string[]} parts
|
|
16
|
+
* @param {string[]} parts 以'|'分隔的各部分
|
|
16
17
|
* @param {accum} accum
|
|
17
18
|
* @complexity `n`
|
|
18
19
|
*/
|
|
19
20
|
constructor(parts, config = Parser.getConfig(), accum = []) {
|
|
20
21
|
super(undefined, config, true, accum, {AtomToken: 0, Token: 1, HiddenToken: '2:'});
|
|
21
|
-
for (
|
|
22
|
+
for (let i = 0; i < parts.length; i++) {
|
|
22
23
|
if (i === 0 || i > 1) {
|
|
23
|
-
const AtomToken = i === 0 ? require('./atom') : require('./atom/hidden')
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
const AtomToken = i === 0 ? require('./atom') : require('./atom/hidden');
|
|
25
|
+
const token = new AtomToken(parts[i], i === 0 ? 'arg-name' : undefined, config, accum, {
|
|
26
|
+
'Stage-2': ':', '!HeadingToken': '',
|
|
27
|
+
});
|
|
27
28
|
this.appendChild(token);
|
|
28
29
|
} else {
|
|
29
|
-
const token = new Token(
|
|
30
|
+
const token = new Token(parts[i], config, true, accum);
|
|
30
31
|
token.type = 'arg-default';
|
|
31
32
|
this.appendChild(token.setAttribute('stage', 2));
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
|
-
this.protectChildren(0);
|
|
35
|
+
this.getAttribute('protectChildren')(0);
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
/** @override */
|
|
37
39
|
cloneNode() {
|
|
38
|
-
const [name, ...cloned] = this.
|
|
40
|
+
const [name, ...cloned] = this.cloneChildNodes();
|
|
39
41
|
return Parser.run(() => {
|
|
40
42
|
const token = new ArgToken([''], this.getAttribute('config'));
|
|
41
|
-
token.
|
|
43
|
+
token.firstChild.safeReplaceWith(name);
|
|
42
44
|
token.append(...cloned);
|
|
43
45
|
return token.afterBuild();
|
|
44
46
|
});
|
|
45
47
|
}
|
|
46
48
|
|
|
49
|
+
/** @override */
|
|
47
50
|
afterBuild() {
|
|
48
|
-
this.setAttribute('name', this.
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
51
|
+
this.setAttribute('name', this.firstChild.text().trim());
|
|
52
|
+
const /** @type {AstListener} */ argListener = ({prevTarget}) => {
|
|
53
|
+
if (prevTarget === this.firstChild) {
|
|
54
|
+
this.setAttribute('name', prevTarget.text().trim());
|
|
55
|
+
}
|
|
56
|
+
};
|
|
55
57
|
this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
|
|
56
58
|
return this;
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
/**
|
|
62
|
+
* @override
|
|
63
|
+
* @param {string} selector
|
|
64
|
+
*/
|
|
65
|
+
toString(selector) {
|
|
66
|
+
return selector && this.matches(selector) ? '' : `{{{${super.toString(selector, '|')}}}}`;
|
|
61
67
|
}
|
|
62
68
|
|
|
69
|
+
/** @override */
|
|
63
70
|
getPadding() {
|
|
64
71
|
return 3;
|
|
65
72
|
}
|
|
66
73
|
|
|
74
|
+
/** @override */
|
|
67
75
|
getGaps() {
|
|
68
76
|
return 1;
|
|
69
77
|
}
|
|
70
78
|
|
|
71
|
-
/** @
|
|
79
|
+
/** @override */
|
|
80
|
+
print() {
|
|
81
|
+
return super.print({pre: '{{{', post: '}}}', sep: '|'});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @override
|
|
86
|
+
* @returns {string}
|
|
87
|
+
*/
|
|
72
88
|
text() {
|
|
73
|
-
return `{{{${text(this.
|
|
89
|
+
return `{{{${text(this.childNodes.slice(0, 2), '|')}}}}`;
|
|
74
90
|
}
|
|
75
91
|
|
|
76
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* 移除无效部分
|
|
94
|
+
* @complexity `n`
|
|
95
|
+
*/
|
|
77
96
|
removeRedundant() {
|
|
78
97
|
Parser.run(() => {
|
|
79
98
|
for (let i = this.childNodes.length - 1; i > 1; i--) {
|
|
@@ -83,8 +102,26 @@ class ArgToken extends Token {
|
|
|
83
102
|
}
|
|
84
103
|
|
|
85
104
|
/**
|
|
86
|
-
*
|
|
87
|
-
* @param {number}
|
|
105
|
+
* @override
|
|
106
|
+
* @param {number} start 起始位置
|
|
107
|
+
* @returns {LintError[]}
|
|
108
|
+
*/
|
|
109
|
+
lint(start = 0) {
|
|
110
|
+
const {childNodes: [argName, argDefault, ...rest]} = this,
|
|
111
|
+
errors = argName.lint(start + 3);
|
|
112
|
+
if (argDefault) {
|
|
113
|
+
errors.push(...argDefault.lint(start + 4 + String(argName).length));
|
|
114
|
+
}
|
|
115
|
+
if (rest.length > 0) {
|
|
116
|
+
const rect = this.getRootNode().posFromIndex(start);
|
|
117
|
+
errors.push(...rest.map(child => generateForChild(child, rect, '三重括号内的不可见部分')));
|
|
118
|
+
}
|
|
119
|
+
return errors;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 移除子节点,且在移除`arg-default`子节点时自动移除全部多余子节点
|
|
124
|
+
* @param {number} i 移除位置
|
|
88
125
|
* @returns {Token}
|
|
89
126
|
*/
|
|
90
127
|
removeAt(i) {
|
|
@@ -94,11 +131,16 @@ class ArgToken extends Token {
|
|
|
94
131
|
return super.removeAt(i);
|
|
95
132
|
}
|
|
96
133
|
|
|
97
|
-
/**
|
|
134
|
+
/**
|
|
135
|
+
* @override
|
|
136
|
+
* @param {Token} token 待插入的子节点
|
|
137
|
+
* @param {number} i 插入位置
|
|
138
|
+
* @throws `RangeError` 不可插入多余子节点
|
|
139
|
+
*/
|
|
98
140
|
insertAt(token, i = this.childNodes.length) {
|
|
99
141
|
const j = i < 0 ? i + this.childNodes.length : i;
|
|
100
142
|
if (j > 1 && !Parser.running) {
|
|
101
|
-
throw new RangeError(`${this.constructor.name}
|
|
143
|
+
throw new RangeError(`${this.constructor.name} 不可插入多余的子节点!`);
|
|
102
144
|
}
|
|
103
145
|
super.insertAt(token, i);
|
|
104
146
|
if (j === 1) {
|
|
@@ -107,36 +149,42 @@ class ArgToken extends Token {
|
|
|
107
149
|
return token;
|
|
108
150
|
}
|
|
109
151
|
|
|
110
|
-
/**
|
|
152
|
+
/**
|
|
153
|
+
* 设置参数名
|
|
154
|
+
* @param {string} name 新参数名
|
|
155
|
+
* @throws `SyntaxError` 非法的参数名
|
|
156
|
+
*/
|
|
111
157
|
setName(name) {
|
|
112
158
|
name = String(name);
|
|
113
159
|
const root = Parser.parse(`{{{${name}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
114
|
-
{childNodes: {length},
|
|
115
|
-
if (length !== 1 ||
|
|
160
|
+
{childNodes: {length}, firstChild: arg} = root;
|
|
161
|
+
if (length !== 1 || arg.type !== 'arg' || arg.childNodes.length !== 1) {
|
|
116
162
|
throw new SyntaxError(`非法的参数名称:${noWrap(name)}`);
|
|
117
163
|
}
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
this.firstElementChild.safeReplaceWith(newName);
|
|
164
|
+
const {firstChild} = arg;
|
|
165
|
+
arg.destroy(true);
|
|
166
|
+
this.firstChild.safeReplaceWith(firstChild);
|
|
122
167
|
}
|
|
123
168
|
|
|
124
|
-
/**
|
|
169
|
+
/**
|
|
170
|
+
* 设置预设值
|
|
171
|
+
* @param {string} value 预设值
|
|
172
|
+
* @throws `SyntaxError` 非法的参数预设值
|
|
173
|
+
*/
|
|
125
174
|
setDefault(value) {
|
|
126
175
|
value = String(value);
|
|
127
176
|
const root = Parser.parse(`{{{|${value}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
128
|
-
{childNodes: {length},
|
|
129
|
-
if (length !== 1 ||
|
|
177
|
+
{childNodes: {length}, firstChild: arg} = root;
|
|
178
|
+
if (length !== 1 || arg.type !== 'arg' || arg.childNodes.length !== 2) {
|
|
130
179
|
throw new SyntaxError(`非法的参数预设值:${noWrap(value)}`);
|
|
131
180
|
}
|
|
132
|
-
const [, oldDefault] = this
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
firstElementChild.destroy();
|
|
181
|
+
const {childNodes: [, oldDefault]} = this,
|
|
182
|
+
{lastChild} = arg;
|
|
183
|
+
arg.destroy(true);
|
|
136
184
|
if (oldDefault) {
|
|
137
|
-
oldDefault.safeReplaceWith(
|
|
185
|
+
oldDefault.safeReplaceWith(lastChild);
|
|
138
186
|
} else {
|
|
139
|
-
this.appendChild(
|
|
187
|
+
this.appendChild(lastChild);
|
|
140
188
|
}
|
|
141
189
|
}
|
|
142
190
|
}
|
package/src/atom/hidden.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const hidden = require('../../mixin/hidden'),
|
|
4
|
-
|
|
4
|
+
Parser = require('../..'),
|
|
5
5
|
AtomToken = require('.');
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/** 不可见的节点 */
|
|
8
|
+
class HiddenToken extends hidden(AtomToken) {
|
|
9
|
+
type = 'hidden';
|
|
10
|
+
}
|
|
8
11
|
|
|
9
12
|
Parser.classes.HiddenToken = __filename;
|
|
10
13
|
module.exports = HiddenToken;
|
package/src/atom/index.js
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const Parser = require('../..'),
|
|
4
4
|
Token = require('..');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 不会被继续解析的plain Token
|
|
8
|
-
* @classdesc `{childNodes: (
|
|
8
|
+
* @classdesc `{childNodes: (AstText|Token)[]}`
|
|
9
9
|
*/
|
|
10
10
|
class AtomToken extends Token {
|
|
11
|
+
type = 'plain';
|
|
12
|
+
|
|
11
13
|
/**
|
|
12
|
-
* @param {
|
|
14
|
+
* @param {string} wikitext wikitext
|
|
15
|
+
* @param {string|undefined} type Token.type
|
|
13
16
|
* @param {accum} accum
|
|
14
|
-
* @param {acceptable} acceptable
|
|
17
|
+
* @param {acceptable} acceptable 可接受的子节点设置
|
|
15
18
|
*/
|
|
16
|
-
constructor(wikitext, type
|
|
19
|
+
constructor(wikitext, type, config = Parser.getConfig(), accum = [], acceptable = null) {
|
|
17
20
|
super(wikitext, config, true, accum, acceptable);
|
|
18
|
-
|
|
21
|
+
if (type) {
|
|
22
|
+
this.type = type;
|
|
23
|
+
}
|
|
19
24
|
}
|
|
20
25
|
|
|
26
|
+
/**
|
|
27
|
+
* @override
|
|
28
|
+
* @this {AtomToken & {constructor: typeof AtomToken}}
|
|
29
|
+
*/
|
|
21
30
|
cloneNode() {
|
|
22
|
-
const cloned = this.
|
|
23
|
-
/** @type {typeof AtomToken} */ Constructor = this.constructor,
|
|
31
|
+
const cloned = this.cloneChildNodes(),
|
|
24
32
|
config = this.getAttribute('config'),
|
|
25
33
|
acceptable = this.getAttribute('acceptable'),
|
|
26
|
-
token = Parser.run(() => new
|
|
34
|
+
token = Parser.run(() => new this.constructor(undefined, this.type, config, [], acceptable));
|
|
27
35
|
token.append(...cloned);
|
|
28
36
|
return token;
|
|
29
37
|
}
|