wikiparser-node 0.6.1 → 0.6.5-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.
Files changed (82) hide show
  1. package/bundle/bundle.min.js +40 -0
  2. package/package.json +9 -11
  3. package/README.md +0 -39
  4. package/config/default.json +0 -832
  5. package/config/llwiki.json +0 -630
  6. package/config/moegirl.json +0 -727
  7. package/config/zhwiki.json +0 -1269
  8. package/index.js +0 -277
  9. package/lib/element.js +0 -612
  10. package/lib/node.js +0 -771
  11. package/lib/ranges.js +0 -130
  12. package/lib/text.js +0 -175
  13. package/lib/title.js +0 -70
  14. package/mixin/attributeParent.js +0 -113
  15. package/mixin/fixedToken.js +0 -40
  16. package/mixin/hidden.js +0 -21
  17. package/mixin/sol.js +0 -68
  18. package/parser/brackets.js +0 -118
  19. package/parser/commentAndExt.js +0 -63
  20. package/parser/converter.js +0 -45
  21. package/parser/externalLinks.js +0 -31
  22. package/parser/hrAndDoubleUnderscore.js +0 -36
  23. package/parser/html.js +0 -42
  24. package/parser/links.js +0 -97
  25. package/parser/list.js +0 -59
  26. package/parser/magicLinks.js +0 -41
  27. package/parser/quotes.js +0 -64
  28. package/parser/selector.js +0 -175
  29. package/parser/table.js +0 -114
  30. package/src/arg.js +0 -198
  31. package/src/atom/hidden.js +0 -13
  32. package/src/atom/index.js +0 -43
  33. package/src/attribute.js +0 -460
  34. package/src/charinsert.js +0 -91
  35. package/src/converter.js +0 -176
  36. package/src/converterFlags.js +0 -279
  37. package/src/converterRule.js +0 -259
  38. package/src/extLink.js +0 -175
  39. package/src/gallery.js +0 -146
  40. package/src/hasNowiki/index.js +0 -42
  41. package/src/hasNowiki/pre.js +0 -40
  42. package/src/heading.js +0 -123
  43. package/src/html.js +0 -230
  44. package/src/imageParameter.js +0 -276
  45. package/src/imagemap.js +0 -205
  46. package/src/imagemapLink.js +0 -43
  47. package/src/index.js +0 -842
  48. package/src/link/category.js +0 -49
  49. package/src/link/file.js +0 -321
  50. package/src/link/galleryImage.js +0 -125
  51. package/src/link/index.js +0 -347
  52. package/src/magicLink.js +0 -135
  53. package/src/nested/choose.js +0 -24
  54. package/src/nested/combobox.js +0 -23
  55. package/src/nested/index.js +0 -88
  56. package/src/nested/references.js +0 -23
  57. package/src/nowiki/comment.js +0 -71
  58. package/src/nowiki/dd.js +0 -59
  59. package/src/nowiki/doubleUnderscore.js +0 -56
  60. package/src/nowiki/hr.js +0 -41
  61. package/src/nowiki/index.js +0 -56
  62. package/src/nowiki/list.js +0 -16
  63. package/src/nowiki/noinclude.js +0 -28
  64. package/src/nowiki/quote.js +0 -63
  65. package/src/onlyinclude.js +0 -61
  66. package/src/paramTag/index.js +0 -83
  67. package/src/paramTag/inputbox.js +0 -42
  68. package/src/parameter.js +0 -214
  69. package/src/syntax.js +0 -91
  70. package/src/table/index.js +0 -981
  71. package/src/table/td.js +0 -308
  72. package/src/table/tr.js +0 -299
  73. package/src/tagPair/ext.js +0 -122
  74. package/src/tagPair/include.js +0 -60
  75. package/src/tagPair/index.js +0 -126
  76. package/src/transclude.js +0 -751
  77. package/tool/index.js +0 -1199
  78. package/util/base.js +0 -17
  79. package/util/debug.js +0 -73
  80. package/util/diff.js +0 -76
  81. package/util/lint.js +0 -40
  82. package/util/string.js +0 -105
@@ -1,49 +0,0 @@
1
- 'use strict';
2
-
3
- const Title = require('../../lib/title'),
4
- Parser = require('../..'),
5
- LinkToken = require('.');
6
-
7
- /**
8
- * 分类
9
- * @classdesc `{childNodes: [AtomToken, ?Token]}`
10
- */
11
- class CategoryToken extends LinkToken {
12
- type = 'category';
13
-
14
- /** 分类排序关键字 */
15
- get sortkey() {
16
- return this.childNodes[1]?.text()?.replaceAll(
17
- /&#(\d+|x[\da-f]+);|\n/gu,
18
- /** @param {string} p */
19
- (_, p) => p ? String.fromCodePoint(p[0] === 'x' ? parseInt(p.slice(1), 16) : Number(p)) : '',
20
- );
21
- }
22
-
23
- set sortkey(text) {
24
- this.setSortkey(text);
25
- }
26
-
27
- /**
28
- * @param {string} link 分类名
29
- * @param {string|undefined} text 排序关键字
30
- * @param {Title} title 分类页面标题对象
31
- * @param {accum} accum
32
- * @param {string} delimiter `|`
33
- */
34
- constructor(link, text, title, config = Parser.getConfig(), accum = [], delimiter = '|') {
35
- super(link, text, title, config, accum, delimiter);
36
- this.seal(['selfLink', 'interwiki', 'setLangLink', 'setFragment', 'asSelfLink', 'pipeTrick'], true);
37
- }
38
-
39
- /**
40
- * 设置排序关键字
41
- * @param {string} text 排序关键字
42
- */
43
- setSortkey(text) {
44
- this.setLinkText(text);
45
- }
46
- }
47
-
48
- Parser.classes.CategoryToken = __filename;
49
- module.exports = CategoryToken;
package/src/link/file.js DELETED
@@ -1,321 +0,0 @@
1
- 'use strict';
2
-
3
- const Title = require('../../lib/title'),
4
- {explode, noWrap} = require('../../util/string'),
5
- {externalUse} = require('../../util/debug'),
6
- {generateForChild} = require('../../util/lint'),
7
- Parser = require('../..'),
8
- LinkToken = require('.'),
9
- ImageParameterToken = require('../imageParameter');
10
-
11
- /**
12
- * 图片
13
- * @classdesc `{childNodes: [AtomToken, ...ImageParameterToken]}`
14
- */
15
- class FileToken extends LinkToken {
16
- type = 'file';
17
- /** @type {Set<string>} */ #keys = new Set();
18
- /** @type {Record<string, Set<ImageParameterToken>>} */ #args = {};
19
-
20
- /** 图片链接 */
21
- get link() {
22
- return this.getArg('link')?.link;
23
- }
24
-
25
- set link(value) {
26
- this.setValue('link', value);
27
- }
28
-
29
- /** 图片大小 */
30
- get size() {
31
- return this.getArg('width')?.size;
32
- }
33
-
34
- /** 图片宽度 */
35
- get width() {
36
- return this.size?.width;
37
- }
38
-
39
- set width(width) {
40
- const arg = this.getArg('width');
41
- if (arg) {
42
- arg.width = width;
43
- } else {
44
- this.setValue('width', width);
45
- }
46
- }
47
-
48
- /** 图片高度 */
49
- get height() {
50
- return this.size?.height;
51
- }
52
-
53
- set height(height) {
54
- const arg = this.getArg('width');
55
- if (arg) {
56
- arg.height = height;
57
- } else {
58
- this.setValue('width', `x${height}`);
59
- }
60
- }
61
-
62
- /**
63
- * @param {string} link 文件名
64
- * @param {string|undefined} text 图片参数
65
- * @param {Title} title 文件标题对象
66
- * @param {accum} accum
67
- * @param {string} delimiter `|`
68
- * @complexity `n`
69
- */
70
- constructor(link, text, title, config = Parser.getConfig(), accum = [], delimiter = '|') {
71
- super(link, undefined, title, config, accum, delimiter);
72
- this.setAttribute('acceptable', {AtomToken: 0, ImageParameterToken: '1:'});
73
- this.append(...explode('-{', '}-', '|', text).map(part => new ImageParameterToken(part, config, accum)));
74
- this.seal(
75
- ['selfLink', 'interwiki', 'setLangLink', 'setFragment', 'asSelfLink', 'setLinkText', 'pipeTrick'],
76
- true,
77
- );
78
- }
79
-
80
- /**
81
- * @override
82
- * @param {number} start 起始位置
83
- */
84
- lint(start = 0) {
85
- const errors = super.lint(start),
86
- frameArgs = this.getFrameArgs(),
87
- horizAlignArgs = this.getHorizAlignArgs(),
88
- vertAlignArgs = this.getVertAlignArgs(),
89
- captions = this.getArgs('caption');
90
- if (frameArgs.length > 1 || horizAlignArgs.length > 1 || vertAlignArgs.length > 1 || captions.size > 1) {
91
- const rect = this.getRootNode().posFromIndex(start);
92
- if (frameArgs.length > 1) {
93
- errors.push(...frameArgs.map(arg => generateForChild(arg, rect, '重复或冲突的图片框架参数')));
94
- }
95
- if (horizAlignArgs.length > 1) {
96
- errors.push(...horizAlignArgs.map(arg => generateForChild(arg, rect, '重复或冲突的图片水平对齐参数')));
97
- }
98
- if (vertAlignArgs.length > 1) {
99
- errors.push(...vertAlignArgs.map(arg => generateForChild(arg, rect, '重复或冲突的图片垂直对齐参数')));
100
- }
101
- if (captions.size > 1) {
102
- errors.push(...[...captions].map(arg => generateForChild(arg, rect, '重复的图片说明')));
103
- }
104
- }
105
- return errors;
106
- }
107
-
108
- /**
109
- * 获取所有图片参数节点
110
- * @returns {ImageParameterToken[]}
111
- */
112
- getAllArgs() {
113
- return this.childNodes.slice(1);
114
- }
115
-
116
- /**
117
- * 获取指定图片参数
118
- * @param {string} key 参数名
119
- * @param {boolean} copy 是否返回备份
120
- * @complexity `n`
121
- */
122
- getArgs(key, copy = true) {
123
- if (typeof key !== 'string') {
124
- this.typeError('getArgs', 'String');
125
- }
126
- copy ||= !Parser.debugging && externalUse('getArgs');
127
- let args;
128
- if (Object.hasOwn(this.#args, key)) {
129
- args = this.#args[key];
130
- } else {
131
- args = new Set(this.getAllArgs().filter(({name}) => key === name));
132
- this.#args[key] = args;
133
- }
134
- return copy ? new Set(args) : args;
135
- }
136
-
137
- /**
138
- * 获取图片框架属性参数节点
139
- * @complexity `n`
140
- */
141
- getFrameArgs() {
142
- const args = this.getAllArgs()
143
- .filter(({name}) => ['manualthumb', 'frameless', 'framed', 'thumbnail'].includes(name));
144
- if (args.length > 1) {
145
- Parser.error(`图片 ${this.name} 带有 ${args.length} 个框架参数,只有第 1 个 ${args[0].name} 会生效!`);
146
- }
147
- return args;
148
- }
149
-
150
- /**
151
- * 获取图片水平对齐参数节点
152
- * @complexity `n`
153
- */
154
- getHorizAlignArgs() {
155
- const args = this.getAllArgs()
156
- .filter(({name}) => ['left', 'right', 'center', 'none'].includes(name));
157
- if (args.length > 1) {
158
- Parser.error(`图片 ${this.name} 带有 ${args.length} 个水平对齐参数,只有第 1 个 ${args[0].name} 会生效!`);
159
- }
160
- return args;
161
- }
162
-
163
- /**
164
- * 获取图片垂直对齐参数节点
165
- * @complexity `n`
166
- */
167
- getVertAlignArgs() {
168
- const args = this.getAllArgs().filter(
169
- ({name}) => ['baseline', 'sub', 'super', 'top', 'text-top', 'middle', 'bottom', 'text-bottom']
170
- .includes(name),
171
- );
172
- if (args.length > 1) {
173
- Parser.error(`图片 ${this.name} 带有 ${args.length} 个垂直对齐架参数,只有第 1 个 ${args[0].name} 会生效!`);
174
- }
175
- return args;
176
- }
177
-
178
- /**
179
- * 获取生效的指定图片参数
180
- * @param {string} key 参数名
181
- * @complexity `n`
182
- */
183
- getArg(key) {
184
- return [...this.getArgs(key, false)].sort((a, b) => a.compareDocumentPosition(b)).at(-1);
185
- }
186
-
187
- /**
188
- * 是否具有指定图片参数
189
- * @param {string} key 参数名
190
- * @complexity `n`
191
- */
192
- hasArg(key) {
193
- return this.getArgs(key, false).size > 0;
194
- }
195
-
196
- /**
197
- * 移除指定图片参数
198
- * @param {string} key 参数名
199
- * @complexity `n`
200
- */
201
- removeArg(key) {
202
- for (const token of this.getArgs(key, false)) {
203
- this.removeChild(token);
204
- }
205
- }
206
-
207
- /**
208
- * 获取图片参数名
209
- * @complexity `n`
210
- */
211
- getKeys() {
212
- const args = this.getAllArgs();
213
- if (this.#keys.size === 0 && args.length > 0) {
214
- for (const {name} of args) {
215
- this.#keys.add(name);
216
- }
217
- }
218
- return [...this.#keys];
219
- }
220
-
221
- /**
222
- * 获取指定的图片参数值
223
- * @param {string} key 参数名
224
- * @complexity `n`
225
- */
226
- getValues(key) {
227
- return [...this.getArgs(key, false)].map(token => token.getValue());
228
- }
229
-
230
- /**
231
- * 获取生效的指定图片参数值
232
- * @template {string|undefined} T
233
- * @param {T} key 参数名
234
- * @returns {T extends undefined ? Record<string, string> : string|true}
235
- * @complexity `n`
236
- */
237
- getValue(key) {
238
- return key === undefined
239
- ? Object.fromEntries(this.getKeys().map(k => [k, this.getValue(k)]))
240
- : this.getArg(key)?.getValue();
241
- }
242
-
243
- /**
244
- * 设置图片参数
245
- * @param {string} key 参数名
246
- * @param {string|boolean} value 参数值
247
- * @complexity `n`
248
- * @throws `RangeError` 未定义的图片参数
249
- * @throws `SyntaxError` 非法的参数
250
- */
251
- setValue(key, value) {
252
- if (typeof key !== 'string') {
253
- this.typeError('setValue', 'String');
254
- } else if (value === false) {
255
- this.removeArg(key);
256
- return;
257
- }
258
- const token = this.getArg(key);
259
- value = value === true ? value : String(value);
260
- if (token) {
261
- token.setValue(value);
262
- return;
263
- }
264
- let syntax = '';
265
- const config = this.getAttribute('config');
266
- if (key !== 'caption') {
267
- syntax = Object.entries(config.img).find(([, name]) => name === key)?.[0];
268
- if (!syntax) {
269
- throw new RangeError(`未定义的图片参数: ${key}`);
270
- }
271
- }
272
- if (value === true) {
273
- if (syntax.includes('$1')) {
274
- this.typeError('setValue', 'Boolean');
275
- }
276
- const newArg = Parser.run(() => new ImageParameterToken(syntax, config));
277
- this.insertAt(newArg);
278
- return;
279
- }
280
- const wikitext = `[[File:F|${syntax ? syntax.replace('$1', value) : value}]]`,
281
- root = Parser.parse(wikitext, this.getAttribute('include'), 6, config),
282
- {length, firstChild: file} = root,
283
- {name, type, length: fileLength, lastChild: imageParameter} = file;
284
- if (length !== 1 || type !== 'file' || name !== 'File:F' || fileLength !== 2 || imageParameter.name !== key) {
285
- throw new SyntaxError(`非法的 ${key} 参数:${noWrap(value)}`);
286
- }
287
- this.insertAt(imageParameter);
288
- }
289
-
290
- /**
291
- * @override
292
- * @param {number} i 移除位置
293
- * @complexity `n`
294
- */
295
- removeAt(i) {
296
- const /** @type {ImageParameterToken} */ token = super.removeAt(i),
297
- args = this.getArgs(token.name, false, false);
298
- args.delete(token);
299
- if (args.size === 0) {
300
- this.#keys.delete(token.name);
301
- }
302
- return token;
303
- }
304
-
305
- /**
306
- * @override
307
- * @param {ImageParameterToken} token 待插入的子节点
308
- * @param {number} i 插入位置
309
- * @complexity `n`
310
- */
311
- insertAt(token, i = this.childNodes.length) {
312
- if (!Parser.running) {
313
- this.getArgs(token.name, false, false).add(token);
314
- this.#keys.add(token.name);
315
- }
316
- return super.insertAt(token, i);
317
- }
318
- }
319
-
320
- Parser.classes.FileToken = __filename;
321
- module.exports = FileToken;
@@ -1,125 +0,0 @@
1
- 'use strict';
2
-
3
- const {undo} = require('../../util/debug'),
4
- {generateForSelf} = require('../../util/lint'),
5
- Title = require('../../lib/title'),
6
- Parser = require('../..'),
7
- FileToken = require('./file');
8
-
9
- /**
10
- * 图片
11
- * @classdesc `{childNodes: [AtomToken, ...ImageParameterToken]}`
12
- */
13
- class GalleryImageToken extends FileToken {
14
- type = 'gallery-image';
15
- #invalid = false;
16
-
17
- /**
18
- * @param {string} link 图片文件名
19
- * @param {string|undefined} text 图片参数
20
- * @param {Title} title 图片文件标题对象
21
- * @param {accum} accum
22
- */
23
- constructor(link, text, title, config = Parser.getConfig(), accum = []) {
24
- let token;
25
- if (text !== undefined) {
26
- const Token = require('..');
27
- token = new Token(text, config, true, accum);
28
- token.type = 'temp';
29
- for (let n = 1; n < Parser.MAX_STAGE; n++) {
30
- token.getAttribute('parseOnce')();
31
- }
32
- accum.splice(accum.indexOf(token), 1);
33
- }
34
- super(link, token?.toString(), title, config, accum);
35
- this.setAttribute('bracket', false);
36
- if (!Object.values(config.img).includes('width')) {
37
- this.seal(['size', 'width', 'height'], true);
38
- }
39
- }
40
-
41
- /**
42
- * @override
43
- * @throws `Error` 非法的内链目标
44
- * @throws `Error` 不可更改命名空间
45
- */
46
- afterBuild() {
47
- const {
48
- title: initTitle, interwiki: initInterwiki, ns: initNs,
49
- } = this.normalizeTitle(String(this.firstChild), this.type === 'imagemap-image' ? 0 : 6, true);
50
- this.setAttribute('name', initTitle);
51
- this.#invalid = initInterwiki || initNs !== 6; // 只用于gallery-image的首次解析
52
- const /** @type {AstListener} */ linkListener = (e, data) => {
53
- const {prevTarget} = e;
54
- if (prevTarget?.type === 'link-target') {
55
- const name = String(prevTarget),
56
- defaultNs = this.type === 'imagemap-image' ? 0 : 6,
57
- {title, interwiki, ns, valid} = this.normalizeTitle(name, defaultNs, true);
58
- if (!valid) {
59
- undo(e, data);
60
- throw new Error(`非法的图片文件名:${name}`);
61
- } else if (interwiki || ns !== 6) {
62
- undo(e, data);
63
- throw new Error(`图片链接不可更改命名空间:${name}`);
64
- }
65
- this.setAttribute('name', title);
66
- this.#invalid = false;
67
- }
68
- };
69
- this.addEventListener(['remove', 'insert', 'replace', 'text'], linkListener);
70
- return this;
71
- }
72
-
73
- /** @override */
74
- getPadding() {
75
- return 0;
76
- }
77
-
78
- /**
79
- * @override
80
- * @param {number} start 起始位置
81
- */
82
- lint(start = 0) {
83
- const errors = super.lint(start);
84
- if (this.#invalid) {
85
- errors.push(generateForSelf(this, this.getRootNode().posFromIndex(start), '无效的图库图片'));
86
- }
87
- return errors;
88
- }
89
-
90
- /**
91
- * @override
92
- * @param {string} selector
93
- */
94
- toString(selector) {
95
- return super.toString(selector).replaceAll('\n', ' ');
96
- }
97
-
98
- /** @override */
99
- text() {
100
- return super.text().replaceAll('\n', ' ');
101
- }
102
-
103
- /**
104
- * @override
105
- * @param {string} link 链接目标
106
- * @throws `SyntaxError` 非法的链接目标
107
- */
108
- setTarget(link) {
109
- link = String(link);
110
- const include = this.getAttribute('include'),
111
- config = this.getAttribute('config'),
112
- root = Parser.parse(`<gallery>${link}</gallery>`, include, 1, config),
113
- {length, firstChild: gallery} = root,
114
- {type, lastChild: {length: galleryLength, firstChild: image}} = gallery;
115
- if (length !== 1 || type !== 'ext' || galleryLength !== 1 || image.type !== 'gallery-image') {
116
- throw new SyntaxError(`非法的图库文件名:${link}`);
117
- }
118
- const {firstChild} = image;
119
- image.destroy(true);
120
- this.firstChild.safeReplaceWith(firstChild);
121
- }
122
- }
123
-
124
- Parser.classes.GalleryImageToken = __filename;
125
- module.exports = GalleryImageToken;