wikiparser-node 0.11.0 → 1.0.0-b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -9
- package/bundle/bundle.min.js +37 -0
- package/config/llwiki.json +1 -35
- package/config/minimum.json +136 -0
- package/config/moegirl.json +1 -44
- package/config/zhwiki.json +1 -466
- package/extensions/dist/base.js +105 -0
- package/extensions/dist/editor.js +172 -0
- package/extensions/dist/highlight.js +46 -0
- package/extensions/dist/lint.js +43 -0
- package/extensions/editor.css +63 -0
- package/extensions/ui.css +119 -0
- package/i18n/zh-hans.json +1 -0
- package/i18n/zh-hant.json +1 -0
- package/package.json +38 -31
- package/config/.schema.json +0 -134
- package/dist/index.d.ts +0 -114
- package/dist/lib/element.d.ts +0 -162
- package/dist/lib/node.d.ts +0 -291
- package/dist/lib/ranges.d.ts +0 -37
- package/dist/lib/text.d.ts +0 -64
- package/dist/lib/title.d.ts +0 -21
- package/dist/mixin/attributeParent.d.ts +0 -9
- package/dist/mixin/fixedToken.d.ts +0 -8
- package/dist/mixin/hidden.d.ts +0 -8
- package/dist/mixin/singleLine.d.ts +0 -8
- package/dist/mixin/sol.d.ts +0 -8
- package/dist/parser/brackets.d.ts +0 -12
- package/dist/parser/commentAndExt.d.ts +0 -8
- package/dist/parser/converter.d.ts +0 -7
- package/dist/parser/externalLinks.d.ts +0 -7
- package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
- package/dist/parser/html.d.ts +0 -7
- package/dist/parser/links.d.ts +0 -7
- package/dist/parser/list.d.ts +0 -7
- package/dist/parser/magicLinks.d.ts +0 -7
- package/dist/parser/quotes.d.ts +0 -7
- package/dist/parser/selector.d.ts +0 -12
- package/dist/parser/table.d.ts +0 -11
- package/dist/src/arg.d.ts +0 -54
- package/dist/src/atom/hidden.d.ts +0 -5
- package/dist/src/atom/index.d.ts +0 -15
- package/dist/src/attribute.d.ts +0 -65
- package/dist/src/attributes.d.ts +0 -112
- package/dist/src/charinsert.d.ts +0 -32
- package/dist/src/converter.d.ts +0 -103
- package/dist/src/converterFlags.d.ts +0 -83
- package/dist/src/converterRule.d.ts +0 -75
- package/dist/src/extLink.d.ts +0 -62
- package/dist/src/gallery.d.ts +0 -33
- package/dist/src/hasNowiki/index.d.ts +0 -14
- package/dist/src/hasNowiki/pre.d.ts +0 -13
- package/dist/src/heading.d.ts +0 -44
- package/dist/src/html.d.ts +0 -56
- package/dist/src/imageParameter.d.ts +0 -65
- package/dist/src/imagemap.d.ts +0 -37
- package/dist/src/imagemapLink.d.ts +0 -21
- package/dist/src/index.d.ts +0 -186
- package/dist/src/link/category.d.ts +0 -16
- package/dist/src/link/file.d.ts +0 -85
- package/dist/src/link/galleryImage.d.ts +0 -15
- package/dist/src/link/index.d.ts +0 -88
- package/dist/src/magicLink.d.ts +0 -36
- package/dist/src/nested/choose.d.ts +0 -13
- package/dist/src/nested/combobox.d.ts +0 -13
- package/dist/src/nested/index.d.ts +0 -18
- package/dist/src/nested/references.d.ts +0 -13
- package/dist/src/nowiki/comment.d.ts +0 -31
- package/dist/src/nowiki/dd.d.ts +0 -17
- package/dist/src/nowiki/doubleUnderscore.d.ts +0 -22
- package/dist/src/nowiki/hr.d.ts +0 -13
- package/dist/src/nowiki/index.d.ts +0 -27
- package/dist/src/nowiki/list.d.ts +0 -8
- package/dist/src/nowiki/noinclude.d.ts +0 -8
- package/dist/src/nowiki/quote.d.ts +0 -13
- package/dist/src/onlyinclude.d.ts +0 -24
- package/dist/src/paramTag/index.d.ts +0 -29
- package/dist/src/paramTag/inputbox.d.ts +0 -8
- package/dist/src/parameter.d.ts +0 -75
- package/dist/src/syntax.d.ts +0 -20
- package/dist/src/table/index.d.ts +0 -273
- package/dist/src/table/td.d.ts +0 -100
- package/dist/src/table/tr.d.ts +0 -91
- package/dist/src/tagPair/ext.d.ts +0 -18
- package/dist/src/tagPair/include.d.ts +0 -25
- package/dist/src/tagPair/index.d.ts +0 -41
- package/dist/src/transclude.d.ts +0 -199
- package/dist/tool/index.d.ts +0 -420
- package/dist/util/base.d.ts +0 -10
- package/dist/util/debug.d.ts +0 -20
- package/dist/util/diff.d.ts +0 -8
- package/dist/util/lint.d.ts +0 -28
- package/dist/util/string.d.ts +0 -55
- package/index.js +0 -333
- package/lib/element.js +0 -618
- package/lib/node.js +0 -730
- package/lib/ranges.js +0 -130
- package/lib/text.js +0 -265
- package/lib/title.js +0 -83
- package/mixin/attributeParent.js +0 -117
- package/mixin/fixedToken.js +0 -40
- package/mixin/hidden.js +0 -21
- package/mixin/singleLine.js +0 -31
- package/mixin/sol.js +0 -54
- package/parser/brackets.js +0 -128
- package/parser/commentAndExt.js +0 -62
- package/parser/converter.js +0 -46
- package/parser/externalLinks.js +0 -33
- package/parser/hrAndDoubleUnderscore.js +0 -49
- package/parser/html.js +0 -42
- package/parser/links.js +0 -94
- package/parser/list.js +0 -59
- package/parser/magicLinks.js +0 -41
- package/parser/quotes.js +0 -64
- package/parser/selector.js +0 -180
- package/parser/table.js +0 -114
- package/src/arg.js +0 -207
- package/src/atom/hidden.js +0 -13
- package/src/atom/index.js +0 -43
- package/src/attribute.js +0 -472
- package/src/attributes.js +0 -453
- package/src/charinsert.js +0 -97
- package/src/converter.js +0 -176
- package/src/converterFlags.js +0 -284
- package/src/converterRule.js +0 -256
- package/src/extLink.js +0 -180
- package/src/gallery.js +0 -149
- package/src/hasNowiki/index.js +0 -44
- package/src/hasNowiki/pre.js +0 -40
- package/src/heading.js +0 -134
- package/src/html.js +0 -254
- package/src/imageParameter.js +0 -303
- package/src/imagemap.js +0 -199
- package/src/imagemapLink.js +0 -41
- package/src/index.js +0 -938
- package/src/link/category.js +0 -44
- package/src/link/file.js +0 -287
- package/src/link/galleryImage.js +0 -120
- package/src/link/index.js +0 -388
- package/src/magicLink.js +0 -151
- package/src/nested/choose.js +0 -24
- package/src/nested/combobox.js +0 -23
- package/src/nested/index.js +0 -96
- package/src/nested/references.js +0 -23
- package/src/nowiki/comment.js +0 -71
- package/src/nowiki/dd.js +0 -59
- package/src/nowiki/doubleUnderscore.js +0 -56
- package/src/nowiki/hr.js +0 -41
- package/src/nowiki/index.js +0 -56
- package/src/nowiki/list.js +0 -16
- package/src/nowiki/noinclude.js +0 -28
- package/src/nowiki/quote.js +0 -69
- package/src/onlyinclude.js +0 -64
- package/src/paramTag/index.js +0 -89
- package/src/paramTag/inputbox.js +0 -35
- package/src/parameter.js +0 -239
- package/src/syntax.js +0 -91
- package/src/table/index.js +0 -985
- package/src/table/td.js +0 -343
- package/src/table/tr.js +0 -319
- package/src/tagPair/ext.js +0 -146
- package/src/tagPair/include.js +0 -50
- package/src/tagPair/index.js +0 -131
- package/src/transclude.js +0 -843
- package/tool/index.js +0 -1209
- package/typings/api.d.ts +0 -9
- package/typings/array.d.ts +0 -29
- package/typings/event.d.ts +0 -22
- package/typings/index.d.ts +0 -118
- package/typings/node.d.ts +0 -35
- package/typings/parser.d.ts +0 -12
- package/typings/table.d.ts +0 -10
- package/typings/token.d.ts +0 -31
- package/typings/tool.d.ts +0 -6
- package/util/base.js +0 -17
- package/util/debug.js +0 -73
- package/util/diff.js +0 -76
- package/util/lint.js +0 -57
- package/util/string.js +0 -126
package/parser/selector.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../typings/parser').SelectorArray} SelectorArray */
|
|
4
|
-
/** @typedef {import('../typings/parser').pseudo} pseudo */
|
|
5
|
-
|
|
6
|
-
const Parser = require('..');
|
|
7
|
-
|
|
8
|
-
const /** @type {Set<pseudo>} */ simplePseudos = new Set([
|
|
9
|
-
'root',
|
|
10
|
-
'first-child',
|
|
11
|
-
'first-of-type',
|
|
12
|
-
'last-child',
|
|
13
|
-
'last-of-type',
|
|
14
|
-
'only-child',
|
|
15
|
-
'only-of-type',
|
|
16
|
-
'empty',
|
|
17
|
-
'parent',
|
|
18
|
-
'header',
|
|
19
|
-
'hidden',
|
|
20
|
-
'visible',
|
|
21
|
-
'only-whitespace',
|
|
22
|
-
'local-link',
|
|
23
|
-
'read-only',
|
|
24
|
-
'read-write',
|
|
25
|
-
'invalid',
|
|
26
|
-
'required',
|
|
27
|
-
'optional',
|
|
28
|
-
]),
|
|
29
|
-
/** @type {pseudo[]} */ complexPseudos = [
|
|
30
|
-
'is',
|
|
31
|
-
'not',
|
|
32
|
-
'nth-child',
|
|
33
|
-
'nth-of-type',
|
|
34
|
-
'nth-last-child',
|
|
35
|
-
'nth-last-of-type',
|
|
36
|
-
'contains',
|
|
37
|
-
'has',
|
|
38
|
-
'lang',
|
|
39
|
-
],
|
|
40
|
-
specialChars = [
|
|
41
|
-
['[', '['],
|
|
42
|
-
[']', ']'],
|
|
43
|
-
['(', '('],
|
|
44
|
-
[')', ')'],
|
|
45
|
-
['"', '"'],
|
|
46
|
-
[`'`, '''],
|
|
47
|
-
[':', ':'],
|
|
48
|
-
['\\', '\'],
|
|
49
|
-
['&', '&'],
|
|
50
|
-
],
|
|
51
|
-
pseudoRegex = new RegExp(`:(${complexPseudos.join('|')})$`, 'u'),
|
|
52
|
-
regularRegex = /[[(,>+~]|\s+/u,
|
|
53
|
-
attributeRegex = /^\s*(\w+)\s*(?:([~|^$*!]?=)\s*("[^"]*"|'[^']*'|[^\s[\]]+)(?:\s+(i))?\s*)?\]/u,
|
|
54
|
-
functionRegex = /^(\s*"[^"]*"\s*|\s*'[^']*'\s*|[^()]*)\)/u,
|
|
55
|
-
grouping = new Set([',', '>', '+', '~']),
|
|
56
|
-
combinator = new Set(['>', '+', '~', '']);
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 清理转义符号
|
|
60
|
-
* @param {string} selector
|
|
61
|
-
*/
|
|
62
|
-
const sanitize = selector => {
|
|
63
|
-
for (const [c, escaped] of specialChars) {
|
|
64
|
-
selector = selector.replaceAll(`\\${c}`, escaped);
|
|
65
|
-
}
|
|
66
|
-
return selector;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* 还原转义符号
|
|
71
|
-
* @param {string|undefined} selector
|
|
72
|
-
*/
|
|
73
|
-
const desanitize = selector => {
|
|
74
|
-
if (selector === undefined) {
|
|
75
|
-
return undefined;
|
|
76
|
-
}
|
|
77
|
-
for (const [c, escaped] of specialChars) {
|
|
78
|
-
selector = selector.replaceAll(escaped, c);
|
|
79
|
-
}
|
|
80
|
-
return selector.trim();
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* 去除首尾的引号
|
|
85
|
-
* @param {string|undefined} val 属性值或伪选择器函数的参数
|
|
86
|
-
*/
|
|
87
|
-
const deQuote = val => {
|
|
88
|
-
if (val === undefined) {
|
|
89
|
-
return undefined;
|
|
90
|
-
}
|
|
91
|
-
const quotes = /^(["']).*\1$/u.exec(val)?.[1];
|
|
92
|
-
return quotes ? val.slice(1, -1) : val;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* 解析简单伪选择器
|
|
97
|
-
* @param {SelectorArray} step 当前顶部
|
|
98
|
-
* @param {string} str 不含属性和复杂伪选择器的语句
|
|
99
|
-
* @throws `SyntaxError` 非法的选择器
|
|
100
|
-
*/
|
|
101
|
-
const pushSimple = (step, str) => {
|
|
102
|
-
const pieces = str.trim().split(':'),
|
|
103
|
-
// eslint-disable-next-line unicorn/explicit-length-check
|
|
104
|
-
i = pieces.slice(1).findIndex(pseudo => simplePseudos.has(pseudo)) + 1 || pieces.length;
|
|
105
|
-
if (pieces.slice(i).some(pseudo => !simplePseudos.has(pseudo))) {
|
|
106
|
-
throw new SyntaxError(`非法的选择器!\n${str}\n可能需要将':'转义为'\\:'。`);
|
|
107
|
-
}
|
|
108
|
-
step.push(desanitize(pieces.slice(0, i).join(':')), ...pieces.slice(i).map(piece => `:${piece}`));
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 解析选择器
|
|
113
|
-
* @param {string} selector
|
|
114
|
-
* @throws `SyntaxError` 非法的选择器
|
|
115
|
-
*/
|
|
116
|
-
const parseSelector = selector => {
|
|
117
|
-
selector = selector.trim();
|
|
118
|
-
const /** @type {SelectorArray[][]} */ stack = [[[]]];
|
|
119
|
-
let sanitized = sanitize(selector),
|
|
120
|
-
regex = regularRegex,
|
|
121
|
-
mt = regex.exec(sanitized),
|
|
122
|
-
[condition] = stack,
|
|
123
|
-
[step] = condition;
|
|
124
|
-
while (mt) {
|
|
125
|
-
let {0: syntax, index} = mt;
|
|
126
|
-
if (syntax.trim() === '') {
|
|
127
|
-
index += syntax.length;
|
|
128
|
-
const char = sanitized[index];
|
|
129
|
-
syntax = grouping.has(char) ? char : '';
|
|
130
|
-
}
|
|
131
|
-
if (syntax === ',') { // 情形1:并列
|
|
132
|
-
pushSimple(step, sanitized.slice(0, index));
|
|
133
|
-
condition = [[]];
|
|
134
|
-
[step] = condition;
|
|
135
|
-
stack.push(condition);
|
|
136
|
-
} else if (combinator.has(syntax)) { // 情形2:关系
|
|
137
|
-
pushSimple(step, sanitized.slice(0, index));
|
|
138
|
-
if (!step.some(Boolean)) {
|
|
139
|
-
throw new SyntaxError(`非法的选择器!\n${selector}\n可能需要通用选择器'*'。`);
|
|
140
|
-
}
|
|
141
|
-
step.relation = syntax;
|
|
142
|
-
step = [];
|
|
143
|
-
condition.push(step);
|
|
144
|
-
} else if (syntax === '[') { // 情形3:属性开启
|
|
145
|
-
pushSimple(step, sanitized.slice(0, index));
|
|
146
|
-
regex = attributeRegex;
|
|
147
|
-
} else if (syntax.endsWith(']')) { // 情形4:属性闭合
|
|
148
|
-
mt[3] = desanitize(deQuote(mt[3]));
|
|
149
|
-
step.push(mt.slice(1));
|
|
150
|
-
regex = regularRegex;
|
|
151
|
-
} else if (syntax === '(') { // 情形5:伪选择器开启
|
|
152
|
-
const pseudoExec = pseudoRegex.exec(sanitized.slice(0, index));
|
|
153
|
-
if (!pseudoExec) {
|
|
154
|
-
throw new SyntaxError(`非法的选择器!\n${desanitize(sanitized)}\n请检查伪选择器是否存在。`);
|
|
155
|
-
}
|
|
156
|
-
pushSimple(step, sanitized.slice(0, pseudoExec.index));
|
|
157
|
-
step.push(pseudoExec[1]); // 临时存放复杂伪选择器
|
|
158
|
-
regex = functionRegex;
|
|
159
|
-
} else { // 情形6:伪选择器闭合
|
|
160
|
-
const /** @type {pseudo} */ pseudo = step.pop();
|
|
161
|
-
mt.push(pseudo);
|
|
162
|
-
mt[1] = deQuote(mt[1]);
|
|
163
|
-
step.push(mt.slice(1));
|
|
164
|
-
regex = regularRegex;
|
|
165
|
-
}
|
|
166
|
-
sanitized = sanitized.slice(index + syntax.length);
|
|
167
|
-
if (grouping.has(syntax)) {
|
|
168
|
-
sanitized = sanitized.trim();
|
|
169
|
-
}
|
|
170
|
-
mt = regex.exec(sanitized);
|
|
171
|
-
}
|
|
172
|
-
if (regex === regularRegex) {
|
|
173
|
-
pushSimple(step, sanitized);
|
|
174
|
-
return stack;
|
|
175
|
-
}
|
|
176
|
-
throw new SyntaxError(`非法的选择器!\n${selector}\n检测到未闭合的'${regex === attributeRegex ? '[' : '('}'`);
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
Parser.parsers.parseSelector = __filename;
|
|
180
|
-
module.exports = parseSelector;
|
package/parser/table.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
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 {import('../typings/token').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
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
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 {import('../typings/token').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 {import('../typings/token').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 {import('../typings/event').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();
|
|
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();
|
|
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;
|
package/src/atom/hidden.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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;
|
package/src/atom/index.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
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 {import('../../typings/token').accum} accum
|
|
17
|
-
* @param {import('../../typings/token').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;
|