wikiparser-node 0.5.0 → 0.6.2-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/bundle/bundle.min.js +34 -0
- package/package.json +10 -13
- package/README.md +0 -39
- package/config/default.json +0 -769
- package/config/llwiki.json +0 -630
- package/config/moegirl.json +0 -727
- package/config/zhwiki.json +0 -1269
- package/index.js +0 -268
- package/lib/element.js +0 -639
- package/lib/node.js +0 -743
- package/lib/ranges.js +0 -130
- package/lib/text.js +0 -175
- package/lib/title.js +0 -69
- package/mixin/attributeParent.js +0 -113
- package/mixin/fixedToken.js +0 -40
- package/mixin/hidden.js +0 -19
- package/mixin/sol.js +0 -69
- package/parser/brackets.js +0 -112
- package/parser/commentAndExt.js +0 -63
- package/parser/converter.js +0 -45
- package/parser/externalLinks.js +0 -31
- package/parser/hrAndDoubleUnderscore.js +0 -36
- package/parser/html.js +0 -42
- package/parser/links.js +0 -97
- package/parser/list.js +0 -59
- package/parser/magicLinks.js +0 -41
- package/parser/quotes.js +0 -64
- package/parser/selector.js +0 -175
- package/parser/table.js +0 -112
- package/src/arg.js +0 -193
- package/src/atom/hidden.js +0 -13
- package/src/atom/index.js +0 -41
- package/src/attribute.js +0 -449
- package/src/converter.js +0 -165
- package/src/converterFlags.js +0 -269
- package/src/converterRule.js +0 -259
- package/src/extLink.js +0 -173
- package/src/gallery.js +0 -122
- package/src/heading.js +0 -119
- package/src/html.js +0 -230
- package/src/imageParameter.js +0 -260
- package/src/index.js +0 -744
- package/src/link/category.js +0 -53
- package/src/link/file.js +0 -285
- package/src/link/galleryImage.js +0 -61
- package/src/link/index.js +0 -325
- package/src/magicLink.js +0 -136
- 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 -44
- package/src/nowiki/list.js +0 -16
- package/src/nowiki/noinclude.js +0 -28
- package/src/nowiki/quote.js +0 -63
- package/src/onlyinclude.js +0 -59
- package/src/parameter.js +0 -200
- package/src/syntax.js +0 -83
- package/src/table/index.js +0 -980
- package/src/table/td.js +0 -314
- package/src/table/tr.js +0 -299
- package/src/tagPair/ext.js +0 -104
- package/src/tagPair/include.js +0 -60
- package/src/tagPair/index.js +0 -125
- package/src/transclude.js +0 -736
- package/tool/index.js +0 -918
- package/util/debug.js +0 -73
- package/util/diff.js +0 -76
- package/util/lint.js +0 -40
- package/util/string.js +0 -105
package/src/table/td.js
DELETED
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fixedToken = require('../../mixin/fixedToken'),
|
|
4
|
-
{externalUse, typeError} = require('../../util/debug'),
|
|
5
|
-
Parser = require('../..'),
|
|
6
|
-
Token = require('..'),
|
|
7
|
-
TrToken = require('./tr');
|
|
8
|
-
|
|
9
|
-
const aliases = {td: '\n|', th: '\n!', caption: '\n|+'},
|
|
10
|
-
openingPattern = /^(?:\n[\S\n]*(?:[|!]|\|\+|\{\{\s*!\s*\}\}\+?)|(?:\||\{\{\s*!\s*\}\}){2}|!!|\{\{\s*!!\s*\}\})$/u;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* `<td>`、`<th>`和`<caption>`
|
|
14
|
-
* @classdesc `{childNodes: [SyntaxToken, AttributeToken, Token]}`
|
|
15
|
-
*/
|
|
16
|
-
class TdToken extends fixedToken(TrToken) {
|
|
17
|
-
type = 'td';
|
|
18
|
-
#innerSyntax = '';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 单元格类型
|
|
22
|
-
* @complexity `n`
|
|
23
|
-
*/
|
|
24
|
-
get subtype() {
|
|
25
|
-
return this.getSyntax().subtype;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
set subtype(subtype) {
|
|
29
|
-
this.setSyntax(subtype);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** rowspan */
|
|
33
|
-
get rowspan() {
|
|
34
|
-
return this.getAttr('rowspan');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
set rowspan(rowspan) {
|
|
38
|
-
this.setAttr('rowspan', rowspan);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** colspan */
|
|
42
|
-
get colspan() {
|
|
43
|
-
return this.getAttr('colspan');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
set colspan(colspan) {
|
|
47
|
-
this.setAttr('colspan', colspan);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** 内部wikitext */
|
|
51
|
-
get innerText() {
|
|
52
|
-
return this.lastChild.text();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** 是否位于行首 */
|
|
56
|
-
isIndependent() {
|
|
57
|
-
return this.firstChild.text()[0] === '\n';
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 获取单元格语法信息
|
|
62
|
-
* @returns {{subtype: 'td'|'th'|'caption', escape: boolean, correction: boolean}}
|
|
63
|
-
* @complexity `n`
|
|
64
|
-
*/
|
|
65
|
-
getSyntax() {
|
|
66
|
-
const syntax = this.firstChild.text(),
|
|
67
|
-
esc = syntax.includes('{{'),
|
|
68
|
-
char = syntax.at(-1);
|
|
69
|
-
let subtype = 'td';
|
|
70
|
-
if (char === '!') {
|
|
71
|
-
subtype = 'th';
|
|
72
|
-
} else if (char === '+') {
|
|
73
|
-
subtype = 'caption';
|
|
74
|
-
}
|
|
75
|
-
if (this.isIndependent()) {
|
|
76
|
-
return {subtype, escape: esc, correction: false};
|
|
77
|
-
}
|
|
78
|
-
const {previousSibling} = this;
|
|
79
|
-
if (previousSibling?.type !== 'td') {
|
|
80
|
-
return {subtype, escape: esc, correction: true};
|
|
81
|
-
}
|
|
82
|
-
const result = previousSibling.getSyntax();
|
|
83
|
-
result.escape ||= esc;
|
|
84
|
-
result.correction = previousSibling.lastChild
|
|
85
|
-
.toString('comment, ext, include, noinclude, arg, template, magic-word, html')
|
|
86
|
-
.includes('\n');
|
|
87
|
-
if (subtype === 'th' && result.subtype !== 'th') {
|
|
88
|
-
result.subtype = 'th';
|
|
89
|
-
result.correction = true;
|
|
90
|
-
}
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
getRowCount = undefined;
|
|
95
|
-
getNthCol = undefined;
|
|
96
|
-
insertTableCell = undefined;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* @param {string} syntax 单元格语法
|
|
100
|
-
* @param {string} inner 内部wikitext
|
|
101
|
-
* @param {accum} accum
|
|
102
|
-
*/
|
|
103
|
-
constructor(syntax, inner, config = Parser.getConfig(), accum = []) {
|
|
104
|
-
let innerSyntax = inner?.match(/\||\0\d+!\x7F/u),
|
|
105
|
-
attr = innerSyntax ? inner.slice(0, innerSyntax.index) : '';
|
|
106
|
-
if (/\[\[|-\{/u.test(attr)) {
|
|
107
|
-
innerSyntax = null;
|
|
108
|
-
attr = '';
|
|
109
|
-
}
|
|
110
|
-
super(syntax, attr, config, accum, openingPattern);
|
|
111
|
-
if (innerSyntax) {
|
|
112
|
-
[this.#innerSyntax] = innerSyntax;
|
|
113
|
-
}
|
|
114
|
-
// eslint-disable-next-line no-unsafe-optional-chaining
|
|
115
|
-
const innerToken = new Token(inner?.slice(innerSyntax?.index + this.#innerSyntax.length), config, true, accum);
|
|
116
|
-
innerToken.type = 'td-inner';
|
|
117
|
-
this.setAttribute('acceptable', {SyntaxToken: 0, AttributeToken: 1, Token: 2})
|
|
118
|
-
.seal(['getRowCount', 'getNthCol', 'insertTableCell'], true)
|
|
119
|
-
.appendChild(innerToken.setAttribute('stage', 4));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** @override */
|
|
123
|
-
cloneNode() {
|
|
124
|
-
const /** @type {TdToken} */ token = super.cloneNode();
|
|
125
|
-
token.setAttribute('innerSyntax', this.#innerSyntax);
|
|
126
|
-
return token;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* 创建新的单元格
|
|
131
|
-
* @param {string|Token} inner 内部wikitext
|
|
132
|
-
* @param {'td'|'th'|'caption'} subtype 单元格类型
|
|
133
|
-
* @param {Record<string, string>} attr 单元格属性
|
|
134
|
-
* @param {boolean} include 是否嵌入
|
|
135
|
-
* @throws `RangeError` 非法的单元格类型
|
|
136
|
-
*/
|
|
137
|
-
static create(inner, subtype = 'td', attr = {}, include = false, config = Parser.getConfig()) {
|
|
138
|
-
if (typeof inner !== 'string' && (!(inner instanceof Token) || !inner.isPlain()) || typeof attr !== 'object') {
|
|
139
|
-
typeError(this, 'create', 'String', 'Token', 'Object');
|
|
140
|
-
} else if (subtype !== 'td' && subtype !== 'th' && subtype !== 'caption') {
|
|
141
|
-
throw new RangeError('单元格的子类型只能为 "td"、"th" 或 "caption"!');
|
|
142
|
-
} else if (typeof inner === 'string') {
|
|
143
|
-
inner = Parser.parse(inner, include, undefined, config);
|
|
144
|
-
}
|
|
145
|
-
const token = Parser.run(() => new TdToken('\n|', undefined, config));
|
|
146
|
-
token.setSyntax(subtype);
|
|
147
|
-
token.lastChild.safeReplaceWith(inner);
|
|
148
|
-
for (const [k, v] of Object.entries(attr)) {
|
|
149
|
-
token.setAttr(k, v);
|
|
150
|
-
}
|
|
151
|
-
return token;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* @override
|
|
156
|
-
* @template {string} T
|
|
157
|
-
* @param {T} key 属性键
|
|
158
|
-
* @returns {TokenAttribute<T>}
|
|
159
|
-
*/
|
|
160
|
-
getAttribute(key) {
|
|
161
|
-
return key === 'innerSyntax' ? this.#innerSyntax : super.getAttribute(key);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* @override
|
|
166
|
-
* @template {string} T
|
|
167
|
-
* @param {T} key 属性键
|
|
168
|
-
* @param {TokenAttribute<T>} value 属性值
|
|
169
|
-
* @returns {this}
|
|
170
|
-
* @throws `RangeError` 仅用于代码调试
|
|
171
|
-
*/
|
|
172
|
-
setAttribute(key, value) {
|
|
173
|
-
if (key !== 'innerSyntax') {
|
|
174
|
-
return super.setAttribute(key, value);
|
|
175
|
-
} else if (!Parser.debugging && externalUse('setAttribute')) {
|
|
176
|
-
throw new RangeError(`使用 ${this.constructor.name}.setAttribute 方法设置私有属性 #${key} 仅用于代码调试!`);
|
|
177
|
-
}
|
|
178
|
-
this.#innerSyntax = String(value);
|
|
179
|
-
return this;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/** @override */
|
|
183
|
-
afterBuild() {
|
|
184
|
-
if (this.#innerSyntax.includes('\0')) {
|
|
185
|
-
this.#innerSyntax = this.getAttribute('buildFromStr')(this.#innerSyntax).map(String).join('');
|
|
186
|
-
}
|
|
187
|
-
return this;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* @override
|
|
192
|
-
* @param {string} syntax 表格语法
|
|
193
|
-
* @param {boolean} esc 是否需要转义
|
|
194
|
-
*/
|
|
195
|
-
setSyntax(syntax, esc) {
|
|
196
|
-
super.setSyntax(aliases[syntax] ?? syntax, esc);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* 修复\<td\>语法
|
|
201
|
-
* @complexity `n`
|
|
202
|
-
*/
|
|
203
|
-
#correct() {
|
|
204
|
-
if (String(this.childNodes[1])) {
|
|
205
|
-
this.#innerSyntax ||= '|';
|
|
206
|
-
}
|
|
207
|
-
const {subtype, escape, correction} = this.getSyntax();
|
|
208
|
-
if (correction) {
|
|
209
|
-
this.setSyntax(subtype, escape);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* 改为独占一行
|
|
215
|
-
* @complexity `n`
|
|
216
|
-
*/
|
|
217
|
-
independence() {
|
|
218
|
-
if (!this.isIndependent()) {
|
|
219
|
-
const {subtype, escape} = this.getSyntax();
|
|
220
|
-
this.setSyntax(subtype, escape);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* @override
|
|
226
|
-
* @param {string} selector
|
|
227
|
-
* @returns {string}
|
|
228
|
-
* @complexity `n`
|
|
229
|
-
*/
|
|
230
|
-
toString(selector) {
|
|
231
|
-
this.#correct();
|
|
232
|
-
const {childNodes: [syntax, attr, inner]} = this;
|
|
233
|
-
return selector && this.matches(selector)
|
|
234
|
-
? ''
|
|
235
|
-
: `${syntax.toString(selector)}${attr.toString(selector)}${this.#innerSyntax}${inner.toString(selector)}`;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* @override
|
|
240
|
-
* @param {number} i 子节点位置
|
|
241
|
-
*/
|
|
242
|
-
getGaps(i = 0) {
|
|
243
|
-
i = i < 0 ? i + this.childNodes.length : i;
|
|
244
|
-
if (i === 1) {
|
|
245
|
-
this.#correct();
|
|
246
|
-
return this.#innerSyntax.length;
|
|
247
|
-
}
|
|
248
|
-
return 0;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/** @override */
|
|
252
|
-
print() {
|
|
253
|
-
const {childNodes: [syntax, attr, inner]} = this;
|
|
254
|
-
return `<span class="wpb-td">${syntax.print()}${attr.print()}${this.#innerSyntax}${inner.print()}</span>`;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* @override
|
|
259
|
-
* @returns {string}
|
|
260
|
-
* @complexity `n`
|
|
261
|
-
*/
|
|
262
|
-
text() {
|
|
263
|
-
this.#correct();
|
|
264
|
-
const {childNodes: [syntax, attr, inner]} = this;
|
|
265
|
-
return `${syntax.text()}${attr.text()}${this.#innerSyntax}${inner.text()}`;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* 获取单元格属性
|
|
270
|
-
* @template {string} T
|
|
271
|
-
* @param {T} key 属性键
|
|
272
|
-
* @returns {T extends 'rowspan'|'colspan' ? number : Record<string, string|true>}
|
|
273
|
-
*/
|
|
274
|
-
getAttr(key) {
|
|
275
|
-
const /** @type {string|true} */ value = super.getAttr(key);
|
|
276
|
-
key = key?.toLowerCase()?.trim();
|
|
277
|
-
return key === 'rowspan' || key === 'colspan' ? Number(value) || 1 : value;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* 设置单元格属性
|
|
282
|
-
* @template {string} T
|
|
283
|
-
* @param {T} key 属性键
|
|
284
|
-
* @param {T extends 'rowspan'|'colspan' ? number : string|boolean} value 属性值
|
|
285
|
-
*/
|
|
286
|
-
setAttr(key, value) {
|
|
287
|
-
if (typeof key !== 'string') {
|
|
288
|
-
this.typeError('setAttr', 'String');
|
|
289
|
-
}
|
|
290
|
-
key = key.toLowerCase().trim();
|
|
291
|
-
if (typeof value === 'number' && (key === 'rowspan' || key === 'colspan')) {
|
|
292
|
-
value = value === 1 ? false : String(value);
|
|
293
|
-
}
|
|
294
|
-
const /** @type {boolean} */ result = super.setAttr(key, value);
|
|
295
|
-
if (!String(this.childNodes[1])) {
|
|
296
|
-
this.#innerSyntax = '';
|
|
297
|
-
}
|
|
298
|
-
return result;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/** @override */
|
|
302
|
-
escape() {
|
|
303
|
-
super.escape();
|
|
304
|
-
if (String(this.childNodes[1])) {
|
|
305
|
-
this.#innerSyntax ||= '{{!}}';
|
|
306
|
-
}
|
|
307
|
-
if (this.#innerSyntax === '|') {
|
|
308
|
-
this.#innerSyntax = '{{!}}';
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
Parser.classes.TdToken = __filename;
|
|
314
|
-
module.exports = TdToken;
|
package/src/table/tr.js
DELETED
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const {generateForChild} = require('../../util/lint'),
|
|
4
|
-
attributeParent = require('../../mixin/attributeParent'),
|
|
5
|
-
Parser = require('../..'),
|
|
6
|
-
AstText = require('../../lib/text'),
|
|
7
|
-
Token = require('..'),
|
|
8
|
-
SyntaxToken = require('../syntax'),
|
|
9
|
-
AttributeToken = require('../attribute');
|
|
10
|
-
|
|
11
|
-
const openingPattern = /^\n[^\S\n]*(?:\|-+|\{\{\s*!\s*\}\}-+|\{\{\s*!-\s*\}\}-*)$/u;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 转义表格语法
|
|
15
|
-
* @param {SyntaxToken} syntax 表格语法节点
|
|
16
|
-
*/
|
|
17
|
-
const escapeTable = syntax => {
|
|
18
|
-
const templates = {'{|': '(!', '|}': '!)', '||': '!!', '|': '!'},
|
|
19
|
-
wikitext = syntax.childNodes.map(
|
|
20
|
-
child => child.type === 'text'
|
|
21
|
-
? String(child).replaceAll(/\{\||\|\}|\|{2}|\|/gu, p => `{{${templates[p]}}}`)
|
|
22
|
-
: String(child),
|
|
23
|
-
).join(''),
|
|
24
|
-
token = Parser.parse(wikitext, syntax.getAttribute('include'), 2, syntax.getAttribute('config'));
|
|
25
|
-
syntax.replaceChildren(...token.childNodes);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 表格行,含开头的换行,不含结尾的换行
|
|
30
|
-
* @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?Token, ...TdToken]}`
|
|
31
|
-
*/
|
|
32
|
-
class TrToken extends attributeParent(Token, 1) {
|
|
33
|
-
type = 'tr';
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @param {string} syntax 表格语法
|
|
37
|
-
* @param {string} attr 表格属性
|
|
38
|
-
* @param {accum} accum
|
|
39
|
-
* @param {RegExp} pattern 表格语法正则
|
|
40
|
-
*/
|
|
41
|
-
constructor(syntax, attr = '', config = Parser.getConfig(), accum = [], pattern = openingPattern) {
|
|
42
|
-
super(undefined, config, true, accum, {Token: 2, SyntaxToken: 0, AttributeToken: 1, TdToken: '2:'});
|
|
43
|
-
this.append(
|
|
44
|
-
new SyntaxToken(syntax, pattern, 'table-syntax', config, accum, {
|
|
45
|
-
'Stage-1': ':', '!ExtToken': '', TranscludeToken: ':',
|
|
46
|
-
}),
|
|
47
|
-
new AttributeToken(attr, 'table-attr', 'tr', config, accum),
|
|
48
|
-
);
|
|
49
|
-
this.getAttribute('protectChildren')(0, 1);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @override
|
|
54
|
-
* @param {number} start 起始位置
|
|
55
|
-
*/
|
|
56
|
-
lint(start = 0) {
|
|
57
|
-
const errors = super.lint(start),
|
|
58
|
-
inter = this.childNodes.find(({type}) => type === 'table-inter'),
|
|
59
|
-
str = String(inter).trim();
|
|
60
|
-
if (inter && str && !/^<!--.*-->$/u.test(str)) {
|
|
61
|
-
const error = generateForChild(inter, this.getRootNode().posFromIndex(start), '将被移出表格的内容');
|
|
62
|
-
error.startLine++;
|
|
63
|
-
error.startCol = 0;
|
|
64
|
-
errors.push(error);
|
|
65
|
-
}
|
|
66
|
-
return errors;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @override
|
|
71
|
-
* @this {TrToken & {constructor: typeof TrToken}}
|
|
72
|
-
*/
|
|
73
|
-
cloneNode() {
|
|
74
|
-
const [syntax, attr, inner, ...cloned] = this.cloneChildNodes();
|
|
75
|
-
return Parser.run(() => {
|
|
76
|
-
const token = new this.constructor(undefined, undefined, this.getAttribute('config'));
|
|
77
|
-
token.firstChild.safeReplaceWith(syntax);
|
|
78
|
-
token.childNodes[1].safeReplaceWith(attr);
|
|
79
|
-
if (token.type === 'td') { // TdToken
|
|
80
|
-
token.childNodes[2].safeReplaceWith(inner);
|
|
81
|
-
} else if (inner !== undefined) {
|
|
82
|
-
token.appendChild(inner);
|
|
83
|
-
}
|
|
84
|
-
token.append(...cloned);
|
|
85
|
-
return token;
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** 修复简单的表格语法错误 */
|
|
90
|
-
#correct() {
|
|
91
|
-
const {childNodes: [,, child]} = this;
|
|
92
|
-
if (child?.isPlain()) {
|
|
93
|
-
const /** @type {{firstChild: AstText}} */ {firstChild} = child;
|
|
94
|
-
if (firstChild.type !== 'text') {
|
|
95
|
-
child.prepend('\n');
|
|
96
|
-
} else if (firstChild.data[0] !== '\n') {
|
|
97
|
-
firstChild.insertData(0, '\n');
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @override
|
|
104
|
-
* @param {string} selector
|
|
105
|
-
*/
|
|
106
|
-
toString(selector) {
|
|
107
|
-
this.#correct();
|
|
108
|
-
return super.toString(selector);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** @override */
|
|
112
|
-
text() {
|
|
113
|
-
this.#correct();
|
|
114
|
-
const str = super.text();
|
|
115
|
-
return this.type === 'tr' && !str.trim().includes('\n') ? '' : str;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 转义表格语法
|
|
120
|
-
* @complexity `n`
|
|
121
|
-
*/
|
|
122
|
-
escape() {
|
|
123
|
-
for (const child of this.childNodes) {
|
|
124
|
-
if (child instanceof SyntaxToken) {
|
|
125
|
-
escapeTable(child);
|
|
126
|
-
} else if (child instanceof TrToken) {
|
|
127
|
-
child.escape();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* 设置表格语法
|
|
134
|
-
* @param {string} syntax 表格语法
|
|
135
|
-
* @param {boolean} esc 是否需要转义
|
|
136
|
-
*/
|
|
137
|
-
setSyntax(syntax, esc) {
|
|
138
|
-
const {firstChild} = this;
|
|
139
|
-
firstChild.replaceChildren(syntax);
|
|
140
|
-
if (esc) {
|
|
141
|
-
escapeTable(firstChild);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* @override
|
|
147
|
-
* @param {number} i 移除位置
|
|
148
|
-
* @complexity `n`
|
|
149
|
-
*/
|
|
150
|
-
removeAt(i) {
|
|
151
|
-
const TdToken = require('./td');
|
|
152
|
-
const child = this.childNodes.at(i);
|
|
153
|
-
if (child instanceof TdToken && child.isIndependent()) {
|
|
154
|
-
const {nextSibling} = child;
|
|
155
|
-
if (nextSibling?.type === 'td') {
|
|
156
|
-
nextSibling.independence();
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return super.removeAt(i);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* @override
|
|
164
|
-
* @template {AstText|Token} T
|
|
165
|
-
* @param {T} token 待插入的子节点
|
|
166
|
-
* @param {number} i 插入位置
|
|
167
|
-
* @returns {T}
|
|
168
|
-
* @complexity `n`
|
|
169
|
-
*/
|
|
170
|
-
insertAt(token, i = this.childNodes.length) {
|
|
171
|
-
if (!Parser.running && !(token instanceof TrToken)) {
|
|
172
|
-
this.typeError('insertAt', 'TrToken');
|
|
173
|
-
}
|
|
174
|
-
const TdToken = require('./td');
|
|
175
|
-
const child = this.childNodes.at(i);
|
|
176
|
-
if (token instanceof TdToken && token.isIndependent() && child instanceof TdToken) {
|
|
177
|
-
child.independence();
|
|
178
|
-
}
|
|
179
|
-
return super.insertAt(token, i);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* 获取行数
|
|
184
|
-
* @returns {0|1}
|
|
185
|
-
* @complexity `n`
|
|
186
|
-
*/
|
|
187
|
-
getRowCount() {
|
|
188
|
-
const TdToken = require('./td');
|
|
189
|
-
return Number(this.childNodes.some(
|
|
190
|
-
child => child instanceof TdToken && child.isIndependent() && child.firstChild.text().at(-1) !== '+',
|
|
191
|
-
));
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* 获取相邻行
|
|
196
|
-
* @param {(childNodes: Token[], index: number) => Token[]} subset 筛选兄弟节点的方法
|
|
197
|
-
* @complexity `n`
|
|
198
|
-
*/
|
|
199
|
-
#getSiblingRow(subset) {
|
|
200
|
-
const {parentNode} = this;
|
|
201
|
-
if (!parentNode) {
|
|
202
|
-
return undefined;
|
|
203
|
-
}
|
|
204
|
-
const {childNodes} = parentNode,
|
|
205
|
-
index = childNodes.indexOf(this);
|
|
206
|
-
for (const child of subset(childNodes, index)) {
|
|
207
|
-
if (child instanceof TrToken && child.getRowCount()) {
|
|
208
|
-
return child;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return undefined;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* 获取下一行
|
|
216
|
-
* @complexity `n`
|
|
217
|
-
*/
|
|
218
|
-
getNextRow() {
|
|
219
|
-
return this.#getSiblingRow((childNodes, index) => childNodes.slice(index + 1));
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* 获取前一行
|
|
224
|
-
* @complexity `n`
|
|
225
|
-
*/
|
|
226
|
-
getPreviousRow() {
|
|
227
|
-
return this.#getSiblingRow((childNodes, index) => childNodes.slice(0, index).reverse());
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* 获取列数
|
|
232
|
-
* @complexity `n`
|
|
233
|
-
*/
|
|
234
|
-
getColCount() {
|
|
235
|
-
const TdToken = require('./td');
|
|
236
|
-
let count = 0,
|
|
237
|
-
last = 0;
|
|
238
|
-
for (const child of this.childNodes) {
|
|
239
|
-
if (child instanceof TdToken) {
|
|
240
|
-
last = child.isIndependent() ? Number(child.subtype !== 'caption') : last;
|
|
241
|
-
count += last;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
return count;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* 获取第n列
|
|
249
|
-
* @param {number} n 列号
|
|
250
|
-
* @param {boolean} insert 是否用于判断插入新列的位置
|
|
251
|
-
* @returns {TdToken}
|
|
252
|
-
* @complexity `n`
|
|
253
|
-
* @throws `RangeError` 不存在对应单元格
|
|
254
|
-
*/
|
|
255
|
-
getNthCol(n, insert) {
|
|
256
|
-
if (typeof n !== 'number') {
|
|
257
|
-
this.typeError('getNthCol', 'Number');
|
|
258
|
-
}
|
|
259
|
-
const nCols = this.getColCount();
|
|
260
|
-
n = n < 0 ? n + nCols : n;
|
|
261
|
-
if (n < 0 || n > nCols || n === nCols && !insert) {
|
|
262
|
-
throw new RangeError(`不存在第 ${n} 个单元格!`);
|
|
263
|
-
}
|
|
264
|
-
const TdToken = require('./td');
|
|
265
|
-
let last = 0;
|
|
266
|
-
for (const child of this.childNodes.slice(2)) {
|
|
267
|
-
if (child instanceof TdToken) {
|
|
268
|
-
if (child.isIndependent()) {
|
|
269
|
-
last = Number(child.subtype !== 'caption');
|
|
270
|
-
}
|
|
271
|
-
n -= last;
|
|
272
|
-
if (n < 0) {
|
|
273
|
-
return child;
|
|
274
|
-
}
|
|
275
|
-
} else if (child.type === 'tr' || child.type === 'table-syntax') {
|
|
276
|
-
return child;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
return undefined;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* 插入新的单元格
|
|
284
|
-
* @param {string|Token} inner 单元格内部wikitext
|
|
285
|
-
* @param {TableCoords} coord 单元格坐标
|
|
286
|
-
* @param {'td'|'th'|'caption'} subtype 单元格类型
|
|
287
|
-
* @param {Record<string, string|boolean>} attr 单元格属性
|
|
288
|
-
* @returns {TdToken}
|
|
289
|
-
* @complexity `n`
|
|
290
|
-
*/
|
|
291
|
-
insertTableCell(inner, {column}, subtype = 'td', attr = {}) {
|
|
292
|
-
const TdToken = require('./td');
|
|
293
|
-
const token = TdToken.create(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
|
|
294
|
-
return this.insertBefore(token, this.getNthCol(column, true));
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
Parser.classes.TrToken = __filename;
|
|
299
|
-
module.exports = TrToken;
|
package/src/tagPair/ext.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const attributeParent = require('../../mixin/attributeParent'),
|
|
4
|
-
Parser = require('../..'),
|
|
5
|
-
TagPairToken = require('.');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 扩展标签
|
|
9
|
-
* @classdesc `{childNodes: [AttributeToken, NowikiToken|Token]}`
|
|
10
|
-
*/
|
|
11
|
-
class ExtToken extends attributeParent(TagPairToken) {
|
|
12
|
-
type = 'ext';
|
|
13
|
-
|
|
14
|
-
/** @override */
|
|
15
|
-
get closed() {
|
|
16
|
-
return super.closed;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @param {string} name 标签名
|
|
21
|
-
* @param {string} attr 标签属性
|
|
22
|
-
* @param {string} inner 内部wikitext
|
|
23
|
-
* @param {string|undefined} closed 是否封闭
|
|
24
|
-
* @param {accum} accum
|
|
25
|
-
*/
|
|
26
|
-
constructor(name, attr = '', inner = '', closed = undefined, config = Parser.getConfig(), accum = []) {
|
|
27
|
-
attr = !attr || attr.trimStart() !== attr ? attr : ` ${attr}`;
|
|
28
|
-
const AttributeToken = require('../attribute');
|
|
29
|
-
const lcName = name.toLowerCase(),
|
|
30
|
-
attrToken = new AttributeToken(attr, 'ext-attr', lcName, config, accum),
|
|
31
|
-
newConfig = structuredClone(config),
|
|
32
|
-
ext = new Set(newConfig.ext);
|
|
33
|
-
let /** @type {acceptable} */ acceptable, /** @type {Token} */ innerToken;
|
|
34
|
-
switch (lcName) {
|
|
35
|
-
case 'choose':
|
|
36
|
-
case 'option':
|
|
37
|
-
case 'ref':
|
|
38
|
-
case 'poem':
|
|
39
|
-
case 'indicator':
|
|
40
|
-
case 'tab':
|
|
41
|
-
case 'tabs':
|
|
42
|
-
case 'pre':
|
|
43
|
-
case 'combobox':
|
|
44
|
-
case 'combooption': {
|
|
45
|
-
ext.delete(lcName);
|
|
46
|
-
newConfig.ext = [
|
|
47
|
-
...ext,
|
|
48
|
-
...lcName === 'choose' ? ['option'] : [],
|
|
49
|
-
...lcName === 'combobox' ? ['combooption'] : [],
|
|
50
|
-
];
|
|
51
|
-
const Token = require('..');
|
|
52
|
-
acceptable = {AttributeToken: 0, Token: 1};
|
|
53
|
-
innerToken = new Token(inner, newConfig, true, accum);
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
case 'gallery': {
|
|
57
|
-
ext.delete(lcName);
|
|
58
|
-
newConfig.ext = [...ext];
|
|
59
|
-
const GalleryToken = require('../gallery');
|
|
60
|
-
acceptable = {AttributeToken: 0, GalleryToken: 1};
|
|
61
|
-
innerToken = new GalleryToken(inner, newConfig, accum);
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/*
|
|
66
|
-
* 更多定制扩展的代码示例:
|
|
67
|
-
* ```
|
|
68
|
-
* case 'extensionName': {
|
|
69
|
-
* ext.delete(lcName);
|
|
70
|
-
* newConfig.ext = [...ext];
|
|
71
|
-
* const ExtensionToken = require('../extension');
|
|
72
|
-
* acceptable = {AttributeToken: 0, ExtensionToken: 1};
|
|
73
|
-
* innerToken = new ExtensionToken(inner, newConfig, accum);
|
|
74
|
-
* break;
|
|
75
|
-
* }
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
default: {
|
|
79
|
-
const NowikiToken = require('../nowiki');
|
|
80
|
-
acceptable = {AttributeToken: 0, NowikiToken: 1};
|
|
81
|
-
innerToken = new NowikiToken(inner, config);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
innerToken.setAttribute('name', lcName).type = 'ext-inner';
|
|
85
|
-
if (lcName === 'pre') {
|
|
86
|
-
innerToken.setAttribute('stage', Parser.MAX_STAGE - 1);
|
|
87
|
-
}
|
|
88
|
-
super(name, attrToken, innerToken, closed, config, accum, acceptable);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** @override */
|
|
92
|
-
cloneNode() {
|
|
93
|
-
const inner = this.lastChild.cloneNode(),
|
|
94
|
-
tags = this.getAttribute('tags'),
|
|
95
|
-
config = this.getAttribute('config'),
|
|
96
|
-
attr = String(this.firstChild),
|
|
97
|
-
token = Parser.run(() => new ExtToken(tags[0], attr, '', this.selfClosing ? undefined : tags[1], config));
|
|
98
|
-
token.lastChild.safeReplaceWith(inner);
|
|
99
|
-
return token;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
Parser.classes.ExtToken = __filename;
|
|
104
|
-
module.exports = ExtToken;
|