wikiparser-node 0.9.2-b → 0.10.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 +40 -0
- package/config/.schema.json +134 -0
- package/config/default.json +832 -0
- package/config/llwiki.json +630 -0
- package/config/moegirl.json +729 -0
- package/config/zhwiki.json +1269 -0
- package/i18n/zh-hans.json +1 -0
- package/i18n/zh-hant.json +1 -0
- package/index.js +333 -0
- package/lib/element.js +611 -0
- package/lib/node.js +770 -0
- package/lib/ranges.js +130 -0
- package/lib/text.js +263 -0
- package/lib/title.js +83 -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 +54 -0
- package/package.json +49 -47
- package/parser/brackets.js +126 -0
- package/parser/commentAndExt.js +59 -0
- package/parser/converter.js +46 -0
- package/parser/externalLinks.js +33 -0
- package/parser/hrAndDoubleUnderscore.js +49 -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 +207 -0
- package/src/atom/hidden.js +13 -0
- package/src/atom/index.js +43 -0
- package/src/attribute.js +470 -0
- package/src/attributes.js +453 -0
- package/src/charinsert.js +97 -0
- package/src/converter.js +176 -0
- package/src/converterFlags.js +284 -0
- package/src/converterRule.js +256 -0
- package/src/extLink.js +180 -0
- package/src/gallery.js +149 -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 +254 -0
- package/src/imageParameter.js +303 -0
- package/src/imagemap.js +199 -0
- package/src/imagemapLink.js +41 -0
- package/src/index.js +932 -0
- package/src/link/category.js +44 -0
- package/src/link/file.js +287 -0
- package/src/link/galleryImage.js +120 -0
- package/src/link/index.js +388 -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 +93 -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 +35 -0
- package/src/parameter.js +239 -0
- package/src/syntax.js +91 -0
- package/src/table/index.js +983 -0
- package/src/table/td.js +338 -0
- package/src/table/tr.js +319 -0
- package/src/tagPair/ext.js +145 -0
- package/src/tagPair/include.js +50 -0
- package/src/tagPair/index.js +126 -0
- package/src/transclude.js +843 -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 +55 -0
- package/util/string.js +126 -0
- package/bundle/bundle.min.js +0 -38
- package/extensions/editor.css +0 -62
- package/extensions/editor.js +0 -328
- package/extensions/ui.css +0 -119
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {extUrlChar, extUrlCharFirst} = require('../util/string'),
|
|
4
|
+
Parser = require('..'),
|
|
5
|
+
MagicLinkToken = require('../src/magicLink');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 解析自由外链
|
|
9
|
+
* @param {string} wikitext wikitext
|
|
10
|
+
* @param {accum} accum
|
|
11
|
+
*/
|
|
12
|
+
const parseMagicLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
|
|
13
|
+
const regex = new RegExp(`(?<![\\p{L}\\d_])(?:${config.protocol})(${extUrlCharFirst}${extUrlChar})`, 'giu');
|
|
14
|
+
return wikitext.replace(regex, /** @param {string} p1 */ (m, p1) => {
|
|
15
|
+
let trail = '',
|
|
16
|
+
url = m;
|
|
17
|
+
const m2 = /&(?:lt|gt|nbsp|#x0*(?:3[ce]|a0)|#0*(?:6[02]|160));/iu.exec(url);
|
|
18
|
+
if (m2) {
|
|
19
|
+
trail = url.slice(m2.index);
|
|
20
|
+
url = url.slice(0, m2.index);
|
|
21
|
+
}
|
|
22
|
+
const sep = new RegExp(`[,;.:!?${url.includes('(') ? '' : ')'}]+$`, 'u'),
|
|
23
|
+
sepChars = sep.exec(url);
|
|
24
|
+
if (sepChars) {
|
|
25
|
+
let correction = 0;
|
|
26
|
+
if (sepChars[0][0] === ';' && /&(?:[a-z]+|#x[\da-f]+|#\d+)$/iu.test(url.slice(0, sepChars.index))) {
|
|
27
|
+
correction = 1;
|
|
28
|
+
}
|
|
29
|
+
trail = `${url.slice(sepChars.index + correction)}${trail}`;
|
|
30
|
+
url = url.slice(0, sepChars.index + correction);
|
|
31
|
+
}
|
|
32
|
+
if (trail.length >= p1.length) {
|
|
33
|
+
return m;
|
|
34
|
+
}
|
|
35
|
+
new MagicLinkToken(url, false, config, accum);
|
|
36
|
+
return `\0${accum.length - 1}w\x7F${trail}`;
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
Parser.parsers.parseMagicLinks = __filename;
|
|
41
|
+
module.exports = parseMagicLinks;
|
package/parser/quotes.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..'),
|
|
4
|
+
QuoteToken = require('../src/nowiki/quote');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 解析单引号
|
|
8
|
+
* @param {string} text wikitext
|
|
9
|
+
* @param {accum} accum
|
|
10
|
+
*/
|
|
11
|
+
const parseQuotes = (text, config = Parser.getConfig(), accum = []) => {
|
|
12
|
+
const arr = text.split(/('{2,})/u),
|
|
13
|
+
{length} = arr;
|
|
14
|
+
if (length === 1) {
|
|
15
|
+
return text;
|
|
16
|
+
}
|
|
17
|
+
let nBold = 0,
|
|
18
|
+
nItalic = 0,
|
|
19
|
+
firstSingle, firstMulti, firstSpace;
|
|
20
|
+
for (let i = 1; i < length; i += 2) {
|
|
21
|
+
const {length: len} = arr[i];
|
|
22
|
+
switch (len) {
|
|
23
|
+
case 2:
|
|
24
|
+
nItalic++;
|
|
25
|
+
break;
|
|
26
|
+
case 4:
|
|
27
|
+
arr[i - 1] += `'`;
|
|
28
|
+
arr[i] = `'''`;
|
|
29
|
+
// fall through
|
|
30
|
+
case 3:
|
|
31
|
+
nBold++;
|
|
32
|
+
if (firstSingle) {
|
|
33
|
+
break;
|
|
34
|
+
} else if (arr[i - 1].endsWith(' ')) {
|
|
35
|
+
if (!firstMulti && !firstSpace) {
|
|
36
|
+
firstSpace = i;
|
|
37
|
+
}
|
|
38
|
+
} else if (arr[i - 1].at(-2) === ' ') {
|
|
39
|
+
firstSingle = i;
|
|
40
|
+
} else {
|
|
41
|
+
firstMulti ||= i;
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
44
|
+
default:
|
|
45
|
+
arr[i - 1] += `'`.repeat(len - 5);
|
|
46
|
+
arr[i] = `'''''`;
|
|
47
|
+
nItalic++;
|
|
48
|
+
nBold++;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (nItalic % 2 === 1 && nBold % 2 === 1) {
|
|
52
|
+
const i = firstSingle ?? firstMulti ?? firstSpace;
|
|
53
|
+
arr[i] = `''`;
|
|
54
|
+
arr[i - 1] += `'`;
|
|
55
|
+
}
|
|
56
|
+
for (let i = 1; i < length; i += 2) {
|
|
57
|
+
new QuoteToken(arr[i].length, config, accum);
|
|
58
|
+
arr[i] = `\0${accum.length - 1}q\x7F`;
|
|
59
|
+
}
|
|
60
|
+
return arr.join('');
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
Parser.parsers.parseQuotes = __filename;
|
|
64
|
+
module.exports = parseQuotes;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..');
|
|
4
|
+
|
|
5
|
+
const /** @type {Set<pseudo>} */ simplePseudos = new Set([
|
|
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
|
+
grouping = new Set([',', '>', '+', '~']),
|
|
53
|
+
combinator = new Set(['>', '+', '~', '']);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 清理转义符号
|
|
57
|
+
* @param {string} selector
|
|
58
|
+
*/
|
|
59
|
+
const sanitize = selector => {
|
|
60
|
+
for (const [c, escaped] of specialChars) {
|
|
61
|
+
selector = selector.replaceAll(`\\${c}`, escaped);
|
|
62
|
+
}
|
|
63
|
+
return selector;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 还原转义符号
|
|
68
|
+
* @param {string|undefined} selector
|
|
69
|
+
*/
|
|
70
|
+
const desanitize = selector => {
|
|
71
|
+
if (selector === undefined) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
for (const [c, escaped] of specialChars) {
|
|
75
|
+
selector = selector.replaceAll(escaped, c);
|
|
76
|
+
}
|
|
77
|
+
return selector.trim();
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 去除首尾的引号
|
|
82
|
+
* @param {string|undefined} val 属性值或伪选择器函数的参数
|
|
83
|
+
*/
|
|
84
|
+
const deQuote = val => {
|
|
85
|
+
if (val === undefined) {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
const quotes = /^(["']).*\1$/u.exec(val)?.[1];
|
|
89
|
+
return quotes ? val.slice(1, -1) : val;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 解析简单伪选择器
|
|
94
|
+
* @param {SelectorArray} step 当前顶部
|
|
95
|
+
* @param {string} str 不含属性和复杂伪选择器的语句
|
|
96
|
+
* @throws `SyntaxError` 非法的选择器
|
|
97
|
+
*/
|
|
98
|
+
const pushSimple = (step, str) => {
|
|
99
|
+
const pieces = str.trim().split(':'),
|
|
100
|
+
// eslint-disable-next-line unicorn/explicit-length-check
|
|
101
|
+
i = pieces.slice(1).findIndex(pseudo => simplePseudos.has(pseudo)) + 1 || pieces.length;
|
|
102
|
+
if (pieces.slice(i).some(pseudo => !simplePseudos.has(pseudo))) {
|
|
103
|
+
throw new SyntaxError(`非法的选择器!\n${str}\n可能需要将':'转义为'\\:'。`);
|
|
104
|
+
}
|
|
105
|
+
step.push(desanitize(pieces.slice(0, i).join(':')), ...pieces.slice(i).map(piece => `:${piece}`));
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 解析选择器
|
|
110
|
+
* @param {string} selector
|
|
111
|
+
* @throws `SyntaxError` 非法的选择器
|
|
112
|
+
*/
|
|
113
|
+
const parseSelector = selector => {
|
|
114
|
+
selector = selector.trim();
|
|
115
|
+
const /** @type {SelectorArray[][]} */ stack = [[[]]];
|
|
116
|
+
let sanitized = sanitize(selector),
|
|
117
|
+
regex = regularRegex,
|
|
118
|
+
mt = regex.exec(sanitized),
|
|
119
|
+
[condition] = stack,
|
|
120
|
+
[step] = condition;
|
|
121
|
+
while (mt) {
|
|
122
|
+
let {0: syntax, index} = mt;
|
|
123
|
+
if (syntax.trim() === '') {
|
|
124
|
+
index += syntax.length;
|
|
125
|
+
const char = sanitized[index];
|
|
126
|
+
syntax = grouping.has(char) ? char : '';
|
|
127
|
+
}
|
|
128
|
+
if (syntax === ',') { // 情形1:并列
|
|
129
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
130
|
+
condition = [[]];
|
|
131
|
+
[step] = condition;
|
|
132
|
+
stack.push(condition);
|
|
133
|
+
} else if (combinator.has(syntax)) { // 情形2:关系
|
|
134
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
135
|
+
if (!step.some(Boolean)) {
|
|
136
|
+
throw new SyntaxError(`非法的选择器!\n${selector}\n可能需要通用选择器'*'。`);
|
|
137
|
+
}
|
|
138
|
+
step.relation = syntax;
|
|
139
|
+
step = [];
|
|
140
|
+
condition.push(step);
|
|
141
|
+
} else if (syntax === '[') { // 情形3:属性开启
|
|
142
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
143
|
+
regex = attributeRegex;
|
|
144
|
+
} else if (syntax.endsWith(']')) { // 情形4:属性闭合
|
|
145
|
+
mt[3] = desanitize(deQuote(mt[3]));
|
|
146
|
+
step.push(mt.slice(1));
|
|
147
|
+
regex = regularRegex;
|
|
148
|
+
} else if (syntax === '(') { // 情形5:伪选择器开启
|
|
149
|
+
const pseudoExec = pseudoRegex.exec(sanitized.slice(0, index));
|
|
150
|
+
if (!pseudoExec) {
|
|
151
|
+
throw new SyntaxError(`非法的选择器!\n${desanitize(sanitized)}\n请检查伪选择器是否存在。`);
|
|
152
|
+
}
|
|
153
|
+
pushSimple(step, sanitized.slice(0, pseudoExec.index));
|
|
154
|
+
step.push(pseudoExec[1]); // 临时存放复杂伪选择器
|
|
155
|
+
regex = functionRegex;
|
|
156
|
+
} else { // 情形6:伪选择器闭合
|
|
157
|
+
const /** @type {pseudo} */ pseudo = step.pop();
|
|
158
|
+
mt.push(pseudo);
|
|
159
|
+
mt[1] = deQuote(mt[1]);
|
|
160
|
+
step.push(mt.slice(1));
|
|
161
|
+
regex = regularRegex;
|
|
162
|
+
}
|
|
163
|
+
sanitized = sanitized.slice(index + syntax.length);
|
|
164
|
+
if (grouping.has(syntax)) {
|
|
165
|
+
sanitized = sanitized.trim();
|
|
166
|
+
}
|
|
167
|
+
mt = regex.exec(sanitized);
|
|
168
|
+
}
|
|
169
|
+
if (regex === regularRegex) {
|
|
170
|
+
pushSimple(step, sanitized);
|
|
171
|
+
return stack;
|
|
172
|
+
}
|
|
173
|
+
throw new SyntaxError(`非法的选择器!\n${selector}\n检测到未闭合的'${regex === attributeRegex ? '[' : '('}'`);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
Parser.parsers.parseSelector = __filename;
|
|
177
|
+
module.exports = parseSelector;
|
package/parser/table.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
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');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 解析表格,注意`tr`和`td`包含开头的换行
|
|
13
|
+
* @param {Token & {firstChild: AstText}} root 根节点
|
|
14
|
+
* @param {accum} accum
|
|
15
|
+
*/
|
|
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 === 'parameter-value' || type === 'ext-inner' && name === 'poem'
|
|
20
|
+
? ''
|
|
21
|
+
: `\n${lines.shift()}`;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 向表格中插入纯文本
|
|
25
|
+
* @param {string} str 待插入的文本
|
|
26
|
+
* @param {TrToken} top 当前解析的表格或表格行
|
|
27
|
+
*/
|
|
28
|
+
const push = (str, top) => {
|
|
29
|
+
if (!top) {
|
|
30
|
+
out += str;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const /** @type {Token}} */ {lastChild} = top;
|
|
34
|
+
if (lastChild.constructor === Token) {
|
|
35
|
+
lastChild.setText(String(lastChild) + str);
|
|
36
|
+
} else {
|
|
37
|
+
const token = new Token(str, config, true, accum);
|
|
38
|
+
token.type = 'table-inter';
|
|
39
|
+
top.insertAt(token.setAttribute('stage', 3));
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
for (const outLine of lines) {
|
|
43
|
+
let top = stack.pop();
|
|
44
|
+
const [spaces] = /^(?:\s|\0\d+c\x7F)*/u.exec(outLine),
|
|
45
|
+
line = outLine.slice(spaces.length),
|
|
46
|
+
matchesStart = /^(:*)((?:\s|\0\d+c\x7F)*)(\{\||\{(?:\0\d+c\x7F)*\0\d+!\x7F|\0\d+\{\x7F)(.*)$/u.exec(line);
|
|
47
|
+
if (matchesStart) {
|
|
48
|
+
while (top && top.type !== 'td') {
|
|
49
|
+
top = stack.pop();
|
|
50
|
+
}
|
|
51
|
+
const [, indent, moreSpaces, tableSyntax, attr] = matchesStart;
|
|
52
|
+
if (indent) {
|
|
53
|
+
new DdToken(indent, config, accum);
|
|
54
|
+
}
|
|
55
|
+
push(`\n${spaces}${indent && `\0${accum.length - 1}d\x7F`}${moreSpaces}\0${accum.length}b\x7F`, top);
|
|
56
|
+
const table = new TableToken(tableSyntax, attr, config, accum);
|
|
57
|
+
stack.push(...top ? [top] : [], table);
|
|
58
|
+
continue;
|
|
59
|
+
} else if (!top) {
|
|
60
|
+
out += `\n${outLine}`;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const matches = // eslint-disable-line operator-linebreak
|
|
64
|
+
/^(?:(\|\}|\0\d+!\x7F\}|\0\d+\}\x7F)|(\|-+|\0\d+!\x7F-+|\0\d+-\x7F-*)(?!-)|(!|(?:\||\0\d+!\x7F)\+?))(.*)$/u
|
|
65
|
+
.exec(line);
|
|
66
|
+
if (!matches) {
|
|
67
|
+
push(`\n${outLine}`, top);
|
|
68
|
+
stack.push(...top ? [top] : []);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const [, closing, row, cell, attr] = matches;
|
|
72
|
+
if (closing) {
|
|
73
|
+
while (!(top instanceof TableToken)) {
|
|
74
|
+
top = stack.pop();
|
|
75
|
+
}
|
|
76
|
+
top.close(`\n${spaces}${closing}`, true);
|
|
77
|
+
push(attr, stack.at(-1));
|
|
78
|
+
} else if (row) {
|
|
79
|
+
if (top.type === 'td') {
|
|
80
|
+
top = stack.pop();
|
|
81
|
+
}
|
|
82
|
+
if (top.type === 'tr') {
|
|
83
|
+
top = stack.pop();
|
|
84
|
+
}
|
|
85
|
+
const tr = new TrToken(`\n${spaces}${row}`, attr, config, accum);
|
|
86
|
+
stack.push(top, tr);
|
|
87
|
+
top.insertAt(tr);
|
|
88
|
+
} else {
|
|
89
|
+
if (top.type === 'td') {
|
|
90
|
+
top = stack.pop();
|
|
91
|
+
}
|
|
92
|
+
const regex = cell === '!'
|
|
93
|
+
? /!!|(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu
|
|
94
|
+
: /(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu;
|
|
95
|
+
let mt = regex.exec(attr),
|
|
96
|
+
lastIndex = 0,
|
|
97
|
+
lastSyntax = `\n${spaces}${cell}`;
|
|
98
|
+
while (mt) {
|
|
99
|
+
const td = new TdToken(lastSyntax, attr.slice(lastIndex, mt.index), config, accum);
|
|
100
|
+
top.insertAt(td);
|
|
101
|
+
({lastIndex} = regex);
|
|
102
|
+
[lastSyntax] = mt;
|
|
103
|
+
mt = regex.exec(attr);
|
|
104
|
+
}
|
|
105
|
+
const td = new TdToken(lastSyntax, attr.slice(lastIndex), config, accum);
|
|
106
|
+
stack.push(top, td);
|
|
107
|
+
top.insertAt(td);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return out.slice(1);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
Parser.parsers.parseTable = __filename;
|
|
114
|
+
module.exports = parseTable;
|
package/src/arg.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {text, noWrap} = require('../util/string'),
|
|
4
|
+
{generateForSelf, generateForChild} = require('../util/lint'),
|
|
5
|
+
Parser = require('..'),
|
|
6
|
+
Token = require('.');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `{{{}}}`包裹的参数
|
|
10
|
+
* @classdesc `{childNodes: [AtomToken, ?Token, ...HiddenToken]}`
|
|
11
|
+
*/
|
|
12
|
+
class ArgToken extends Token {
|
|
13
|
+
type = 'arg';
|
|
14
|
+
|
|
15
|
+
/** default */
|
|
16
|
+
get default() {
|
|
17
|
+
return this.childNodes[1]?.text() ?? false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {string[]} parts 以'|'分隔的各部分
|
|
22
|
+
* @param {accum} accum
|
|
23
|
+
* @complexity `n`
|
|
24
|
+
*/
|
|
25
|
+
constructor(parts, config = Parser.getConfig(), accum = []) {
|
|
26
|
+
super(undefined, config, true, accum, {
|
|
27
|
+
AtomToken: 0, Token: 1, HiddenToken: '2:',
|
|
28
|
+
});
|
|
29
|
+
for (let i = 0; i < parts.length; i++) {
|
|
30
|
+
if (i === 0 || i > 1) {
|
|
31
|
+
const AtomToken = i === 0 ? require('./atom') : require('./atom/hidden');
|
|
32
|
+
const token = new AtomToken(parts[i], i === 0 ? 'arg-name' : undefined, config, accum, {
|
|
33
|
+
'Stage-2': ':', '!HeadingToken': '',
|
|
34
|
+
});
|
|
35
|
+
super.insertAt(token);
|
|
36
|
+
} else {
|
|
37
|
+
const token = new Token(parts[i], config, true, accum);
|
|
38
|
+
token.type = 'arg-default';
|
|
39
|
+
super.insertAt(token.setAttribute('stage', 2));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
this.getAttribute('protectChildren')(0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @override
|
|
47
|
+
* @param {string} selector
|
|
48
|
+
*/
|
|
49
|
+
toString(selector) {
|
|
50
|
+
return selector && this.matches(selector) ? '' : `{{{${super.toString(selector, '|')}}}}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @override
|
|
55
|
+
* @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
text() {
|
|
58
|
+
return `{{{${text(this.childNodes.slice(0, 2), '|')}}}}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @override */
|
|
62
|
+
getPadding() {
|
|
63
|
+
return 3;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** @override */
|
|
67
|
+
getGaps() {
|
|
68
|
+
return 1;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @override */
|
|
72
|
+
print() {
|
|
73
|
+
return super.print({pre: '{{{', post: '}}}', sep: '|'});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @override
|
|
78
|
+
* @param {number} start 起始位置
|
|
79
|
+
* @returns {LintError[]}
|
|
80
|
+
*/
|
|
81
|
+
lint(start = this.getAbsoluteIndex()) {
|
|
82
|
+
if (!this.getAttribute('include')) {
|
|
83
|
+
return [generateForSelf(this, {start}, 'unexpected template argument')];
|
|
84
|
+
}
|
|
85
|
+
const {childNodes: [argName, argDefault, ...rest]} = this,
|
|
86
|
+
errors = argName.lint(start + 3);
|
|
87
|
+
if (argDefault) {
|
|
88
|
+
errors.push(...argDefault.lint(start + 4 + String(argName).length));
|
|
89
|
+
}
|
|
90
|
+
if (rest.length > 0) {
|
|
91
|
+
const rect = {start, ...this.getRootNode().posFromIndex(start)};
|
|
92
|
+
errors.push(...rest.map(child => {
|
|
93
|
+
const error = generateForChild(child, rect, 'invisible content inside triple brackets'),
|
|
94
|
+
{startIndex, startCol, excerpt} = error;
|
|
95
|
+
return {...error, startIndex: startIndex - 1, startCol: startCol - 1, excerpt: `|${excerpt}`};
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
return errors;
|
|
99
|
+
}
|
|
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
|
+
}
|
|
205
|
+
|
|
206
|
+
Parser.classes.ArgToken = __filename;
|
|
207
|
+
module.exports = ArgToken;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const hidden = require('../../mixin/hidden'),
|
|
4
|
+
Parser = require('../..'),
|
|
5
|
+
AtomToken = require('.');
|
|
6
|
+
|
|
7
|
+
/** 不可见的节点 */
|
|
8
|
+
class HiddenToken extends hidden(AtomToken) {
|
|
9
|
+
type = 'hidden';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
Parser.classes.HiddenToken = __filename;
|
|
13
|
+
module.exports = HiddenToken;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('../..'),
|
|
4
|
+
Token = require('..');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 不会被继续解析的plain Token
|
|
8
|
+
* @classdesc `{childNodes: ...AstText|Token}`
|
|
9
|
+
*/
|
|
10
|
+
class AtomToken extends Token {
|
|
11
|
+
type = 'plain';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} wikitext wikitext
|
|
15
|
+
* @param {string|undefined} type Token.type
|
|
16
|
+
* @param {accum} accum
|
|
17
|
+
* @param {acceptable} acceptable 可接受的子节点设置
|
|
18
|
+
*/
|
|
19
|
+
constructor(wikitext, type, config = Parser.getConfig(), accum = [], acceptable = undefined) {
|
|
20
|
+
super(wikitext, config, true, accum, acceptable);
|
|
21
|
+
if (type) {
|
|
22
|
+
this.type = type;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
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
|
+
}
|
|
41
|
+
|
|
42
|
+
Parser.classes.AtomToken = __filename;
|
|
43
|
+
module.exports = AtomToken;
|