wikiparser-node 1.2.0-b → 1.2.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 (154) hide show
  1. package/config/.schema.json +172 -0
  2. package/config/llwiki.json +35 -1
  3. package/config/moegirl.json +44 -1
  4. package/config/zhwiki.json +466 -1
  5. package/dist/addon/table.d.ts +6 -0
  6. package/dist/addon/table.js +564 -0
  7. package/dist/base.d.ts +46 -0
  8. package/dist/bin/toc.js +18 -0
  9. package/dist/index.d.ts +31 -0
  10. package/dist/index.js +209 -0
  11. package/dist/internal.d.ts +44 -0
  12. package/dist/lib/element.d.ts +155 -0
  13. package/dist/lib/element.js +654 -0
  14. package/dist/lib/node.d.ts +146 -0
  15. package/dist/lib/node.js +419 -0
  16. package/dist/lib/range.d.ts +104 -0
  17. package/dist/lib/range.js +385 -0
  18. package/dist/lib/ranges.d.ts +26 -0
  19. package/dist/lib/ranges.js +117 -0
  20. package/dist/lib/text.d.ts +62 -0
  21. package/dist/lib/text.js +235 -0
  22. package/dist/lib/title.d.ts +38 -0
  23. package/dist/lib/title.js +162 -0
  24. package/dist/mixin/attributesParent.js +94 -0
  25. package/dist/mixin/fixed.js +32 -0
  26. package/dist/mixin/flagsParent.js +70 -0
  27. package/dist/mixin/hidden.js +22 -0
  28. package/dist/mixin/magicLinkParent.js +41 -0
  29. package/dist/mixin/singleLine.js +25 -0
  30. package/dist/mixin/sol.js +43 -0
  31. package/dist/mixin/syntax.js +56 -0
  32. package/dist/parser/braces.js +123 -0
  33. package/dist/parser/commentAndExt.js +69 -0
  34. package/dist/parser/converter.js +40 -0
  35. package/dist/parser/externalLinks.js +28 -0
  36. package/dist/parser/hrAndDoubleUnderscore.js +38 -0
  37. package/dist/parser/html.js +36 -0
  38. package/dist/parser/links.js +94 -0
  39. package/dist/parser/list.js +59 -0
  40. package/dist/parser/magicLinks.js +40 -0
  41. package/dist/parser/quotes.js +67 -0
  42. package/dist/parser/selector.js +162 -0
  43. package/dist/parser/table.js +112 -0
  44. package/dist/src/arg.d.ts +58 -0
  45. package/dist/src/arg.js +190 -0
  46. package/dist/src/atom.d.ts +12 -0
  47. package/dist/src/atom.js +27 -0
  48. package/dist/src/attribute.d.ts +86 -0
  49. package/dist/src/attribute.js +392 -0
  50. package/dist/src/attributes.d.ts +107 -0
  51. package/dist/src/attributes.js +344 -0
  52. package/dist/src/converter.d.ts +51 -0
  53. package/dist/src/converter.js +79 -0
  54. package/dist/src/converterFlags.d.ts +87 -0
  55. package/dist/src/converterFlags.js +223 -0
  56. package/dist/src/converterRule.d.ts +81 -0
  57. package/dist/src/converterRule.js +212 -0
  58. package/dist/src/extLink.d.ts +53 -0
  59. package/dist/src/extLink.js +121 -0
  60. package/dist/src/gallery.d.ts +55 -0
  61. package/dist/src/gallery.js +123 -0
  62. package/dist/src/heading.d.ts +72 -0
  63. package/dist/src/heading.js +128 -0
  64. package/dist/src/hidden.d.ts +21 -0
  65. package/dist/src/hidden.js +23 -0
  66. package/dist/src/html.d.ts +103 -0
  67. package/dist/src/html.js +230 -0
  68. package/dist/src/imageParameter.d.ts +66 -0
  69. package/dist/src/imageParameter.js +247 -0
  70. package/dist/src/imagemap.d.ts +57 -0
  71. package/dist/src/imagemap.js +148 -0
  72. package/dist/src/imagemapLink.d.ts +57 -0
  73. package/dist/src/imagemapLink.js +40 -0
  74. package/dist/src/index.d.ts +146 -0
  75. package/dist/src/index.js +777 -0
  76. package/dist/src/link/base.d.ts +52 -0
  77. package/dist/src/link/base.js +207 -0
  78. package/dist/src/link/category.d.ts +13 -0
  79. package/dist/src/link/category.js +29 -0
  80. package/dist/src/link/file.d.ts +96 -0
  81. package/dist/src/link/file.js +253 -0
  82. package/dist/src/link/galleryImage.d.ts +43 -0
  83. package/dist/src/link/galleryImage.js +106 -0
  84. package/dist/src/link/index.d.ts +53 -0
  85. package/dist/src/link/index.js +123 -0
  86. package/dist/src/magicLink.d.ts +69 -0
  87. package/dist/src/magicLink.js +148 -0
  88. package/dist/src/nested.d.ts +44 -0
  89. package/dist/src/nested.js +87 -0
  90. package/dist/src/nowiki/base.d.ts +46 -0
  91. package/dist/src/nowiki/base.js +42 -0
  92. package/dist/src/nowiki/comment.d.ts +39 -0
  93. package/dist/src/nowiki/comment.js +67 -0
  94. package/dist/src/nowiki/dd.d.ts +8 -0
  95. package/dist/src/nowiki/dd.js +25 -0
  96. package/dist/src/nowiki/doubleUnderscore.d.ts +43 -0
  97. package/dist/src/nowiki/doubleUnderscore.js +50 -0
  98. package/dist/src/nowiki/hr.d.ts +29 -0
  99. package/dist/src/nowiki/hr.js +14 -0
  100. package/dist/src/nowiki/index.d.ts +16 -0
  101. package/dist/src/nowiki/index.js +21 -0
  102. package/dist/src/nowiki/list.d.ts +29 -0
  103. package/dist/src/nowiki/list.js +48 -0
  104. package/dist/src/nowiki/listBase.d.ts +18 -0
  105. package/dist/src/nowiki/listBase.js +12 -0
  106. package/dist/src/nowiki/noinclude.d.ts +25 -0
  107. package/dist/src/nowiki/noinclude.js +25 -0
  108. package/dist/src/nowiki/quote.d.ts +22 -0
  109. package/dist/src/nowiki/quote.js +50 -0
  110. package/dist/src/onlyinclude.d.ts +16 -0
  111. package/dist/src/onlyinclude.js +57 -0
  112. package/dist/src/paramTag/index.d.ts +37 -0
  113. package/dist/src/paramTag/index.js +68 -0
  114. package/dist/src/paramTag/inputbox.d.ts +8 -0
  115. package/dist/src/paramTag/inputbox.js +23 -0
  116. package/dist/src/parameter.d.ts +84 -0
  117. package/dist/src/parameter.js +206 -0
  118. package/dist/src/pre.d.ts +28 -0
  119. package/dist/src/pre.js +51 -0
  120. package/dist/src/syntax.d.ts +24 -0
  121. package/dist/src/syntax.js +30 -0
  122. package/dist/src/table/base.d.ts +52 -0
  123. package/dist/src/table/base.js +78 -0
  124. package/dist/src/table/index.d.ts +229 -0
  125. package/dist/src/table/index.js +380 -0
  126. package/dist/src/table/td.d.ts +103 -0
  127. package/dist/src/table/td.js +261 -0
  128. package/dist/src/table/tr.d.ts +32 -0
  129. package/dist/src/table/tr.js +56 -0
  130. package/dist/src/table/trBase.d.ts +54 -0
  131. package/dist/src/table/trBase.js +159 -0
  132. package/dist/src/tagPair/ext.d.ts +58 -0
  133. package/dist/src/tagPair/ext.js +142 -0
  134. package/dist/src/tagPair/include.d.ts +52 -0
  135. package/dist/src/tagPair/include.js +58 -0
  136. package/dist/src/tagPair/index.d.ts +47 -0
  137. package/dist/src/tagPair/index.js +93 -0
  138. package/dist/src/transclude.d.ts +167 -0
  139. package/dist/src/transclude.js +689 -0
  140. package/dist/util/constants.js +108 -0
  141. package/dist/util/debug.js +64 -0
  142. package/dist/util/diff.js +72 -0
  143. package/dist/util/lint.js +48 -0
  144. package/dist/util/string.js +61 -0
  145. package/errors/README +1 -0
  146. package/package.json +12 -32
  147. package/printed/README +1 -0
  148. package/bundle/bundle.min.js +0 -36
  149. package/extensions/dist/base.js +0 -68
  150. package/extensions/dist/editor.js +0 -159
  151. package/extensions/dist/highlight.js +0 -30
  152. package/extensions/dist/lint.js +0 -48
  153. package/extensions/editor.css +0 -63
  154. package/extensions/ui.css +0 -117
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseQuotes = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const Parser = require("../index");
6
+ const quote_1 = require("../src/nowiki/quote");
7
+ /**
8
+ * 解析单引号
9
+ * @param wikitext
10
+ * @param config
11
+ * @param accum
12
+ */
13
+ const parseQuotes = (wikitext, config = Parser.getConfig(), accum = []) => {
14
+ const arr = wikitext.split(/('{2,})/u), { length } = arr;
15
+ if (length === 1) {
16
+ return wikitext;
17
+ }
18
+ let nBold = 0, nItalic = 0, firstSingle, firstMulti, firstSpace;
19
+ for (let i = 1; i < length; i += 2) {
20
+ const { length: len } = arr[i];
21
+ switch (len) {
22
+ case 2:
23
+ nItalic++;
24
+ break;
25
+ case 4:
26
+ arr[i - 1] += `'`;
27
+ arr[i] = `'''`;
28
+ // fall through
29
+ case 3:
30
+ nBold++;
31
+ if (firstSingle !== undefined) {
32
+ break;
33
+ }
34
+ else if (arr[i - 1].endsWith(' ')) {
35
+ if (firstMulti === undefined && firstSpace === undefined) {
36
+ firstSpace = i;
37
+ }
38
+ }
39
+ else if (arr[i - 1].at(-2) === ' ') {
40
+ firstSingle = i;
41
+ }
42
+ else {
43
+ firstMulti ??= i;
44
+ }
45
+ break;
46
+ default:
47
+ arr[i - 1] += `'`.repeat(len - 5);
48
+ arr[i] = `'''''`;
49
+ nItalic++;
50
+ nBold++;
51
+ }
52
+ }
53
+ if (nItalic % 2 === 1 && nBold % 2 === 1) {
54
+ const i = firstSingle ?? firstMulti ?? firstSpace;
55
+ if (i !== undefined) {
56
+ arr[i] = `''`;
57
+ arr[i - 1] += `'`;
58
+ }
59
+ }
60
+ for (let i = 1; i < length; i += 2) {
61
+ new quote_1.QuoteToken(arr[i], config, accum);
62
+ arr[i] = `\0${accum.length - 1}q\x7F`;
63
+ }
64
+ return arr.join('');
65
+ };
66
+ exports.parseQuotes = parseQuotes;
67
+ constants_1.parsers['parseQuotes'] = __filename;
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseSelector = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const Parser = require("../index");
6
+ const simplePseudos = new Set([
7
+ 'root',
8
+ 'first-child',
9
+ 'first-of-type',
10
+ 'last-child',
11
+ 'last-of-type',
12
+ 'only-child',
13
+ 'only-of-type',
14
+ 'empty',
15
+ 'parent',
16
+ 'header',
17
+ 'hidden',
18
+ 'visible',
19
+ 'only-whitespace',
20
+ 'any-link',
21
+ 'local-link',
22
+ 'invalid',
23
+ 'required',
24
+ 'optional',
25
+ ]);
26
+ const complexPseudos = [
27
+ 'is',
28
+ 'not',
29
+ 'nth-child',
30
+ 'nth-of-type',
31
+ 'nth-last-child',
32
+ 'nth-last-of-type',
33
+ 'contains',
34
+ 'has',
35
+ 'lang',
36
+ 'regex',
37
+ ];
38
+ const specialChars = [
39
+ ['[', '&lbrack;'],
40
+ [']', '&rbrack;'],
41
+ ['(', '&lpar;'],
42
+ [')', '&rpar;'],
43
+ ['"', '&quot;'],
44
+ [`'`, '&apos;'],
45
+ [':', '&colon;'],
46
+ ['\\', '&bsol;'],
47
+ ['&', '&amp;'],
48
+ ];
49
+ const pseudoRegex = new RegExp(`:(${complexPseudos.join('|')})$`, 'u'), regularRegex = /[[(,>+~]|\s+/u, attributeRegex = /^\s*(\w+)\s*(?:([~|^$*!]?=)\s*("[^"]*"|'[^']*'|[^\s[\]]+)(?:\s+(i))?\s*)?\]/u, functionRegex = /^(\s*"[^"]*"\s*|\s*'[^']*'\s*|[^()]*)\)/u, grouping = new Set([',', '>', '+', '~']), combinator = new Set(['>', '+', '~', '']);
50
+ /**
51
+ * 清理转义符号
52
+ * @param selector
53
+ */
54
+ const sanitize = (selector) => {
55
+ for (const [c, escaped] of specialChars) {
56
+ selector = selector.replaceAll(`\\${c}`, escaped);
57
+ }
58
+ return selector;
59
+ };
60
+ /**
61
+ * 还原转义符号
62
+ * @param selector
63
+ */
64
+ const desanitize = (selector) => {
65
+ for (const [c, escaped] of specialChars) {
66
+ selector = selector.replaceAll(escaped, c);
67
+ }
68
+ return selector.trim();
69
+ };
70
+ /**
71
+ * 去除首尾的引号
72
+ * @param val 属性值或伪选择器函数的参数
73
+ */
74
+ const deQuote = (val) => /^(["']).*\1$/u.test(val) ? val.slice(1, -1) : val.trim();
75
+ /**
76
+ * 解析简单伪选择器
77
+ * @param step 当前顶部
78
+ * @param str 不含属性和复杂伪选择器的语句
79
+ * @throws `SyntaxError` 非法的选择器
80
+ */
81
+ const pushSimple = (step, str) => {
82
+ const pieces = str.trim().split(':'),
83
+ // eslint-disable-next-line unicorn/explicit-length-check
84
+ i = pieces.slice(1).findIndex(pseudo => simplePseudos.has(pseudo)) + 1 || pieces.length;
85
+ if (pieces.slice(i).some(pseudo => !simplePseudos.has(pseudo))) {
86
+ throw new SyntaxError(`非法的选择器!\n${str}\n可能需要将':'转义为'\\:'。`);
87
+ }
88
+ step.push(desanitize(pieces.slice(0, i).join(':')), ...pieces.slice(i).map(piece => `:${piece}`));
89
+ };
90
+ /**
91
+ * 解析选择器
92
+ * @param selector
93
+ * @throws `SyntaxError` 非法的选择器
94
+ */
95
+ const parseSelector = (selector) => {
96
+ selector = selector.trim();
97
+ const stack = [[[]]];
98
+ let sanitized = sanitize(selector), regex = regularRegex, mt = regex.exec(sanitized), [condition] = stack, [step] = condition;
99
+ while (mt) {
100
+ let { 0: syntax, index } = mt;
101
+ if (syntax.trim() === '') {
102
+ index += syntax.length;
103
+ const char = sanitized[index];
104
+ syntax = grouping.has(char) ? char : '';
105
+ }
106
+ if (syntax === ',') { // 情形1:并列
107
+ pushSimple(step, sanitized.slice(0, index));
108
+ condition = [[]];
109
+ [step] = condition;
110
+ stack.push(condition);
111
+ }
112
+ else if (combinator.has(syntax)) { // 情形2:关系
113
+ pushSimple(step, sanitized.slice(0, index));
114
+ if (!step.some(Boolean)) {
115
+ throw new SyntaxError(`非法的选择器!\n${selector}\n可能需要通用选择器'*'。`);
116
+ }
117
+ step.relation = syntax;
118
+ step = [];
119
+ condition.push(step);
120
+ }
121
+ else if (syntax === '[') { // 情形3:属性开启
122
+ pushSimple(step, sanitized.slice(0, index));
123
+ regex = attributeRegex;
124
+ }
125
+ else if (syntax.endsWith(']')) { // 情形4:属性闭合
126
+ mt[3] &&= desanitize(deQuote(mt[3]));
127
+ step.push(mt.slice(1));
128
+ regex = regularRegex;
129
+ }
130
+ else if (syntax === '(') { // 情形5:伪选择器开启
131
+ const pseudoExec = pseudoRegex.exec(sanitized.slice(0, index));
132
+ if (!pseudoExec) {
133
+ throw new SyntaxError(`非法的选择器!\n${desanitize(sanitized)}\n请检查伪选择器是否存在。`);
134
+ }
135
+ pushSimple(step, sanitized.slice(0, pseudoExec.index));
136
+ step.push(pseudoExec[1]); // 临时存放复杂伪选择器
137
+ regex = functionRegex;
138
+ }
139
+ else { // 情形6:伪选择器闭合
140
+ mt.push(step.pop());
141
+ mt[1] &&= deQuote(mt[1]);
142
+ step.push(mt.slice(1));
143
+ regex = regularRegex;
144
+ }
145
+ sanitized = sanitized.slice(index + syntax.length);
146
+ if (grouping.has(syntax)) {
147
+ sanitized = sanitized.trim();
148
+ }
149
+ mt = regex.exec(sanitized);
150
+ }
151
+ if (regex === regularRegex) {
152
+ pushSimple(step, sanitized);
153
+ const pseudos = new Set(stack.flat(2).filter((e) => typeof e === 'string' && e.startsWith(':')));
154
+ if (pseudos.size > 0) {
155
+ Parser.warn('检测到伪选择器,请确认是否需要将":"转义成"\\:"。', pseudos);
156
+ }
157
+ return stack;
158
+ }
159
+ throw new SyntaxError(`非法的选择器!\n${selector}\n检测到未闭合的'${regex === attributeRegex ? '[' : '('}'`);
160
+ };
161
+ exports.parseSelector = parseSelector;
162
+ constants_1.parsers['parseSelector'] = __filename;
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseTable = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const Parser = require("../index");
6
+ const index_1 = require("../src/index");
7
+ const index_2 = require("../src/table/index");
8
+ const tr_1 = require("../src/table/tr");
9
+ const td_1 = require("../src/table/td");
10
+ const dd_1 = require("../src/nowiki/dd");
11
+ /** @ignore */
12
+ const isTr = (token) => token.lastChild.constructor !== index_1.Token;
13
+ /**
14
+ * 解析表格,注意`tr`和`td`包含开头的换行
15
+ * @param {Token & {firstChild: AstText}} root 根节点
16
+ * @param config
17
+ * @param accum
18
+ */
19
+ const parseTable = ({ firstChild: { data }, type, name }, config = Parser.getConfig(), accum = []) => {
20
+ const stack = [], lines = data.split('\n');
21
+ let out = type === 'root' || type === 'parameter-value' || type === 'ext-inner' && name === 'poem'
22
+ ? ''
23
+ : `\n${lines.shift()}`;
24
+ /**
25
+ * 向表格中插入纯文本
26
+ * @param str 待插入的文本
27
+ * @param top 当前解析的表格或表格行
28
+ */
29
+ const push = (str, top) => {
30
+ if (!top) {
31
+ out += str;
32
+ return;
33
+ }
34
+ const { lastChild } = top;
35
+ if (isTr(top)) {
36
+ const token = new index_1.Token(str, config, accum);
37
+ token.type = 'table-inter';
38
+ token.setAttribute('stage', 3);
39
+ top.insertAt(token);
40
+ }
41
+ else {
42
+ lastChild.setText(String(lastChild) + str);
43
+ }
44
+ };
45
+ for (const outLine of lines) {
46
+ let top = stack.pop();
47
+ const [spaces] = /^(?:\s|\0\d+c\x7F)*/u.exec(outLine), line = outLine.slice(spaces.length), matchesStart = /^(:*)((?:\s|\0\d+c\x7F)*)(\{\||\{(?:\0\d+c\x7F)*\0\d+!\x7F|\0\d+\{\x7F)(.*)$/u
48
+ .exec(line);
49
+ if (matchesStart) {
50
+ while (top && top.type !== 'td') {
51
+ top = stack.pop();
52
+ }
53
+ const [, indent, moreSpaces, tableSyntax, attr] = matchesStart;
54
+ if (indent) {
55
+ new dd_1.DdToken(indent, config, accum);
56
+ }
57
+ push(`\n${spaces}${indent && `\0${accum.length - 1}d\x7F`}${moreSpaces}\0${accum.length}b\x7F`, top);
58
+ stack.push(...top ? [top] : [], new index_2.TableToken(tableSyntax, attr, config, accum));
59
+ continue;
60
+ }
61
+ else if (!top) {
62
+ out += `\n${outLine}`;
63
+ continue;
64
+ }
65
+ // eslint-disable-next-line operator-linebreak
66
+ const matches = /^(?:(\|\}|\0\d+!\x7F\}|\0\d+\}\x7F)|(\|-+|\0\d+!\x7F-+|\0\d+-\x7F-*)(?!-)|(!|(?:\||\0\d+!\x7F)\+?))(.*)$/u
67
+ .exec(line);
68
+ if (!matches) {
69
+ push(`\n${outLine}`, top);
70
+ stack.push(top);
71
+ continue;
72
+ }
73
+ const [, closing, row, cell, attr] = matches;
74
+ if (closing) {
75
+ while (top.type !== 'table') {
76
+ top = stack.pop();
77
+ }
78
+ top.close(`\n${spaces}${closing}`, true);
79
+ push(attr, stack.at(-1));
80
+ }
81
+ else if (row) {
82
+ if (top.type === 'td') {
83
+ top = stack.pop();
84
+ }
85
+ if (top.type === 'tr') {
86
+ top = stack.pop();
87
+ }
88
+ const tr = new tr_1.TrToken(`\n${spaces}${row}`, attr, config, accum);
89
+ stack.push(top, tr);
90
+ top.insertAt(tr);
91
+ }
92
+ else {
93
+ if (top.type === 'td') {
94
+ top = stack.pop();
95
+ }
96
+ const regex = cell === '!' ? /!!|(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu : /(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu;
97
+ let mt = regex.exec(attr), lastIndex = 0, lastSyntax = `\n${spaces}${cell}`;
98
+ while (mt) {
99
+ top.insertAt(new td_1.TdToken(lastSyntax, attr.slice(lastIndex, mt.index), config, accum));
100
+ ({ lastIndex } = regex);
101
+ [lastSyntax] = mt;
102
+ mt = regex.exec(attr);
103
+ }
104
+ const td = new td_1.TdToken(lastSyntax, attr.slice(lastIndex), config, accum);
105
+ stack.push(top, td);
106
+ top.insertAt(td);
107
+ }
108
+ }
109
+ return out.slice(1);
110
+ };
111
+ exports.parseTable = parseTable;
112
+ constants_1.parsers['parseTable'] = __filename;
@@ -0,0 +1,58 @@
1
+ import * as Parser from '../index';
2
+ import { Token } from './index';
3
+ import { AtomToken } from './atom';
4
+ import { HiddenToken } from './hidden';
5
+ import type { LintError } from '../base';
6
+ /**
7
+ * `{{{}}}`包裹的参数
8
+ * @classdesc `{childNodes: [AtomToken, ?Token, ...HiddenToken]}`
9
+ */
10
+ export declare class ArgToken extends Token {
11
+ #private;
12
+ readonly type = "arg";
13
+ readonly name: string;
14
+ readonly childNodes: [AtomToken] | [AtomToken, Token, ...HiddenToken[]];
15
+ abstract get children(): [AtomToken] | [AtomToken, Token, ...HiddenToken[]];
16
+ abstract get firstChild(): AtomToken;
17
+ abstract get firstElementChild(): AtomToken;
18
+ abstract get lastChild(): Token;
19
+ abstract get lastElementChild(): Token;
20
+ /** 预设值 */
21
+ get default(): string | false;
22
+ set default(value: string | false);
23
+ /** @param parts 以'|'分隔的各部分 */
24
+ constructor(parts: readonly string[], config?: Parser.Config, accum?: Token[]);
25
+ /** @override */
26
+ text(): string;
27
+ /** @override */
28
+ lint(start?: number): LintError[];
29
+ /** @override */
30
+ print(): string;
31
+ /** @override */
32
+ cloneNode(): this;
33
+ /** 移除无效部分 */
34
+ removeRedundant(): void;
35
+ /**
36
+ * @override
37
+ * @param i 移除位置
38
+ */
39
+ removeAt(i: number): Token;
40
+ /**
41
+ * @override
42
+ * @param token 待插入的子节点
43
+ * @param i 插入位置
44
+ * @throws `RangeError` 不可插入多余子节点
45
+ * @throws `TypeError` 不可插入文本节点
46
+ */
47
+ insertAt<T extends Token>(token: T, i?: number): T;
48
+ /**
49
+ * 设置参数名
50
+ * @param name 新参数名
51
+ */
52
+ setName(name: string): void;
53
+ /**
54
+ * 设置预设值
55
+ * @param value 预设值
56
+ */
57
+ setDefault(value: string | false): void;
58
+ }
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArgToken = void 0;
4
+ const string_1 = require("../util/string");
5
+ const lint_1 = require("../util/lint");
6
+ const constants_1 = require("../util/constants");
7
+ const debug_1 = require("../util/debug");
8
+ const Parser = require("../index");
9
+ const index_1 = require("./index");
10
+ const atom_1 = require("./atom");
11
+ const hidden_1 = require("./hidden");
12
+ /**
13
+ * `{{{}}}`包裹的参数
14
+ * @classdesc `{childNodes: [AtomToken, ?Token, ...HiddenToken]}`
15
+ */
16
+ class ArgToken extends index_1.Token {
17
+ type = 'arg';
18
+ /** 预设值 */
19
+ get default() {
20
+ return this.childNodes[1]?.text() ?? false;
21
+ }
22
+ /* NOT FOR BROWSER */
23
+ set default(value) {
24
+ this.setDefault(value);
25
+ }
26
+ /* NOT FOR BROWSER END */
27
+ /** @param parts 以'|'分隔的各部分 */
28
+ constructor(parts, config = Parser.getConfig(), accum = []) {
29
+ super(undefined, config, accum, {
30
+ AtomToken: 0, Token: 1, HiddenToken: '2:',
31
+ });
32
+ for (let i = 0; i < parts.length; i++) {
33
+ if (i === 0) {
34
+ const token = new atom_1.AtomToken(parts[i], 'arg-name', config, accum, {
35
+ 'Stage-2': ':', '!HeadingToken': '',
36
+ });
37
+ super.insertAt(token);
38
+ }
39
+ else if (i > 1) {
40
+ const token = new hidden_1.HiddenToken(parts[i], config, accum, {
41
+ 'Stage-2': ':', '!HeadingToken': '',
42
+ });
43
+ super.insertAt(token);
44
+ }
45
+ else {
46
+ const token = new index_1.Token(parts[i], config, accum);
47
+ token.type = 'arg-default';
48
+ token.setAttribute('stage', 2);
49
+ super.insertAt(token);
50
+ }
51
+ }
52
+ this.protectChildren(0);
53
+ }
54
+ /** @private */
55
+ toString(omit) {
56
+ return omit && this.matchesTypes(omit) ? '' : `{{{${super.toString(omit, '|')}}}}`;
57
+ }
58
+ /** @override */
59
+ text() {
60
+ return `{{{${(0, string_1.text)(this.childNodes.slice(0, 2), '|')}}}}`;
61
+ }
62
+ /** @private */
63
+ getAttribute(key) {
64
+ return key === 'padding' ? 3 : super.getAttribute(key);
65
+ }
66
+ /** @private */
67
+ getGaps() {
68
+ return 1;
69
+ }
70
+ /** @override */
71
+ lint(start = this.getAbsoluteIndex()) {
72
+ if (!this.getAttribute('include')) {
73
+ return [(0, lint_1.generateForSelf)(this, { start }, 'unexpected template argument')];
74
+ }
75
+ const { childNodes: [argName, argDefault, ...rest] } = this, errors = argName.lint(start + 3);
76
+ if (argDefault) {
77
+ errors.push(...argDefault.lint(start + 4 + String(argName).length));
78
+ }
79
+ if (rest.length > 0) {
80
+ const rect = { start, ...this.getRootNode().posFromIndex(start) };
81
+ errors.push(...rest.map(child => {
82
+ const error = (0, lint_1.generateForChild)(child, rect, 'invisible content inside triple braces'), { startIndex, startCol, excerpt, } = error;
83
+ return {
84
+ ...error,
85
+ startIndex: startIndex - 1,
86
+ startCol: startCol - 1,
87
+ excerpt: `|${excerpt}`,
88
+ };
89
+ }));
90
+ }
91
+ return errors;
92
+ }
93
+ /** @override */
94
+ print() {
95
+ return super.print({ pre: '{{{', post: '}}}', sep: '|' });
96
+ }
97
+ /* NOT FOR BROWSER */
98
+ /** @override */
99
+ cloneNode() {
100
+ const [name, ...cloned] = this.cloneChildNodes();
101
+ return debug_1.Shadow.run(() => {
102
+ const token = new ArgToken([''], this.getAttribute('config'));
103
+ token.firstChild.safeReplaceWith(name);
104
+ token.append(...cloned);
105
+ token.afterBuild();
106
+ return token;
107
+ });
108
+ }
109
+ /** 设置name */
110
+ #setName() {
111
+ this.setAttribute('name', this.firstChild.text().trim());
112
+ }
113
+ /** @private */
114
+ afterBuild() {
115
+ this.#setName();
116
+ const /** @implements */ argListener = ({ prevTarget }) => {
117
+ if (prevTarget === this.firstChild) {
118
+ this.#setName();
119
+ }
120
+ };
121
+ this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
122
+ }
123
+ /** 移除无效部分 */
124
+ removeRedundant() {
125
+ debug_1.Shadow.run(() => {
126
+ for (let i = this.length - 1; i > 1; i--) {
127
+ super.removeAt(i);
128
+ }
129
+ });
130
+ }
131
+ /**
132
+ * @override
133
+ * @param i 移除位置
134
+ */
135
+ removeAt(i) {
136
+ if (i === 1) {
137
+ this.removeRedundant();
138
+ }
139
+ return super.removeAt(i);
140
+ }
141
+ /**
142
+ * @override
143
+ * @param token 待插入的子节点
144
+ * @param i 插入位置
145
+ * @throws `RangeError` 不可插入多余子节点
146
+ * @throws `TypeError` 不可插入文本节点
147
+ */
148
+ insertAt(token, i = this.length) {
149
+ i += i < 0 ? this.length : 0;
150
+ if (i > 1) {
151
+ throw new RangeError(`${this.constructor.name}不可插入多余的子节点!`);
152
+ }
153
+ else if (typeof token === 'string') {
154
+ throw new TypeError(`${this.constructor.name}不可插入文本节点!`);
155
+ }
156
+ super.insertAt(token, i);
157
+ if (i === 1) {
158
+ token.type = 'arg-default';
159
+ }
160
+ return token;
161
+ }
162
+ /**
163
+ * 设置参数名
164
+ * @param name 新参数名
165
+ */
166
+ setName(name) {
167
+ const { childNodes } = Parser.parse(name, this.getAttribute('include'), 2, this.getAttribute('config'));
168
+ this.firstChild.replaceChildren(...childNodes);
169
+ }
170
+ /**
171
+ * 设置预设值
172
+ * @param value 预设值
173
+ */
174
+ setDefault(value) {
175
+ if (value === false) {
176
+ this.removeAt(1);
177
+ return;
178
+ }
179
+ const root = Parser.parse(value, this.getAttribute('include'), undefined, this.getAttribute('config')), { childNodes: [, oldDefault] } = this;
180
+ if (oldDefault) {
181
+ oldDefault.replaceChildren(...root.childNodes);
182
+ }
183
+ else {
184
+ root.type = 'arg-default';
185
+ this.insertAt(root);
186
+ }
187
+ }
188
+ }
189
+ exports.ArgToken = ArgToken;
190
+ constants_1.classes['ArgToken'] = __filename;
@@ -0,0 +1,12 @@
1
+ import * as Parser from '../index';
2
+ import { Token } from './index';
3
+ declare type AtomTypes = 'arg-name' | 'attr-key' | 'attr-value' | 'ext-attr-dirty' | 'html-attr-dirty' | 'table-attr-dirty' | 'converter-flag' | 'converter-rule-variant' | 'converter-rule-to' | 'converter-rule-from' | 'invoke-function' | 'invoke-module' | 'template-name' | 'link-target' | 'param-line';
4
+ /** 不会被继续解析的plain Token */
5
+ export declare class AtomToken extends Token {
6
+ type: AtomTypes;
7
+ /** @class */
8
+ constructor(wikitext: string | undefined, type: AtomTypes, config?: Parser.Config, accum?: Token[], acceptable?: Acceptable);
9
+ /** @override */
10
+ cloneNode(): this;
11
+ }
12
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AtomToken = void 0;
4
+ const debug_1 = require("../util/debug");
5
+ const constants_1 = require("../util/constants");
6
+ const Parser = require("../index");
7
+ const index_1 = require("./index");
8
+ /** 不会被继续解析的plain Token */
9
+ class AtomToken extends index_1.Token {
10
+ /** @class */
11
+ constructor(wikitext, type, config = Parser.getConfig(), accum = [], acceptable) {
12
+ super(wikitext, config, accum, acceptable);
13
+ this.type = type;
14
+ }
15
+ /* NOT FOR BROWSER */
16
+ /** @override */
17
+ cloneNode() {
18
+ const cloned = this.cloneChildNodes(), config = this.getAttribute('config'), acceptable = this.getAttribute('acceptable');
19
+ return debug_1.Shadow.run(() => {
20
+ const token = new AtomToken(undefined, this.type, config, [], acceptable);
21
+ token.append(...cloned);
22
+ return token;
23
+ });
24
+ }
25
+ }
26
+ exports.AtomToken = AtomToken;
27
+ constants_1.classes['AtomToken'] = __filename;