wikiparser-node 1.11.0-b → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/config/.schema.json +186 -0
  2. package/config/enwiki.json +814 -1
  3. package/config/llwiki.json +35 -1
  4. package/config/moegirl.json +44 -1
  5. package/config/zhwiki.json +466 -1
  6. package/dist/addon/table.js +486 -0
  7. package/dist/addon/token.js +304 -0
  8. package/dist/addon/transclude.js +184 -0
  9. package/dist/base.d.ts +81 -0
  10. package/dist/base.js +39 -0
  11. package/dist/index.d.ts +33 -0
  12. package/dist/index.js +235 -0
  13. package/dist/internal.d.ts +46 -0
  14. package/dist/lib/element.d.ts +125 -0
  15. package/dist/lib/element.js +375 -0
  16. package/dist/lib/node.d.ts +173 -0
  17. package/dist/lib/node.js +478 -0
  18. package/dist/lib/range.d.ts +105 -0
  19. package/dist/lib/range.js +406 -0
  20. package/dist/lib/ranges.d.ts +28 -0
  21. package/dist/lib/ranges.js +126 -0
  22. package/dist/lib/rect.d.ts +18 -0
  23. package/dist/lib/rect.js +34 -0
  24. package/dist/lib/text.d.ts +58 -0
  25. package/dist/lib/text.js +400 -0
  26. package/dist/lib/title.d.ts +42 -0
  27. package/dist/lib/title.js +208 -0
  28. package/dist/mixin/attributesParent.d.ts +49 -0
  29. package/dist/mixin/attributesParent.js +79 -0
  30. package/dist/mixin/fixed.d.ts +5 -0
  31. package/dist/mixin/fixed.js +32 -0
  32. package/dist/mixin/flagsParent.d.ts +43 -0
  33. package/dist/mixin/flagsParent.js +64 -0
  34. package/dist/mixin/hidden.d.ts +7 -0
  35. package/dist/mixin/hidden.js +37 -0
  36. package/dist/mixin/magicLinkParent.d.ts +19 -0
  37. package/dist/mixin/magicLinkParent.js +43 -0
  38. package/dist/mixin/singleLine.d.ts +5 -0
  39. package/dist/mixin/singleLine.js +25 -0
  40. package/dist/mixin/sol.d.ts +6 -0
  41. package/dist/mixin/sol.js +45 -0
  42. package/dist/mixin/syntax.d.ts +8 -0
  43. package/dist/mixin/syntax.js +46 -0
  44. package/dist/parser/braces.js +150 -0
  45. package/dist/parser/commentAndExt.js +79 -0
  46. package/dist/parser/converter.js +39 -0
  47. package/dist/parser/externalLinks.js +37 -0
  48. package/dist/parser/hrAndDoubleUnderscore.js +42 -0
  49. package/dist/parser/html.js +38 -0
  50. package/dist/parser/links.js +101 -0
  51. package/dist/parser/list.js +114 -0
  52. package/dist/parser/magicLinks.js +52 -0
  53. package/dist/parser/quotes.js +67 -0
  54. package/dist/parser/redirect.js +26 -0
  55. package/dist/parser/selector.js +394 -0
  56. package/dist/parser/table.js +123 -0
  57. package/dist/src/arg.d.ts +51 -0
  58. package/dist/src/arg.js +214 -0
  59. package/dist/src/atom.d.ts +14 -0
  60. package/dist/src/atom.js +52 -0
  61. package/dist/src/attribute.d.ts +64 -0
  62. package/dist/src/attribute.js +480 -0
  63. package/dist/src/attributes.d.ts +101 -0
  64. package/dist/src/attributes.js +381 -0
  65. package/dist/src/converter.d.ts +28 -0
  66. package/dist/src/converter.js +148 -0
  67. package/dist/src/converterFlags.d.ts +80 -0
  68. package/dist/src/converterFlags.js +239 -0
  69. package/dist/src/converterRule.d.ts +71 -0
  70. package/dist/src/converterRule.js +216 -0
  71. package/dist/src/extLink.d.ts +36 -0
  72. package/dist/src/extLink.js +218 -0
  73. package/dist/src/gallery.d.ts +47 -0
  74. package/dist/src/gallery.js +143 -0
  75. package/dist/src/heading.d.ts +40 -0
  76. package/dist/src/heading.js +192 -0
  77. package/dist/src/hidden.d.ts +9 -0
  78. package/dist/src/hidden.js +80 -0
  79. package/dist/src/html.d.ts +61 -0
  80. package/dist/src/html.js +343 -0
  81. package/dist/src/imageParameter.d.ts +60 -0
  82. package/dist/src/imageParameter.js +260 -0
  83. package/dist/src/imagemap.d.ts +47 -0
  84. package/dist/src/imagemap.js +146 -0
  85. package/dist/src/imagemapLink.d.ts +31 -0
  86. package/dist/src/imagemapLink.js +97 -0
  87. package/dist/src/index.d.ts +136 -0
  88. package/dist/src/index.js +834 -0
  89. package/dist/src/link/base.d.ts +52 -0
  90. package/dist/src/link/base.js +256 -0
  91. package/dist/src/link/category.d.ts +22 -0
  92. package/dist/src/link/category.js +34 -0
  93. package/dist/src/link/file.d.ts +102 -0
  94. package/dist/src/link/file.js +333 -0
  95. package/dist/src/link/galleryImage.d.ts +29 -0
  96. package/dist/src/link/galleryImage.js +132 -0
  97. package/dist/src/link/index.d.ts +39 -0
  98. package/dist/src/link/index.js +98 -0
  99. package/dist/src/link/redirectTarget.d.ts +27 -0
  100. package/dist/src/link/redirectTarget.js +69 -0
  101. package/dist/src/magicLink.d.ts +58 -0
  102. package/dist/src/magicLink.js +256 -0
  103. package/dist/src/nested.d.ts +40 -0
  104. package/dist/src/nested.js +106 -0
  105. package/dist/src/nowiki/base.d.ts +28 -0
  106. package/dist/src/nowiki/base.js +89 -0
  107. package/dist/src/nowiki/comment.d.ts +14 -0
  108. package/dist/src/nowiki/comment.js +121 -0
  109. package/dist/src/nowiki/dd.d.ts +8 -0
  110. package/dist/src/nowiki/dd.js +72 -0
  111. package/dist/src/nowiki/doubleUnderscore.d.ts +15 -0
  112. package/dist/src/nowiki/doubleUnderscore.js +97 -0
  113. package/dist/src/nowiki/hr.d.ts +5 -0
  114. package/dist/src/nowiki/hr.js +70 -0
  115. package/dist/src/nowiki/index.d.ts +14 -0
  116. package/dist/src/nowiki/index.js +28 -0
  117. package/dist/src/nowiki/list.d.ts +5 -0
  118. package/dist/src/nowiki/list.js +65 -0
  119. package/dist/src/nowiki/listBase.d.ts +23 -0
  120. package/dist/src/nowiki/listBase.js +99 -0
  121. package/dist/src/nowiki/noinclude.d.ts +6 -0
  122. package/dist/src/nowiki/noinclude.js +75 -0
  123. package/dist/src/nowiki/quote.d.ts +14 -0
  124. package/dist/src/nowiki/quote.js +140 -0
  125. package/dist/src/onlyinclude.d.ts +13 -0
  126. package/dist/src/onlyinclude.js +58 -0
  127. package/dist/src/paramTag/index.d.ts +28 -0
  128. package/dist/src/paramTag/index.js +78 -0
  129. package/dist/src/paramTag/inputbox.d.ts +8 -0
  130. package/dist/src/paramTag/inputbox.js +22 -0
  131. package/dist/src/parameter.d.ts +60 -0
  132. package/dist/src/parameter.js +264 -0
  133. package/dist/src/pre.d.ts +28 -0
  134. package/dist/src/pre.js +60 -0
  135. package/dist/src/redirect.d.ts +30 -0
  136. package/dist/src/redirect.js +126 -0
  137. package/dist/src/syntax.d.ts +17 -0
  138. package/dist/src/syntax.js +87 -0
  139. package/dist/src/table/base.d.ts +28 -0
  140. package/dist/src/table/base.js +81 -0
  141. package/dist/src/table/index.d.ts +230 -0
  142. package/dist/src/table/index.js +506 -0
  143. package/dist/src/table/td.d.ts +72 -0
  144. package/dist/src/table/td.js +370 -0
  145. package/dist/src/table/tr.d.ts +30 -0
  146. package/dist/src/table/tr.js +59 -0
  147. package/dist/src/table/trBase.d.ts +49 -0
  148. package/dist/src/table/trBase.js +164 -0
  149. package/dist/src/tagPair/ext.d.ts +29 -0
  150. package/dist/src/tagPair/ext.js +220 -0
  151. package/dist/src/tagPair/include.d.ts +33 -0
  152. package/dist/src/tagPair/include.js +130 -0
  153. package/dist/src/tagPair/index.d.ts +23 -0
  154. package/dist/src/tagPair/index.js +128 -0
  155. package/dist/src/transclude.d.ts +159 -0
  156. package/dist/src/transclude.js +587 -0
  157. package/dist/util/constants.js +25 -0
  158. package/dist/util/debug.js +95 -0
  159. package/dist/util/diff.js +83 -0
  160. package/dist/util/html.js +143 -0
  161. package/dist/util/lint.js +32 -0
  162. package/dist/util/string.js +87 -0
  163. package/errors/README +3 -0
  164. package/package.json +22 -32
  165. package/printed/README +3 -0
  166. package/bundle/bundle.min.js +0 -38
  167. package/extensions/dist/base.js +0 -64
  168. package/extensions/dist/editor.js +0 -159
  169. package/extensions/dist/highlight.js +0 -56
  170. package/extensions/dist/lint.js +0 -72
  171. package/extensions/editor.css +0 -64
  172. package/extensions/ui.css +0 -144
@@ -0,0 +1,304 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const constants_1 = require("../util/constants");
6
+ const debug_1 = require("../util/debug");
7
+ const string_1 = require("../util/string");
8
+ const index_1 = require("../index");
9
+ const index_2 = require("../src/index");
10
+ const comment_1 = require("../src/nowiki/comment");
11
+ const include_1 = require("../src/tagPair/include");
12
+ const ext_1 = require("../src/tagPair/ext");
13
+ const html_1 = require("../src/html");
14
+ const attributes_1 = require("../src/attributes");
15
+ index_2.Token.prototype.createComment = /** @implements */ function (data = '') {
16
+ const config = this.getAttribute('config');
17
+ // @ts-expect-error abstract class
18
+ return debug_1.Shadow.run(() => new comment_1.CommentToken(data.replaceAll('-->', '-->'), true, config));
19
+ };
20
+ index_2.Token.prototype.createElement = /** @implements */ function (tagName, { selfClosing, closing } = {}) {
21
+ const config = this.getAttribute('config'), include = this.getAttribute('include');
22
+ if (tagName === (include ? 'noinclude' : 'includeonly')) {
23
+ return debug_1.Shadow.run(
24
+ // @ts-expect-error abstract class
25
+ () => new include_1.IncludeToken(tagName, '', undefined, selfClosing ? undefined : tagName, config));
26
+ }
27
+ else if (config.ext.includes(tagName)) {
28
+ // @ts-expect-error abstract class
29
+ return debug_1.Shadow.run(() => new ext_1.ExtToken(tagName, '', undefined, selfClosing ? undefined : '', config));
30
+ }
31
+ else if (config.html.some(tags => tags.includes(tagName))) {
32
+ return debug_1.Shadow.run(() => {
33
+ // @ts-expect-error abstract class
34
+ const attr = new attributes_1.AttributesToken(undefined, 'html-attrs', tagName, config);
35
+ attr.afterBuild();
36
+ // @ts-expect-error abstract class
37
+ return new html_1.HtmlToken(tagName, attr, Boolean(closing), Boolean(selfClosing), config);
38
+ });
39
+ }
40
+ throw new RangeError(`Invalid tag name: ${tagName}`);
41
+ };
42
+ index_2.Token.prototype.caretPositionFromIndex = /** @implements */ function (index) {
43
+ if (index === undefined) {
44
+ return undefined;
45
+ }
46
+ const { length } = this.toString();
47
+ if (index >= length || index < -length) {
48
+ return undefined;
49
+ }
50
+ index += index < 0 ? length : 0;
51
+ let self = this, acc = 0, start = 0;
52
+ while (self.type !== 'text') {
53
+ const { childNodes } = self;
54
+ acc += self.getAttribute('padding');
55
+ for (let i = 0; acc <= index && i < childNodes.length; i++) {
56
+ const cur = childNodes[i], l = cur.toString().length;
57
+ acc += l;
58
+ if (acc > index) {
59
+ self = cur;
60
+ acc -= l;
61
+ start = acc;
62
+ break;
63
+ }
64
+ acc += self.getGaps(i);
65
+ }
66
+ if (self.childNodes === childNodes) {
67
+ return { offsetNode: self, offset: index - start };
68
+ }
69
+ }
70
+ return { offsetNode: self, offset: index - start };
71
+ };
72
+ index_2.Token.prototype.sections = /** @implements */ function () {
73
+ if (this.type !== 'root') {
74
+ return undefined;
75
+ }
76
+ const { childNodes, length } = this, headings = [...childNodes.entries()]
77
+ .filter((entry) => entry[1].type === 'heading')
78
+ .map(([i, { level }]) => [i, level]), lastHeading = [-1, -1, -1, -1, -1, -1], sections = headings.map(([i]) => {
79
+ const range = this.createRange();
80
+ range.setStart(this, i);
81
+ return range;
82
+ });
83
+ for (let i = 0; i < headings.length; i++) {
84
+ const [index, level] = headings[i];
85
+ for (let j = level; j < 6; j++) {
86
+ const last = lastHeading[j];
87
+ if (last >= 0) {
88
+ sections[last].setEnd(this, index);
89
+ }
90
+ lastHeading[j] = j === level ? i : -1;
91
+ }
92
+ }
93
+ for (const last of lastHeading) {
94
+ if (last >= 0) {
95
+ sections[last].setEnd(this, length);
96
+ }
97
+ }
98
+ const range = this.createRange();
99
+ range.setStart(this, 0);
100
+ range.setEnd(this, headings[0]?.[0] ?? length);
101
+ sections.unshift(range);
102
+ return sections;
103
+ };
104
+ index_2.Token.prototype.findEnclosingHtml = /** @implements */ function (tag) {
105
+ tag = tag?.toLowerCase();
106
+ const { html } = this.getAttribute('config'), normalTags = new Set(html[0]), voidTags = new Set(html[2]);
107
+ if (html[2].includes(tag)) {
108
+ throw new RangeError(`Void tag: ${tag}`);
109
+ }
110
+ else if (tag !== undefined && !html.slice(0, 2).some(tags => tags.includes(tag))) {
111
+ throw new RangeError(`Invalid tag name: ${tag}`);
112
+ }
113
+ const { parentNode } = this;
114
+ if (!parentNode) {
115
+ return undefined;
116
+ }
117
+ /**
118
+ * 检查是否为指定的 HTML 标签
119
+ * @param node 节点
120
+ * @param name 标签名
121
+ * @param closing 是否为闭合标签
122
+ */
123
+ const checkHtml = (node, name, closing) => node.is('html')
124
+ && (!name && !voidTags.has(node.name) || node.name === name)
125
+ && (normalTags.has(node.name) || !node.selfClosing)
126
+ && node.closing === closing;
127
+ const { childNodes, length } = parentNode, index = childNodes.indexOf(this);
128
+ let i = index - 1, j = length;
129
+ for (; i >= 0; i--) {
130
+ const open = childNodes[i];
131
+ if (checkHtml(open, tag, false)) {
132
+ for (j = index + 1; j < length; j++) {
133
+ const close = childNodes[j];
134
+ if (checkHtml(close, open.name, true)) {
135
+ break;
136
+ }
137
+ }
138
+ if (j < length) {
139
+ break;
140
+ }
141
+ }
142
+ }
143
+ if (i === -1) {
144
+ return parentNode.findEnclosingHtml(tag);
145
+ }
146
+ const range = this.createRange();
147
+ range.setStart(parentNode, i);
148
+ range.setEnd(parentNode, j + 1);
149
+ return range;
150
+ };
151
+ /**
152
+ * 隐式换行
153
+ * @param str 字符串
154
+ */
155
+ const implicitNewLine = (str) => /^(?:\{\||[:;#*])/u.test(str) ? `\n${str}` : str;
156
+ /**
157
+ * 展开模板
158
+ * @param wikitext
159
+ * @param config
160
+ * @param include
161
+ * @param context 模板调用环境
162
+ * @param accum
163
+ * @throws `Error` not root token
164
+ */
165
+ const expand = (wikitext, config, include, context, accum = []) => {
166
+ const magicWords = new Set(['if', 'ifeq', 'switch']), n = accum.length, token = new index_2.Token(wikitext, config, accum);
167
+ token.type = 'root';
168
+ token.parseOnce(0, include);
169
+ if (context) {
170
+ token.setText((0, string_1.removeComment)(token.firstChild.toString()));
171
+ }
172
+ token.parseOnce();
173
+ for (const plain of [...accum.slice(n), token]) {
174
+ if (plain.length !== 1 || plain.firstChild.type !== 'text') {
175
+ continue;
176
+ }
177
+ const { data } = plain.firstChild;
178
+ if (!/\0\d+t\x7F/u.test(data)) {
179
+ continue;
180
+ }
181
+ const expanded = data.replace(/\0(\d+)t\x7F/gu, (m, i) => {
182
+ const target = accum[i], { type, name, length, firstChild: f } = target;
183
+ if (type === 'arg') {
184
+ const arg = (0, string_1.removeComment)(f.toString()).trim();
185
+ if (context === undefined || /\0\d+t\x7F/u.test(arg)) {
186
+ return m;
187
+ }
188
+ else if (context === false || !context.hasArg(arg)) {
189
+ const effective = target.childNodes[1] ?? target;
190
+ // @ts-expect-error sparse array
191
+ accum[accum.indexOf(effective)] = undefined;
192
+ return effective.toString();
193
+ }
194
+ // @ts-expect-error sparse array
195
+ accum[accum.indexOf(context.getArg(arg).lastChild)] = undefined;
196
+ return context.getValue(arg);
197
+ }
198
+ else if (type === 'template') {
199
+ if (context === false) {
200
+ return m;
201
+ }
202
+ const c = target.getAttribute('config'), { title } = index_1.default.normalizeTitle((0, string_1.removeComment)(f.toString()), 10, include, c, true);
203
+ if (!index_1.default.templates.has(title)) {
204
+ if (index_1.default.templateDir === undefined) {
205
+ return m;
206
+ }
207
+ else if (!path.isAbsolute(index_1.default.templateDir)) {
208
+ index_1.default.templateDir = path.join(__dirname, '..', '..', index_1.default.templateDir);
209
+ }
210
+ const file = ['.wiki', '.txt', ''].map(ext => path.join(index_1.default.templateDir, title + ext))
211
+ .find(fs.existsSync);
212
+ if (!file) {
213
+ return m;
214
+ }
215
+ index_1.default.templates.set(title, fs.readFileSync(file, 'utf8'));
216
+ }
217
+ return implicitNewLine(expand(index_1.default.templates.get(title), config, true, target, accum).toString());
218
+ }
219
+ else if (!magicWords.has(name)) {
220
+ return m;
221
+ }
222
+ else if (length < 3 || name === 'ifeq' && length === 3) {
223
+ return '';
224
+ }
225
+ const c = target.childNodes, var1 = c[1].value, var2 = c[2].value, known = !/\0\d+t\x7F/u.test(var1);
226
+ if (name === 'if' && known) {
227
+ const effective = c[var1 ? 2 : 3];
228
+ if (effective) {
229
+ // @ts-expect-error sparse array
230
+ accum[accum.indexOf(effective.lastChild)] = undefined;
231
+ return implicitNewLine(effective.value);
232
+ }
233
+ return '';
234
+ }
235
+ else if (name === 'ifeq' && known && !/\0\d+t\x7F/u.test(var2)) {
236
+ const effective = c[var1 === var2 ? 3 : 4];
237
+ if (effective) {
238
+ // @ts-expect-error sparse array
239
+ accum[accum.indexOf(effective.lastChild)] = undefined;
240
+ return implicitNewLine(effective.value);
241
+ }
242
+ return '';
243
+ }
244
+ else if (name === 'switch' && known) {
245
+ let defaultVal = '', found = false, transclusion = false, defaultParam;
246
+ for (let j = 2; j < length; j++) {
247
+ const { anon, value, firstChild, lastChild } = c[j], option = (0, string_1.trimPHP)((0, string_1.removeComment)(firstChild.toString()));
248
+ transclusion = /\0\d+t\x7F/u.test(anon ? value : option);
249
+ if (anon) {
250
+ if (j === length - 1) {
251
+ // @ts-expect-error sparse array
252
+ accum[accum.indexOf(lastChild)] = undefined;
253
+ return implicitNewLine(value);
254
+ }
255
+ else if (transclusion) {
256
+ break;
257
+ }
258
+ else {
259
+ found ||= var1 === value;
260
+ }
261
+ }
262
+ else if (transclusion) {
263
+ break;
264
+ }
265
+ else if (found || option === var1) {
266
+ // @ts-expect-error sparse array
267
+ accum[accum.indexOf(lastChild)] = undefined;
268
+ return implicitNewLine(value);
269
+ }
270
+ else if (option.toLowerCase() === '#default') {
271
+ defaultVal = value;
272
+ defaultParam = lastChild;
273
+ }
274
+ if (j === length - 1) {
275
+ if (defaultParam) {
276
+ // @ts-expect-error sparse array
277
+ accum[accum.indexOf(defaultParam)] = undefined;
278
+ }
279
+ return implicitNewLine(defaultVal);
280
+ }
281
+ }
282
+ }
283
+ return m;
284
+ });
285
+ plain.setText(expanded);
286
+ if (plain.type === 'parameter-key') {
287
+ plain.parentNode.trimName((0, string_1.removeComment)(expanded));
288
+ }
289
+ }
290
+ return token;
291
+ };
292
+ index_2.Token.prototype.expand = /** @implements */ function (context) {
293
+ if (this.type !== 'root') {
294
+ throw new Error('Only root token can be expanded!');
295
+ }
296
+ return debug_1.Shadow.run(() => expand(this.toString(), this.getAttribute('config'), this.getAttribute('include'), context).parse());
297
+ };
298
+ index_2.Token.prototype.solveConst = /** @implements */ function () {
299
+ if (this.type !== 'root') {
300
+ throw new Error('Only root token can be expanded!');
301
+ }
302
+ return debug_1.Shadow.run(() => expand(this.toString(), this.getAttribute('config'), this.getAttribute('include'), false).parse());
303
+ };
304
+ constants_1.classes['ExtendedTableToken'] = __filename;
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ /* eslint @stylistic/operator-linebreak: [2, "before", {overrides: {"=": "after"}}] */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const constants_1 = require("../util/constants");
5
+ const debug_1 = require("../util/debug");
6
+ const string_1 = require("../util/string");
7
+ const index_1 = require("../index");
8
+ const index_2 = require("../src/index");
9
+ const transclude_1 = require("../src/transclude");
10
+ const parameter_1 = require("../src/parameter");
11
+ const atom_1 = require("../src/atom");
12
+ transclude_1.TranscludeToken.prototype.newAnonArg =
13
+ /** @implements */
14
+ function (val) {
15
+ const config = this.getAttribute('config'), { childNodes } = index_1.default.parse(val, this.getAttribute('include'), undefined, config),
16
+ // @ts-expect-error abstract class
17
+ token = debug_1.Shadow.run(() => new parameter_1.ParameterToken(undefined, undefined, config));
18
+ token.lastChild.append(...childNodes);
19
+ token.afterBuild();
20
+ return this.insertAt(token);
21
+ };
22
+ transclude_1.TranscludeToken.prototype.setValue =
23
+ /** @implements */
24
+ function (key, value) {
25
+ if (!this.isTemplate()) {
26
+ throw new Error('TranscludeToken.setValue method is only for templates!');
27
+ }
28
+ const arg = this.getArg(key);
29
+ if (arg) {
30
+ arg.setValue(value);
31
+ return;
32
+ }
33
+ const include = this.getAttribute('include'), config = this.getAttribute('config'), k = index_1.default.parse(key, include, undefined, config), v = index_1.default.parse(value, include, undefined, config),
34
+ // @ts-expect-error abstract class
35
+ token = debug_1.Shadow.run(() => new parameter_1.ParameterToken(undefined, undefined, config));
36
+ token.firstChild.append(...k.childNodes);
37
+ token.lastChild.append(...v.childNodes);
38
+ token.afterBuild();
39
+ this.insertAt(token);
40
+ };
41
+ transclude_1.TranscludeToken.prototype.replaceTemplate =
42
+ /** @implements */
43
+ function (title) {
44
+ if (this.type === 'magic-word') {
45
+ throw new Error('TranscludeToken.replaceTemplate method is only for templates!');
46
+ }
47
+ const { childNodes } = index_1.default.parse(title, this.getAttribute('include'), 2, this.getAttribute('config'));
48
+ this.firstChild.replaceChildren(...childNodes);
49
+ };
50
+ transclude_1.TranscludeToken.prototype.replaceModule =
51
+ /** @implements */
52
+ function (title) {
53
+ if (this.type !== 'magic-word' || this.name !== 'invoke') {
54
+ throw new Error('TranscludeToken.replaceModule method is only for modules!');
55
+ }
56
+ const config = this.getAttribute('config');
57
+ if (this.length === 1) {
58
+ index_2.Token.prototype.insertAt.call(this, debug_1.Shadow.run(() => new atom_1.AtomToken(undefined, 'invoke-module', config, [], {
59
+ 'Stage-1': ':', '!ExtToken': '',
60
+ })));
61
+ return;
62
+ }
63
+ const { childNodes } = index_1.default.parse(title, this.getAttribute('include'), 2, config);
64
+ this.childNodes[1].replaceChildren(...childNodes);
65
+ };
66
+ transclude_1.TranscludeToken.prototype.replaceFunction =
67
+ /** @implements */
68
+ function (func) {
69
+ if (this.type !== 'magic-word' || this.name !== 'invoke') {
70
+ throw new Error('TranscludeToken.replaceModule method is only for modules!');
71
+ }
72
+ else if (this.length < 2) {
73
+ throw new Error('No module name specified!');
74
+ }
75
+ const config = this.getAttribute('config');
76
+ if (this.length === 2) {
77
+ index_2.Token.prototype.insertAt.call(this, debug_1.Shadow.run(() => new atom_1.AtomToken(undefined, 'invoke-function', config, [], {
78
+ 'Stage-1': ':', '!ExtToken': '',
79
+ })));
80
+ return;
81
+ }
82
+ const { childNodes } = index_1.default.parse(func, this.getAttribute('include'), 2, config);
83
+ this.childNodes[2].replaceChildren(...childNodes);
84
+ };
85
+ transclude_1.TranscludeToken.prototype.fixDuplication =
86
+ /** @implements */
87
+ function (aggressive) {
88
+ if (!this.hasDuplicatedArgs()) {
89
+ return [];
90
+ }
91
+ const duplicatedKeys = [];
92
+ let anonCount = this.getAnonArgs().length;
93
+ for (const [key, args] of this.getDuplicatedArgs()) {
94
+ if (args.length <= 1) {
95
+ continue;
96
+ }
97
+ const values = new Map();
98
+ for (const arg of args) {
99
+ const val = arg.getValue().trim();
100
+ if (values.has(val)) {
101
+ values.get(val).push(arg);
102
+ }
103
+ else {
104
+ values.set(val, [arg]);
105
+ }
106
+ }
107
+ let noMoreAnon = anonCount === 0 || !key.trim() || isNaN(key);
108
+ const emptyArgs = values.get('') ?? [], duplicatedArgs = [...values].filter(([val, { length }]) => val && length > 1).flatMap(([, curArgs]) => {
109
+ const anonIndex = noMoreAnon ? -1 : curArgs.findIndex(({ anon }) => anon);
110
+ if (anonIndex !== -1) {
111
+ noMoreAnon = true;
112
+ }
113
+ curArgs.splice(anonIndex, 1);
114
+ return curArgs;
115
+ }), badArgs = [...emptyArgs, ...duplicatedArgs], index = noMoreAnon ? -1 : emptyArgs.findIndex(({ anon }) => anon);
116
+ if (badArgs.length === args.length) {
117
+ badArgs.splice(index, 1);
118
+ }
119
+ else if (index !== -1) {
120
+ this.anonToNamed();
121
+ anonCount = 0;
122
+ }
123
+ for (const arg of badArgs) {
124
+ arg.remove();
125
+ }
126
+ let remaining = args.length - badArgs.length;
127
+ if (remaining === 1) {
128
+ continue;
129
+ }
130
+ else if (aggressive && (anonCount ? /\D\d+$/u : /(?:^|\D)\d+$/u).test(key)) {
131
+ let last;
132
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
133
+ /^a\d+$/u;
134
+ const str = key.slice(0, -/(?<!\d)\d+$/u.exec(key)[0].length), regex = new RegExp(String.raw `^${(0, string_1.escapeRegExp)(str)}\d+$`, 'u'), series = this.getAllArgs().filter(({ name }) => regex.test(name)), ordered = series.every(({ name }, i) => {
135
+ const j = Number(name.slice(str.length)), cmp = j <= i + 1 && (i === 0 || j >= last || name === key);
136
+ last = j;
137
+ return cmp;
138
+ });
139
+ if (ordered) {
140
+ for (let i = 0; i < series.length; i++) {
141
+ const name = `${str}${i + 1}`, arg = series[i];
142
+ if (arg.name !== name) {
143
+ if (arg.name === key) {
144
+ remaining--;
145
+ }
146
+ arg.rename(name, true);
147
+ }
148
+ }
149
+ }
150
+ }
151
+ if (remaining > 1) {
152
+ index_1.default.error(`${this.type === 'template'
153
+ ? this.name
154
+ : this.normalizeTitle(this.childNodes[1].toString(true), 828)
155
+ .title} still has ${remaining} duplicated ${key} parameters:\n${[...this.getArgs(key)].map(arg => {
156
+ const { top, left } = arg.getBoundingClientRect();
157
+ return `Line ${String(top)} Column ${String(left)}`;
158
+ }).join('\n')}`);
159
+ duplicatedKeys.push(key);
160
+ continue;
161
+ }
162
+ }
163
+ return duplicatedKeys;
164
+ };
165
+ transclude_1.TranscludeToken.prototype.escapeTables =
166
+ /** @implements */
167
+ function () {
168
+ if (!/\n[^\S\n]*(?::+[^\S\n]*)?\{\|/u.test(this.text())) {
169
+ return this;
170
+ }
171
+ const stripped = this.toString().slice(2, -2), include = this.getAttribute('include'), config = this.getAttribute('config'), parsed = index_1.default.parse(stripped, include, 4, config);
172
+ for (const table of parsed.childNodes) {
173
+ if (table.is('table')) {
174
+ table.escape();
175
+ }
176
+ }
177
+ const { firstChild, length } = index_1.default.parse(`{{${parsed.toString()}}}`, include, undefined, config);
178
+ if (length !== 1 || !(firstChild instanceof transclude_1.TranscludeToken)) {
179
+ throw new Error('Failed to escape tables!');
180
+ }
181
+ this.safeReplaceWith(firstChild);
182
+ return firstChild;
183
+ };
184
+ constants_1.classes['ExtendedTranscludeToken'] = __filename;
package/dist/base.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ export interface Config {
2
+ ext: string[];
3
+ readonly html: [string[], string[], string[]];
4
+ readonly namespaces: Record<string, string>;
5
+ readonly nsid: Record<string, number>;
6
+ readonly parserFunction: [Record<string, string>, string[], string[], string[]];
7
+ readonly doubleUnderscore: [string[], string[]];
8
+ readonly protocol: string;
9
+ readonly img: Record<string, string>;
10
+ readonly redirection: string[];
11
+ readonly variants: string[];
12
+ readonly excludes?: string[];
13
+ readonly interwiki: string[];
14
+ readonly conversionTable?: [string, string][];
15
+ readonly redirects?: [string, string][];
16
+ readonly articlePath?: string;
17
+ }
18
+ export type TokenTypes = 'root' | 'plain' | 'redirect' | 'redirect-syntax' | 'redirect-target' | 'onlyinclude' | 'noinclude' | 'include' | 'comment' | 'ext' | 'ext-attrs' | 'ext-attr-dirty' | 'ext-attr' | 'attr-key' | 'attr-value' | 'ext-inner' | 'arg' | 'arg-name' | 'arg-default' | 'hidden' | 'magic-word' | 'magic-word-name' | 'invoke-function' | 'invoke-module' | 'template' | 'template-name' | 'parameter' | 'parameter-key' | 'parameter-value' | 'heading' | 'heading-title' | 'heading-trail' | 'html' | 'html-attrs' | 'html-attr-dirty' | 'html-attr' | 'table' | 'tr' | 'td' | 'table-syntax' | 'table-attrs' | 'table-attr-dirty' | 'table-attr' | 'table-inter' | 'td-inner' | 'hr' | 'double-underscore' | 'link' | 'link-target' | 'link-text' | 'category' | 'file' | 'gallery-image' | 'imagemap-image' | 'image-parameter' | 'quote' | 'ext-link' | 'ext-link-text' | 'ext-link-url' | 'free-ext-link' | 'magic-link' | 'list' | 'dd' | 'list-range' | 'converter' | 'converter-flags' | 'converter-flag' | 'converter-rule' | 'converter-rule-variant' | 'converter-rule-to' | 'converter-rule-from' | 'param-line' | 'imagemap-link';
19
+ export declare const rules: readonly ["bold-header", "format-leakage", "fostered-content", "h1", "illegal-attr", "insecure-style", "invalid-gallery", "invalid-imagemap", "invalid-invoke", "invalid-isbn", "lonely-apos", "lonely-bracket", "lonely-http", "nested-link", "no-arg", "no-duplicate", "no-ignored", "obsolete-attr", "obsolete-tag", "parsing-order", "pipe-like", "table-layout", "tag-like", "unbalanced-header", "unclosed-comment", "unclosed-quote", "unclosed-table", "unescaped", "unknown-page", "unmatched-tag", "unterminated-url", "url-encoding", "var-anchor", "void-ext"];
20
+ export declare namespace LintError {
21
+ type Severity = 'error' | 'warning';
22
+ type Rule = typeof rules[number];
23
+ interface Fix {
24
+ readonly range: [number, number];
25
+ text: string;
26
+ }
27
+ }
28
+ export interface LintError {
29
+ rule: LintError.Rule;
30
+ message: string;
31
+ severity: LintError.Severity;
32
+ startIndex: number;
33
+ endIndex: number;
34
+ startLine: number;
35
+ startCol: number;
36
+ endLine: number;
37
+ endCol: number;
38
+ fix?: LintError.Fix;
39
+ suggestions?: (LintError.Fix & {
40
+ desc: string;
41
+ })[];
42
+ }
43
+ export type AST = Record<string, string | number | boolean> & {
44
+ range: [number, number];
45
+ type?: TokenTypes;
46
+ childNodes?: AST[];
47
+ };
48
+ /** 类似Node */
49
+ export interface AstNode {
50
+ readonly childNodes: readonly AstNode[];
51
+ /** 节点类型 */
52
+ type: string;
53
+ /** Linter */
54
+ lint(): LintError[];
55
+ /** 以HTML格式打印 */
56
+ print(): string;
57
+ }
58
+ /** 所有节点的基类 */
59
+ interface Token extends AstNode {
60
+ readonly name?: string;
61
+ /**
62
+ * 符合选择器的所有后代节点
63
+ * @param selector 选择器
64
+ */
65
+ querySelectorAll<T = Token>(selector: string): T[];
66
+ /** 保存为JSON */
67
+ json(): AST;
68
+ }
69
+ export interface Parser {
70
+ config: Config | string;
71
+ i18n: Record<string, string> | string | undefined;
72
+ /** 获取当前的解析设置 */
73
+ getConfig(): Config;
74
+ /**
75
+ * 解析wikitext
76
+ * @param include 是否嵌入
77
+ * @param maxStage 最大解析层级
78
+ */
79
+ parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): Token;
80
+ }
81
+ export {};
package/dist/base.js ADDED
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rules = void 0;
4
+ exports.rules = [
5
+ 'bold-header',
6
+ 'format-leakage',
7
+ 'fostered-content',
8
+ 'h1',
9
+ 'illegal-attr',
10
+ 'insecure-style',
11
+ 'invalid-gallery',
12
+ 'invalid-imagemap',
13
+ 'invalid-invoke',
14
+ 'invalid-isbn',
15
+ 'lonely-apos',
16
+ 'lonely-bracket',
17
+ 'lonely-http',
18
+ 'nested-link',
19
+ 'no-arg',
20
+ 'no-duplicate',
21
+ 'no-ignored',
22
+ 'obsolete-attr',
23
+ 'obsolete-tag',
24
+ 'parsing-order',
25
+ 'pipe-like',
26
+ 'table-layout',
27
+ 'tag-like',
28
+ 'unbalanced-header',
29
+ 'unclosed-comment',
30
+ 'unclosed-quote',
31
+ 'unclosed-table',
32
+ 'unescaped',
33
+ 'unknown-page',
34
+ 'unmatched-tag',
35
+ 'unterminated-url',
36
+ 'url-encoding',
37
+ 'var-anchor',
38
+ 'void-ext',
39
+ ];
@@ -0,0 +1,33 @@
1
+ import type { Config, LintError, Parser as ParserBase } from './base';
2
+ import type { Title } from './lib/title';
3
+ import type { Token } from './internal';
4
+ declare interface Parser extends ParserBase {
5
+ rules: readonly LintError.Rule[];
6
+ viewOnly: boolean;
7
+ conversionTable: Map<string, string>;
8
+ redirects: Map<string, string>;
9
+ templateDir?: string;
10
+ templates: Map<string, string>;
11
+ warning: boolean;
12
+ debugging: boolean;
13
+ /**
14
+ * 规范化页面标题
15
+ * @param title 标题(含或不含命名空间前缀)
16
+ * @param defaultNs 命名空间
17
+ * @param include 是否嵌入
18
+ */
19
+ normalizeTitle(title: string, defaultNs?: number, include?: boolean, config?: Config): Title;
20
+ parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): Token;
21
+ /**
22
+ * 是否是跨维基链接
23
+ * @param title 链接标题
24
+ */
25
+ isInterwiki(title: string, config?: Config): RegExpExecArray | null;
26
+ }
27
+ declare const Parser: Parser;
28
+ // @ts-expect-error mixed export styles
29
+ export = Parser;
30
+ export default Parser;
31
+ export type { Config, LintError };
32
+ export type * from './internal';
33
+ declare global { type Acceptable = unknown; }