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
package/lib/ranges.js DELETED
@@ -1,130 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..');
4
-
5
- /** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
6
- class Range {
7
- start;
8
- end;
9
- step;
10
-
11
- /**
12
- * @param {string|Range} str 表达式
13
- * @throws `RangeError` 起点、终点和步长均应为整数
14
- * @throws `RangeError` n的系数不能为0
15
- * @throws `RangeError` 应使用CSS选择器或Python切片的格式
16
- */
17
- constructor(str) {
18
- if (str instanceof Range) {
19
- Object.assign(this, str);
20
- return;
21
- }
22
- str = str.trim();
23
- if (str === 'odd') {
24
- Object.assign(this, {start: 1, end: Infinity, step: 2});
25
- } else if (str === 'even') {
26
- Object.assign(this, {start: 0, end: Infinity, step: 2});
27
- } else if (str.includes(':')) {
28
- const [start, end, step = '1'] = str.split(':', 3);
29
- this.start = Number(start);
30
- this.end = Number(end || Infinity);
31
- this.step = Math.max(Number(step), 1);
32
- if (!Number.isInteger(this.start)) {
33
- throw new RangeError(`起点 ${this.start} 应为整数!`);
34
- } else if (this.end !== Infinity && !Number.isInteger(this.end)) {
35
- throw new RangeError(`终点 ${this.end} 应为整数!`);
36
- } else if (!Number.isInteger(this.step)) {
37
- throw new RangeError(`步长 ${this.step} 应为整数!`);
38
- }
39
- } else {
40
- const mt = /^([+-])?(\d+)?n(?:([+-])(\d+))?$/u.exec(str);
41
- if (mt) {
42
- const [, sgnA = '+', a = 1, sgnB = '+'] = mt,
43
- b = Number(mt[4] ?? 0);
44
- this.step = Number(a);
45
- if (this.step === 0) {
46
- throw new RangeError(`参数 ${str} 中 "n" 的系数不允许为 0!`);
47
- } else if (sgnA === '+') {
48
- this.start = sgnB === '+' || b === 0 ? b : this.step - 1 - (b - 1) % this.step;
49
- this.end = Infinity;
50
- } else if (sgnB === '-') {
51
- this.start = 0;
52
- this.end = b > 0 ? 0 : this.step;
53
- } else {
54
- this.start = b % this.step;
55
- this.end = this.step + b;
56
- }
57
- } else {
58
- throw new RangeError(`参数 ${str} 应写作CSS选择器的 "an+b" 形式或Python切片!`);
59
- }
60
- }
61
- }
62
-
63
- /**
64
- * 将Range转换为针对特定数组的下标集
65
- * @param {number|*[]} arr 参考数组
66
- * @complexity `n`
67
- */
68
- applyTo(arr) {
69
- return new Array(typeof arr === 'number' ? arr : arr.length).fill().map((_, i) => i).slice(this.start, this.end)
70
- .filter((_, j) => j % this.step === 0);
71
- }
72
- }
73
-
74
- /** @extends {Array<number|Range>} */
75
- class Ranges extends Array {
76
- /** @param {number|string|Range|(number|string|Range)[]} arr 表达式数组 */
77
- constructor(arr) {
78
- super();
79
- if (arr === undefined) {
80
- return;
81
- }
82
- arr = Array.isArray(arr) ? arr : [arr];
83
- for (const ele of arr) {
84
- if (ele instanceof Range) {
85
- this.push(new Range(ele));
86
- continue;
87
- }
88
- const number = Number(ele);
89
- if (Number.isInteger(number)) {
90
- this.push(number);
91
- } else if (typeof ele === 'string' && Number.isNaN(number)) {
92
- try {
93
- const range = new Range(ele);
94
- this.push(range);
95
- } catch {}
96
- }
97
- }
98
- }
99
-
100
- /**
101
- * 将Ranges转换为针对特定Array的下标集
102
- * @param {number|*[]} arr 参考数组
103
- * @complexity `n`
104
- */
105
- applyTo(arr) {
106
- const length = typeof arr === 'number' ? arr : arr.length;
107
- return [
108
- ...new Set(
109
- [...this].flatMap(ele => {
110
- if (typeof ele === 'number') {
111
- return ele < 0 ? ele + length : ele;
112
- }
113
- return ele.applyTo(length);
114
- }),
115
- ),
116
- ].filter(i => i >= 0 && i < length).sort();
117
- }
118
-
119
- /**
120
- * 检查某个下标是否符合表达式
121
- * @param {string} str 表达式
122
- * @param {number} i 待检查的下标
123
- */
124
- static nth(str, i) {
125
- return new Ranges(str.split(',')).applyTo(i + 1).includes(i);
126
- }
127
- }
128
-
129
- Parser.classes.Ranges = __filename;
130
- module.exports = Ranges;
package/lib/text.js DELETED
@@ -1,175 +0,0 @@
1
- 'use strict';
2
-
3
- const AstNode = require('./node'),
4
- Parser = require('..');
5
-
6
- /** 文本节点 */
7
- class AstText extends AstNode {
8
- type = 'text';
9
- /** @type {string} */ data;
10
-
11
- /** 文本长度 */
12
- get length() {
13
- return this.data.length;
14
- }
15
-
16
- /** @param {string} text 包含文本 */
17
- constructor(text = '') {
18
- super();
19
- Object.defineProperties(this, {
20
- data: {value: text, writable: false},
21
- childNodes: {enumerable: false, configurable: false},
22
- type: {enumerable: false, writable: false, configurable: false},
23
- });
24
- }
25
-
26
- /** 输出字符串 */
27
- toString() {
28
- return this.data;
29
- }
30
-
31
- /**
32
- * 修改内容
33
- * @param {string} text 新内容
34
- */
35
- #setData(text) {
36
- text = String(text);
37
- const {data} = this,
38
- e = new Event('text', {bubbles: true});
39
- this.setAttribute('data', text);
40
- if (data !== text) {
41
- this.dispatchEvent(e, {oldText: data, newText: text});
42
- }
43
- return this;
44
- }
45
-
46
- /**
47
- * 替换字符串
48
- * @param {string} text 替换的字符串
49
- */
50
- replaceData(text = '') {
51
- this.#setData(text);
52
- }
53
-
54
- static errorSyntax = /[{}]+|\[{2,}|\[(?!(?:(?!https?\b)[^[])*\])|(?<=^|\])([^[]*?)\]+|<(?=\s*\/?\w+[\s/>])/giu;
55
-
56
- /**
57
- * Linter
58
- * @param {number} start 起始位置
59
- * @returns {LintError[]}
60
- */
61
- lint(start = 0) {
62
- const {data} = this,
63
- errors = [...data.matchAll(AstText.errorSyntax)];
64
- if (errors.length > 0) {
65
- const {top, left} = this.getRootNode().posFromIndex(start);
66
- return errors.map(({0: error, 1: prefix, index}) => {
67
- if (prefix) {
68
- index += prefix.length;
69
- error = error.slice(prefix.length);
70
- }
71
- const lines = data.slice(0, index).split('\n'),
72
- startLine = lines.length + top - 1,
73
- {length} = lines.at(-1),
74
- startCol = lines.length > 1 ? length : left + length;
75
- return {
76
- message: `孤立的"${error[0]}"`,
77
- severity: error[0] === '{' || error[0] === '}' ? 'error' : 'warning',
78
- startLine,
79
- endLine: startLine,
80
- startCol,
81
- endCol: startCol + error.length,
82
- };
83
- });
84
- }
85
- return [];
86
- }
87
-
88
- /** 复制 */
89
- cloneNode() {
90
- return new AstText(this.data);
91
- }
92
-
93
- /**
94
- * @override
95
- * @template {string} T
96
- * @param {T} key 属性键
97
- * @returns {TokenAttribute<T>}
98
- * @throws `Error` 文本节点没有子节点
99
- */
100
- getAttribute(key) {
101
- return key === 'verifyChild'
102
- ? () => {
103
- throw new Error('文本节点没有子节点!');
104
- }
105
- : super.getAttribute(key);
106
- }
107
-
108
- /** @override */
109
- text() {
110
- return this.data;
111
- }
112
-
113
- /**
114
- * 在后方添加字符串
115
- * @param {string} text 添加的字符串
116
- */
117
- appendData(text) {
118
- this.#setData(this.data + text);
119
- }
120
-
121
- /**
122
- * 删减字符串
123
- * @param {number} offset 起始位置
124
- * @param {number} count 删减字符数
125
- */
126
- deleteData(offset, count) {
127
- this.#setData(this.data.slice(0, offset) + this.data.slice(offset + count));
128
- }
129
-
130
- /**
131
- * 插入字符串
132
- * @param {number} offset 插入位置
133
- * @param {string} text 待插入的字符串
134
- */
135
- insertData(offset, text) {
136
- this.#setData(this.data.slice(0, offset) + text + this.data.slice(offset));
137
- }
138
-
139
- /**
140
- * 提取子串
141
- * @param {number} offset 起始位置
142
- * @param {number} count 字符数
143
- */
144
- substringData(offset, count) {
145
- return this.data.slice(offset, offset + count);
146
- }
147
-
148
- /**
149
- * 将文本子节点分裂为两部分
150
- * @param {number} offset 分裂位置
151
- * @throws `RangeError` 错误的断开位置
152
- * @throws `Error` 没有父节点
153
- */
154
- splitText(offset) {
155
- if (!Number.isInteger(offset)) {
156
- this.typeError('splitText', 'Number');
157
- } else if (offset > this.length || offset < -this.length) {
158
- throw new RangeError(`错误的断开位置!${offset}`);
159
- }
160
- const {parentNode, data} = this;
161
- if (!parentNode) {
162
- throw new Error('待分裂的文本节点没有父节点!');
163
- }
164
- const newText = new AstText(data.slice(offset)),
165
- childNodes = [...parentNode.childNodes];
166
- this.setAttribute('data', data.slice(0, offset));
167
- childNodes.splice(childNodes.indexOf(this) + 1, 0, newText);
168
- newText.setAttribute('parentNode', parentNode);
169
- parentNode.setAttribute('childNodes', childNodes);
170
- return newText;
171
- }
172
- }
173
-
174
- Parser.classes.AstText = __filename;
175
- module.exports = AstText;
package/lib/title.js DELETED
@@ -1,70 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..');
4
-
5
- /** MediaWiki页面标题对象 */
6
- class Title {
7
- valid = true;
8
- ns = 0;
9
- title = '';
10
- main = '';
11
- prefix = '';
12
- interwiki = '';
13
- fragment = '';
14
-
15
- /**
16
- * @param {string} title 标题(含或不含命名空间前缀)
17
- * @param {number} defaultNs 命名空间
18
- */
19
- constructor(title, defaultNs = 0, config = Parser.getConfig()) {
20
- const {namespaces, nsid} = config;
21
- let namespace = namespaces[defaultNs];
22
- title = title.replaceAll('_', ' ').trim();
23
- if (title[0] === ':') {
24
- namespace = '';
25
- title = title.slice(1).trim();
26
- }
27
- const iw = defaultNs ? null : Parser.isInterwiki(title, config);
28
- if (iw) {
29
- this.interwiki = iw[1].toLowerCase();
30
- title = title.slice(iw[0].length);
31
- }
32
- const m = title.split(':');
33
- if (m.length > 1) {
34
- const id = namespaces[nsid[m[0].trim().toLowerCase()]];
35
- if (id !== undefined) {
36
- namespace = id;
37
- title = m.slice(1).join(':').trim();
38
- }
39
- }
40
- this.ns = nsid[namespace.toLowerCase()];
41
- const i = title.indexOf('#');
42
- let fragment = '';
43
- if (i !== -1) {
44
- fragment = title.slice(i + 1).trimEnd();
45
- if (fragment.includes('%')) {
46
- try {
47
- fragment = decodeURIComponent(fragment);
48
- } catch {}
49
- } else if (fragment.includes('.')) {
50
- try {
51
- fragment = decodeURIComponent(fragment.replaceAll('.', '%'));
52
- } catch {}
53
- }
54
- title = title.slice(0, i).trim();
55
- }
56
- this.valid = Boolean(title || fragment) && !/\0\d+[eh!+-]\x7F|[<>[\]{}|]/u.test(title);
57
- this.main = title && `${title[0].toUpperCase()}${title.slice(1)}`;
58
- this.prefix = `${namespace}${namespace && ':'}`;
59
- this.title = `${iw ? `${this.interwiki}:` : ''}${this.prefix}${this.main.replaceAll(' ', '_')}`;
60
- this.fragment = fragment;
61
- }
62
-
63
- /** @override */
64
- toString() {
65
- return `${this.title}${this.fragment && '#'}${this.fragment}`;
66
- }
67
- }
68
-
69
- Parser.classes.Title = __filename;
70
- module.exports = Title;
@@ -1,113 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- AttributeToken = require('../src/attribute');
5
-
6
- /**
7
- * 子节点含有AttributeToken的类
8
- * @template T
9
- * @param {T} Constructor 基类
10
- * @param {number} i AttributeToken子节点的位置
11
- * @returns {T}
12
- */
13
- const attributeParent = (Constructor, i = 0) => class extends Constructor {
14
- /**
15
- * getAttr()方法的getter写法
16
- * @returns {Record<string, string|true>}
17
- */
18
- get attributes() {
19
- return this.getAttr();
20
- }
21
-
22
- /** 以字符串表示的class属性 */
23
- get className() {
24
- const attr = this.getAttr('class');
25
- return typeof attr === 'string' ? attr : '';
26
- }
27
-
28
- set className(className) {
29
- this.setAttr('class', className);
30
- }
31
-
32
- /** 以Set表示的class属性 */
33
- get classList() {
34
- return new Set(this.className.split(/\s/u));
35
- }
36
-
37
- /** id属性 */
38
- get id() {
39
- const attr = this.getAttr('id');
40
- return typeof attr === 'string' ? attr : '';
41
- }
42
-
43
- set id(id) {
44
- this.setAttr('id', id);
45
- }
46
-
47
- /**
48
- * AttributeToken子节点是否具有某属性
49
- * @this {{childNodes: AttributeToken[]}}
50
- * @param {string} key 属性键
51
- */
52
- hasAttr(key) {
53
- return this.childNodes.at(i).hasAttr(key);
54
- }
55
-
56
- /**
57
- * 获取AttributeToken子节点的属性
58
- * @this {{childNodes: AttributeToken[]}}
59
- * @template {string|undefined} T
60
- * @param {T} key 属性键
61
- */
62
- getAttr(key) {
63
- return this.childNodes.at(i).getAttr(key);
64
- }
65
-
66
- /**
67
- * 列举AttributeToken子节点的属性键
68
- * @this {{childNodes: AttributeToken[]}}
69
- */
70
- getAttrNames() {
71
- return this.childNodes.at(i).getAttrNames();
72
- }
73
-
74
- /**
75
- * AttributeToken子节点是否具有任意属性
76
- * @this {{childNodes: AttributeToken[]}}
77
- */
78
- hasAttrs() {
79
- return this.childNodes.at(i).hasAttrs();
80
- }
81
-
82
- /**
83
- * 对AttributeToken子节点设置属性
84
- * @this {{childNodes: AttributeToken[]}}
85
- * @param {string} key 属性键
86
- * @param {string|boolean} value 属性值
87
- */
88
- setAttr(key, value) {
89
- return this.childNodes.at(i).setAttr(key, value);
90
- }
91
-
92
- /**
93
- * 移除AttributeToken子节点的某属性
94
- * @this {{childNodes: AttributeToken[]}}
95
- * @param {string} key 属性键
96
- */
97
- removeAttr(key) {
98
- this.childNodes.at(i).removeAttr(key);
99
- }
100
-
101
- /**
102
- * 开关AttributeToken子节点的某属性
103
- * @this {{childNodes: AttributeToken[]}}
104
- * @param {string} key 属性键
105
- * @param {boolean|undefined} force 强制开启或关闭
106
- */
107
- toggleAttr(key, force) {
108
- this.childNodes.at(i).toggleAttr(key, force);
109
- }
110
- };
111
-
112
- Parser.mixins.attributeParent = __filename;
113
- module.exports = attributeParent;
@@ -1,40 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- Token = require('../src');
5
-
6
- /**
7
- * 不可增删子节点的类
8
- * @template T
9
- * @param {T} Constructor 基类
10
- * @returns {T}
11
- */
12
- const fixedToken = Constructor => class extends Constructor {
13
- static fixed = true;
14
-
15
- /**
16
- * 移除子节点
17
- * @throws `Error`
18
- */
19
- removeAt() {
20
- throw new Error(`${this.constructor.name} 不可删除元素!`);
21
- }
22
-
23
- /**
24
- * 插入子节点
25
- * @template {Token} T
26
- * @param {T} token 待插入的子节点
27
- * @param {number} i 插入位置
28
- * @throws `Error`
29
- */
30
- insertAt(token, i = this.childNodes.length) {
31
- if (Parser.running) {
32
- super.insertAt(token, i);
33
- return token;
34
- }
35
- throw new Error(`${this.constructor.name} 不可插入元素!`);
36
- }
37
- };
38
-
39
- Parser.mixins.fixedToken = __filename;
40
- module.exports = fixedToken;
package/mixin/hidden.js DELETED
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..');
4
-
5
- /**
6
- * 解析后不可见的类
7
- * @template T
8
- * @param {T} Constructor 基类
9
- * @returns {T}
10
- */
11
- const hidden = Constructor => class extends Constructor {
12
- static hidden = true;
13
-
14
- /** 没有可见部分 */
15
- text() { // eslint-disable-line class-methods-use-this
16
- return '';
17
- }
18
- };
19
-
20
- Parser.mixins.hidden = __filename;
21
- module.exports = hidden;
package/mixin/sol.js DELETED
@@ -1,68 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- Token = require('../src');
5
-
6
- /**
7
- * 只能位于行首的类
8
- * @template T
9
- * @param {T} Constructor 基类
10
- * @returns {T}
11
- */
12
- const sol = Constructor => class SolToken extends Constructor {
13
- /**
14
- * 是否可以视为root节点
15
- * @this {Token}
16
- * @param {boolean} includeHeading 是否包括HeadingToken
17
- */
18
- #isRoot(includeHeading) {
19
- const {parentNode, type} = this;
20
- return parentNode?.type === 'root'
21
- || parentNode?.type === 'ext-inner' && (includeHeading || type !== 'heading' && parentNode.name === 'poem');
22
- }
23
-
24
- /**
25
- * 在前方插入newline
26
- * @this {SolToken & Token}
27
- */
28
- prependNewLine() {
29
- return (this.previousVisibleSibling || !this.#isRoot()) && String(this.previousVisibleSibling).at(-1) !== '\n'
30
- ? '\n'
31
- : '';
32
- }
33
-
34
- /**
35
- * 在后方插入newline
36
- * @this {SolToken & Token}
37
- */
38
- appendNewLine() {
39
- return (this.nextVisibleSibling || !this.#isRoot(true)) && String(this.nextVisibleSibling ?? '')[0] !== '\n'
40
- ? '\n'
41
- : '';
42
- }
43
-
44
- /**
45
- * 还原为wikitext
46
- * @param {string} selector
47
- * @param {boolean} ownLine 是否独占一行
48
- */
49
- toString(selector, ownLine) {
50
- return `${this.prependNewLine()}${super.toString(selector)}${ownLine ? this.appendNewLine() : ''}`;
51
- }
52
-
53
- /** 获取padding */
54
- getPadding() {
55
- return this.prependNewLine().length;
56
- }
57
-
58
- /**
59
- * 可见部分
60
- * @param {booean} ownLine 是否独占一行
61
- */
62
- text(ownLine) {
63
- return `${this.prependNewLine()}${super.text()}${ownLine ? this.appendNewLine() : ''}`;
64
- }
65
- };
66
-
67
- Parser.mixins.sol = __filename;
68
- module.exports = sol;