wikiparser-node 0.11.0 → 1.0.0-beta.1

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 (252) hide show
  1. package/config/.schema.json +26 -0
  2. package/dist/index.d.ts +61 -113
  3. package/dist/index.js +314 -0
  4. package/dist/lib/element.d.ts +122 -108
  5. package/dist/lib/element.js +656 -0
  6. package/dist/lib/node.d.ts +115 -221
  7. package/dist/lib/node.js +473 -0
  8. package/dist/lib/ranges.d.ts +27 -26
  9. package/dist/lib/ranges.js +130 -0
  10. package/dist/lib/text.d.ts +36 -28
  11. package/dist/lib/text.js +215 -0
  12. package/dist/lib/title.d.ts +26 -12
  13. package/dist/lib/title.js +108 -0
  14. package/dist/mixin/attributesParent.js +90 -0
  15. package/dist/mixin/fixed.js +29 -0
  16. package/dist/mixin/hidden.js +19 -0
  17. package/dist/mixin/singleLine.js +23 -0
  18. package/dist/mixin/sol.js +41 -0
  19. package/dist/parser/brackets.js +118 -0
  20. package/dist/parser/commentAndExt.js +48 -0
  21. package/dist/parser/converter.js +31 -0
  22. package/dist/parser/externalLinks.js +22 -0
  23. package/dist/parser/hrAndDoubleUnderscore.js +35 -0
  24. package/dist/parser/html.js +29 -0
  25. package/dist/parser/links.js +86 -0
  26. package/dist/parser/list.js +51 -0
  27. package/dist/parser/magicLinks.js +32 -0
  28. package/dist/parser/quotes.js +57 -0
  29. package/dist/parser/selector.js +158 -0
  30. package/dist/parser/table.js +108 -0
  31. package/dist/src/arg.d.ts +47 -23
  32. package/dist/src/arg.js +196 -0
  33. package/dist/src/atom.d.ts +12 -0
  34. package/dist/src/atom.js +22 -0
  35. package/dist/src/attribute.d.ts +74 -33
  36. package/dist/src/attribute.js +433 -0
  37. package/dist/src/attributes.d.ts +61 -55
  38. package/dist/src/attributes.js +371 -0
  39. package/dist/src/converter.d.ts +45 -71
  40. package/dist/src/converter.js +135 -0
  41. package/dist/src/converterFlags.d.ts +64 -39
  42. package/dist/src/converterFlags.js +235 -0
  43. package/dist/src/converterRule.d.ts +49 -27
  44. package/dist/src/converterRule.js +255 -0
  45. package/dist/src/extLink.d.ts +41 -38
  46. package/dist/src/extLink.js +154 -0
  47. package/dist/src/gallery.d.ts +59 -18
  48. package/dist/src/gallery.js +132 -0
  49. package/dist/src/heading.d.ts +60 -22
  50. package/dist/src/heading.js +135 -0
  51. package/dist/src/hidden.d.ts +20 -0
  52. package/dist/src/hidden.js +24 -0
  53. package/dist/src/html.d.ts +83 -29
  54. package/dist/src/html.js +242 -0
  55. package/dist/src/imageParameter.d.ts +59 -40
  56. package/dist/src/imageParameter.js +251 -0
  57. package/dist/src/imagemap.d.ts +65 -21
  58. package/dist/src/imagemap.js +169 -0
  59. package/dist/src/imagemapLink.d.ts +46 -14
  60. package/dist/src/imagemapLink.js +38 -0
  61. package/dist/src/index.d.ts +71 -105
  62. package/dist/src/index.js +826 -0
  63. package/dist/src/link/base.d.ts +71 -0
  64. package/dist/src/link/base.js +225 -0
  65. package/dist/src/link/category.d.ts +10 -11
  66. package/dist/src/link/category.js +26 -0
  67. package/dist/src/link/file.d.ts +61 -39
  68. package/dist/src/link/file.js +242 -0
  69. package/dist/src/link/galleryImage.d.ts +34 -12
  70. package/dist/src/link/galleryImage.js +98 -0
  71. package/dist/src/link/index.d.ts +25 -63
  72. package/dist/src/link/index.js +136 -0
  73. package/dist/src/magicLink.d.ts +22 -15
  74. package/dist/src/magicLink.js +126 -0
  75. package/dist/src/nested.d.ts +47 -0
  76. package/dist/src/nested.js +84 -0
  77. package/dist/src/nowiki/base.d.ts +39 -0
  78. package/dist/src/nowiki/base.js +29 -0
  79. package/dist/src/nowiki/comment.d.ts +31 -20
  80. package/dist/src/nowiki/comment.js +61 -0
  81. package/dist/src/nowiki/dd.d.ts +17 -11
  82. package/dist/src/nowiki/dd.js +50 -0
  83. package/dist/src/nowiki/doubleUnderscore.d.ts +28 -13
  84. package/dist/src/nowiki/doubleUnderscore.js +45 -0
  85. package/dist/src/nowiki/hr.d.ts +28 -10
  86. package/dist/src/nowiki/hr.js +33 -0
  87. package/dist/src/nowiki/index.d.ts +17 -23
  88. package/dist/src/nowiki/index.js +21 -0
  89. package/dist/src/nowiki/list.d.ts +15 -7
  90. package/dist/src/nowiki/list.js +11 -0
  91. package/dist/src/nowiki/noinclude.d.ts +20 -7
  92. package/dist/src/nowiki/noinclude.js +22 -0
  93. package/dist/src/nowiki/quote.d.ts +25 -10
  94. package/dist/src/nowiki/quote.js +55 -0
  95. package/dist/src/onlyinclude.d.ts +28 -12
  96. package/dist/src/onlyinclude.js +64 -0
  97. package/dist/src/paramTag/index.d.ts +40 -17
  98. package/dist/src/paramTag/index.js +76 -0
  99. package/dist/src/paramTag/inputbox.d.ts +8 -7
  100. package/dist/src/paramTag/inputbox.js +19 -0
  101. package/dist/src/parameter.d.ts +62 -41
  102. package/dist/src/parameter.js +201 -0
  103. package/dist/src/pre.d.ts +32 -0
  104. package/dist/src/pre.js +39 -0
  105. package/dist/src/syntax.d.ts +17 -14
  106. package/dist/src/syntax.js +65 -0
  107. package/dist/src/table/base.d.ts +55 -0
  108. package/dist/src/table/base.js +77 -0
  109. package/dist/src/table/index.d.ts +123 -156
  110. package/dist/src/table/index.js +811 -0
  111. package/dist/src/table/td.d.ts +90 -67
  112. package/dist/src/table/td.js +276 -0
  113. package/dist/src/table/tr.d.ts +30 -85
  114. package/dist/src/table/tr.js +48 -0
  115. package/dist/src/table/trBase.d.ts +72 -0
  116. package/dist/src/table/trBase.js +153 -0
  117. package/dist/src/tagPair/ext.d.ts +47 -11
  118. package/dist/src/tagPair/ext.js +127 -0
  119. package/dist/src/tagPair/include.d.ts +32 -13
  120. package/dist/src/tagPair/include.js +40 -0
  121. package/dist/src/tagPair/index.d.ts +44 -29
  122. package/dist/src/tagPair/index.js +111 -0
  123. package/dist/src/transclude.d.ts +88 -85
  124. package/dist/src/transclude.js +739 -0
  125. package/dist/util/base.js +26 -0
  126. package/dist/util/debug.js +52 -0
  127. package/dist/util/diff.js +69 -0
  128. package/dist/util/lint.js +38 -0
  129. package/dist/util/string.js +103 -0
  130. package/errors/README +1 -0
  131. package/i18n/zh-hans.json +1 -0
  132. package/i18n/zh-hant.json +1 -0
  133. package/package.json +21 -24
  134. package/printed/README +1 -0
  135. package/dist/mixin/attributeParent.d.ts +0 -9
  136. package/dist/mixin/fixedToken.d.ts +0 -8
  137. package/dist/mixin/hidden.d.ts +0 -8
  138. package/dist/mixin/singleLine.d.ts +0 -8
  139. package/dist/mixin/sol.d.ts +0 -8
  140. package/dist/parser/brackets.d.ts +0 -12
  141. package/dist/parser/commentAndExt.d.ts +0 -8
  142. package/dist/parser/converter.d.ts +0 -7
  143. package/dist/parser/externalLinks.d.ts +0 -7
  144. package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
  145. package/dist/parser/html.d.ts +0 -7
  146. package/dist/parser/links.d.ts +0 -7
  147. package/dist/parser/list.d.ts +0 -7
  148. package/dist/parser/magicLinks.d.ts +0 -7
  149. package/dist/parser/quotes.d.ts +0 -7
  150. package/dist/parser/selector.d.ts +0 -12
  151. package/dist/parser/table.d.ts +0 -11
  152. package/dist/src/atom/hidden.d.ts +0 -5
  153. package/dist/src/atom/index.d.ts +0 -15
  154. package/dist/src/charinsert.d.ts +0 -32
  155. package/dist/src/hasNowiki/index.d.ts +0 -14
  156. package/dist/src/hasNowiki/pre.d.ts +0 -13
  157. package/dist/src/nested/choose.d.ts +0 -13
  158. package/dist/src/nested/combobox.d.ts +0 -13
  159. package/dist/src/nested/index.d.ts +0 -18
  160. package/dist/src/nested/references.d.ts +0 -13
  161. package/dist/tool/index.d.ts +0 -420
  162. package/dist/util/base.d.ts +0 -10
  163. package/dist/util/debug.d.ts +0 -20
  164. package/dist/util/diff.d.ts +0 -8
  165. package/dist/util/lint.d.ts +0 -28
  166. package/dist/util/string.d.ts +0 -55
  167. package/index.js +0 -333
  168. package/lib/element.js +0 -618
  169. package/lib/node.js +0 -730
  170. package/lib/ranges.js +0 -130
  171. package/lib/text.js +0 -265
  172. package/lib/title.js +0 -83
  173. package/mixin/attributeParent.js +0 -117
  174. package/mixin/fixedToken.js +0 -40
  175. package/mixin/hidden.js +0 -21
  176. package/mixin/singleLine.js +0 -31
  177. package/mixin/sol.js +0 -54
  178. package/parser/brackets.js +0 -128
  179. package/parser/commentAndExt.js +0 -62
  180. package/parser/converter.js +0 -46
  181. package/parser/externalLinks.js +0 -33
  182. package/parser/hrAndDoubleUnderscore.js +0 -49
  183. package/parser/html.js +0 -42
  184. package/parser/links.js +0 -94
  185. package/parser/list.js +0 -59
  186. package/parser/magicLinks.js +0 -41
  187. package/parser/quotes.js +0 -64
  188. package/parser/selector.js +0 -180
  189. package/parser/table.js +0 -114
  190. package/src/arg.js +0 -207
  191. package/src/atom/hidden.js +0 -13
  192. package/src/atom/index.js +0 -43
  193. package/src/attribute.js +0 -472
  194. package/src/attributes.js +0 -453
  195. package/src/charinsert.js +0 -97
  196. package/src/converter.js +0 -176
  197. package/src/converterFlags.js +0 -284
  198. package/src/converterRule.js +0 -256
  199. package/src/extLink.js +0 -180
  200. package/src/gallery.js +0 -149
  201. package/src/hasNowiki/index.js +0 -44
  202. package/src/hasNowiki/pre.js +0 -40
  203. package/src/heading.js +0 -134
  204. package/src/html.js +0 -254
  205. package/src/imageParameter.js +0 -303
  206. package/src/imagemap.js +0 -199
  207. package/src/imagemapLink.js +0 -41
  208. package/src/index.js +0 -938
  209. package/src/link/category.js +0 -44
  210. package/src/link/file.js +0 -287
  211. package/src/link/galleryImage.js +0 -120
  212. package/src/link/index.js +0 -388
  213. package/src/magicLink.js +0 -151
  214. package/src/nested/choose.js +0 -24
  215. package/src/nested/combobox.js +0 -23
  216. package/src/nested/index.js +0 -96
  217. package/src/nested/references.js +0 -23
  218. package/src/nowiki/comment.js +0 -71
  219. package/src/nowiki/dd.js +0 -59
  220. package/src/nowiki/doubleUnderscore.js +0 -56
  221. package/src/nowiki/hr.js +0 -41
  222. package/src/nowiki/index.js +0 -56
  223. package/src/nowiki/list.js +0 -16
  224. package/src/nowiki/noinclude.js +0 -28
  225. package/src/nowiki/quote.js +0 -69
  226. package/src/onlyinclude.js +0 -64
  227. package/src/paramTag/index.js +0 -89
  228. package/src/paramTag/inputbox.js +0 -35
  229. package/src/parameter.js +0 -239
  230. package/src/syntax.js +0 -91
  231. package/src/table/index.js +0 -985
  232. package/src/table/td.js +0 -343
  233. package/src/table/tr.js +0 -319
  234. package/src/tagPair/ext.js +0 -146
  235. package/src/tagPair/include.js +0 -50
  236. package/src/tagPair/index.js +0 -131
  237. package/src/transclude.js +0 -843
  238. package/tool/index.js +0 -1209
  239. package/typings/api.d.ts +0 -9
  240. package/typings/array.d.ts +0 -29
  241. package/typings/event.d.ts +0 -22
  242. package/typings/index.d.ts +0 -118
  243. package/typings/node.d.ts +0 -35
  244. package/typings/parser.d.ts +0 -12
  245. package/typings/table.d.ts +0 -10
  246. package/typings/token.d.ts +0 -31
  247. package/typings/tool.d.ts +0 -6
  248. package/util/base.js +0 -17
  249. package/util/debug.js +0 -73
  250. package/util/diff.js +0 -76
  251. package/util/lint.js +0 -57
  252. package/util/string.js +0 -126
package/src/converter.js DELETED
@@ -1,176 +0,0 @@
1
- 'use strict';
2
-
3
- const {text, print} = require('../util/string'),
4
- Parser = require('..'),
5
- Token = require('.'),
6
- ConverterFlagsToken = require('./converterFlags'),
7
- ConverterRuleToken = require('./converterRule');
8
-
9
- /**
10
- * 转换
11
- * @classdesc `{childNodes: [ConverterFlagsToken, ...ConverterRuleToken]}`
12
- */
13
- class ConverterToken extends Token {
14
- type = 'converter';
15
-
16
- /**
17
- * 是否无转换
18
- * @this {ConverterToken & {lastChild: ConverterRuleToken}}
19
- */
20
- get noConvert() {
21
- return this.hasFlag('R') || this.length === 2 && !this.lastChild.variant;
22
- }
23
-
24
- /** flags */
25
- get flags() {
26
- return this.getAllFlags();
27
- }
28
-
29
- /**
30
- * @param {string[]} flags 转换类型标记
31
- * @param {string[]} rules 转换规则
32
- * @param {import('../typings/token').accum} accum
33
- */
34
- constructor(flags, rules, config = Parser.getConfig(), accum = []) {
35
- super(undefined, config, true, accum);
36
- this.append(new ConverterFlagsToken(flags, config, accum));
37
- const [firstRule] = rules,
38
- hasColon = firstRule.includes(':'),
39
- firstRuleToken = new ConverterRuleToken(firstRule, hasColon, config, accum);
40
- if (hasColon && firstRuleToken.length === 1) {
41
- this.insertAt(new ConverterRuleToken(rules.join(';'), false, config, accum));
42
- } else {
43
- this.append(
44
- firstRuleToken,
45
- ...rules.slice(1).map(rule => new ConverterRuleToken(rule, true, config, accum)),
46
- );
47
- }
48
- this.getAttribute('protectChildren')(0);
49
- }
50
-
51
- /**
52
- * @override
53
- * @param {string} selector
54
- * @returns {string}
55
- */
56
- toString(selector) {
57
- const {childNodes: [flags, ...rules]} = this;
58
- return selector && this.matches(selector)
59
- ? ''
60
- : `-{${flags.toString(selector)}${flags.length > 0 ? '|' : ''}${rules.map(String).join(';')}}-`;
61
- }
62
-
63
- /**
64
- * @override
65
- * @returns {string}
66
- */
67
- text() {
68
- const {childNodes: [flags, ...rules]} = this;
69
- return `-{${flags.text()}|${text(rules, ';')}}-`;
70
- }
71
-
72
- /** @override */
73
- getPadding() {
74
- return 2;
75
- }
76
-
77
- /**
78
- * @override
79
- * @param {number} i 子节点位置
80
- */
81
- getGaps(i = 0) {
82
- i = i < 0 ? i + this.length : i;
83
- return i || this.firstChild.length > 0 ? 1 : 0;
84
- }
85
-
86
- /** @override */
87
- print() {
88
- const {childNodes: [flags, ...rules]} = this;
89
- return `<span class="wpb-converter">-{${flags.print()}${
90
- flags.length > 0 ? '|' : ''
91
- }${print(rules, {sep: ';'})}}-</span>`;
92
- }
93
-
94
- /** @override */
95
- cloneNode() {
96
- const [flags, ...rules] = this.cloneChildNodes();
97
- return Parser.run(() => {
98
- const token = new ConverterToken([], [], this.getAttribute('config'));
99
- token.firstChild.safeReplaceWith(flags);
100
- token.append(...rules);
101
- return token;
102
- });
103
- }
104
-
105
- /**
106
- * 获取所有转换类型标记
107
- * @this {{firstChild: ConverterFlagsToken}}
108
- */
109
- getAllFlags() {
110
- return this.firstChild.getAllFlags();
111
- }
112
-
113
- /**
114
- * 获取有效的转换类型标记
115
- * @this {{firstChild: ConverterFlagsToken}}
116
- */
117
- getEffectiveFlags() {
118
- return this.firstChild.getEffectiveFlags();
119
- }
120
-
121
- /**
122
- * 获取未知的转换类型标记
123
- * @this {{firstChild: ConverterFlagsToken}}
124
- */
125
- getUnknownFlags() {
126
- return this.firstChild.getUnknownFlags();
127
- }
128
-
129
- /**
130
- * 是否具有某转换类型标记
131
- * @this {{firstChild: ConverterFlagsToken}}
132
- * @param {string} flag 转换类型标记
133
- */
134
- hasFlag(flag) {
135
- return this.firstChild.hasFlag(flag);
136
- }
137
-
138
- /**
139
- * 是否具有某有效的转换类型标记
140
- * @this {{firstChild: ConverterFlagsToken}}
141
- * @param {string} flag 转换类型标记
142
- */
143
- hasEffectiveFlag(flag) {
144
- return this.firstChild.hasEffectiveFlag(flag);
145
- }
146
-
147
- /**
148
- * 移除转换类型标记
149
- * @this {{firstChild: ConverterFlagsToken}}
150
- * @param {string} flag 转换类型标记
151
- */
152
- removeFlag(flag) {
153
- this.firstChild.removeFlag(flag);
154
- }
155
-
156
- /**
157
- * 设置转换类型标记
158
- * @this {{firstChild: ConverterFlagsToken}}
159
- * @param {string} flag 转换类型标记
160
- */
161
- setFlag(flag) {
162
- this.firstChild.setFlag(flag);
163
- }
164
-
165
- /**
166
- * 开关某转换类型标记
167
- * @this {{firstChild: ConverterFlagsToken}}
168
- * @param {string} flag 转换类型标记
169
- */
170
- toggleFlag(flag) {
171
- this.firstChild.toggleFlag(flag);
172
- }
173
- }
174
-
175
- Parser.classes.ConverterToken = __filename;
176
- module.exports = ConverterToken;
@@ -1,284 +0,0 @@
1
- 'use strict';
2
-
3
- const {generateForChild} = require('../util/lint'),
4
- Parser = require('..'),
5
- Token = require('.'),
6
- AtomToken = require('./atom');
7
-
8
- const definedFlags = new Set(['A', 'T', 'R', 'D', '-', 'H', 'N']);
9
-
10
- /**
11
- * 转换flags
12
- * @classdesc `{childNodes: ...AtomToken}`
13
- */
14
- class ConverterFlagsToken extends Token {
15
- type = 'converter-flags';
16
- /** @type {string[]} */ #flags;
17
-
18
- /**
19
- * @param {string[]} flags 转换类型标记
20
- * @param {import('../typings/token').accum} accum
21
- */
22
- constructor(flags, config = Parser.getConfig(), accum = []) {
23
- super(undefined, config, true, accum, {
24
- AtomToken: ':',
25
- });
26
- this.append(...flags.map(flag => new AtomToken(flag, 'converter-flag', config, accum)));
27
- }
28
-
29
- /**
30
- * @override
31
- * @complexity `n`
32
- */
33
- afterBuild() {
34
- this.#flags = this.childNodes.map(child => child.text().trim());
35
- const /** @type {import('../typings/event').AstListener} */ converterFlagsListener = ({prevTarget}) => {
36
- if (prevTarget) {
37
- this.#flags[this.childNodes.indexOf(prevTarget)] = prevTarget.text().trim();
38
- }
39
- };
40
- this.addEventListener(['remove', 'insert', 'text', 'replace'], converterFlagsListener);
41
- }
42
-
43
- /**
44
- * @override
45
- * @param {string} selector
46
- */
47
- toString(selector) {
48
- return super.toString(selector, ';');
49
- }
50
-
51
- /** @override */
52
- text() {
53
- return super.text(';');
54
- }
55
-
56
- /** @override */
57
- getGaps() {
58
- return 1;
59
- }
60
-
61
- /** @override */
62
- print() {
63
- return super.print({sep: ';'});
64
- }
65
-
66
- /**
67
- * @override
68
- * @param {number} start 起始位置
69
- */
70
- lint(start = this.getAbsoluteIndex()) {
71
- const variantFlags = this.getVariantFlags(),
72
- unknownFlags = this.getUnknownFlags(),
73
- validFlags = new Set(this.#flags.filter(flag => definedFlags.has(flag))),
74
- {length: emptyFlagCount} = this.#flags.filter(flag => !flag),
75
- knownFlagCount = this.#flags.length - unknownFlags.size - emptyFlagCount,
76
- errors = super.lint(start);
77
- if (variantFlags.size === knownFlagCount || validFlags.size === knownFlagCount) {
78
- return errors;
79
- }
80
- const rect = {start, ...this.getRootNode().posFromIndex(start)},
81
- {childNodes, length} = this;
82
- for (let i = 0; i < length; i++) {
83
- const child = childNodes[i],
84
- flag = child.text().trim();
85
- if (flag && !variantFlags.has(flag) && !unknownFlags.has(flag)
86
- && (variantFlags.size > 0 || !validFlags.has(flag))
87
- ) {
88
- const error = generateForChild(child, rect, 'invalid conversion flag');
89
- errors.push({...error, excerpt: childNodes.slice(0, i + 1).map(String).join(';').slice(-50)});
90
- }
91
- }
92
- return errors;
93
- }
94
-
95
- /**
96
- * 获取未知转换类型标记
97
- * @complexity `n`
98
- */
99
- getUnknownFlags() {
100
- return new Set(this.#flags.filter(flag => /\{{3}[^{}]+\}{3}/u.test(flag)));
101
- }
102
-
103
- /** 获取指定语言变体的转换标记 */
104
- getVariantFlags() {
105
- const variants = new Set(this.getAttribute('config').variants);
106
- return new Set(this.#flags.filter(flag => variants.has(flag)));
107
- }
108
-
109
- /** @override */
110
- cloneNode() {
111
- const cloned = this.cloneChildNodes();
112
- return Parser.run(() => {
113
- const token = new ConverterFlagsToken([], this.getAttribute('config'));
114
- token.append(...cloned);
115
- token.afterBuild();
116
- return token;
117
- });
118
- }
119
-
120
- /**
121
- * @override
122
- * @template {string} T
123
- * @param {T} key 属性键
124
- * @returns {import('../typings/node').TokenAttribute<T>}
125
- */
126
- getAttribute(key) {
127
- if (key === 'flags') {
128
- return Parser.debugging ? this.#flags : [...this.#flags];
129
- }
130
- return super.getAttribute(key);
131
- }
132
-
133
- /**
134
- * @override
135
- * @param {PropertyKey} key 属性键
136
- */
137
- hasAttribute(key) {
138
- return key === 'flags' || super.hasAttribute(key);
139
- }
140
-
141
- /**
142
- * @override
143
- * @param {number} i 移除位置
144
- * @complexity `n`
145
- */
146
- removeAt(i) {
147
- const /** @type {AtomToken} */ token = super.removeAt(i);
148
- this.#flags?.splice(i, 1);
149
- return token;
150
- }
151
-
152
- /**
153
- * @override
154
- * @param {AtomToken} token 待插入的子节点
155
- * @param {number} i 插入位置
156
- * @complexity `n`
157
- */
158
- insertAt(token, i = this.length) {
159
- super.insertAt(token, i);
160
- this.#flags?.splice(i, 0, token.text());
161
- return token;
162
- }
163
-
164
- /** 获取所有转换类型标记 */
165
- getAllFlags() {
166
- return new Set(this.#flags);
167
- }
168
-
169
- /**
170
- * 获取转换类型标记节点
171
- * @param {string} flag 转换类型标记
172
- * @returns {AtomToken[]}
173
- * @complexity `n`
174
- */
175
- getFlagToken(flag) {
176
- return this.#flags.includes(flag) ? this.childNodes.filter(child => child.text().trim() === flag) : [];
177
- }
178
-
179
- /**
180
- * 获取有效转换类型标记
181
- * @complexity `n`
182
- */
183
- getEffectiveFlags() {
184
- const variantFlags = this.getVariantFlags(),
185
- unknownFlags = this.getUnknownFlags();
186
- if (variantFlags.size > 0) {
187
- return new Set([...variantFlags, ...unknownFlags]);
188
- }
189
- const flags = new Set([...this.#flags.filter(flag => definedFlags.has(flag)), ...unknownFlags]);
190
- if (flags.size === 0) {
191
- return new Set('S');
192
- } else if (flags.has('R')) {
193
- return new Set('R');
194
- } else if (flags.has('N')) {
195
- return new Set('N');
196
- } else if (flags.has('-')) {
197
- return new Set('-');
198
- } else if (flags.has('H')) {
199
- const hasT = flags.has('T'),
200
- hasD = flags.has('D');
201
- return hasT && hasD
202
- ? new Set(['+', 'H', 'T', 'D'])
203
- : new Set(['+', 'H', ...hasT ? ['T'] : [], ...hasD ? ['D'] : [], ...unknownFlags]);
204
- }
205
- if (flags.size === 1 && flags.has('T')) {
206
- flags.add('H');
207
- }
208
- if (flags.has('A')) {
209
- flags.add('+');
210
- flags.add('S');
211
- }
212
- if (flags.has('D')) {
213
- flags.delete('S');
214
- }
215
- return flags;
216
- }
217
-
218
- /**
219
- * 是否具有某转换类型标记
220
- * @param {string} flag 转换类型标记
221
- */
222
- hasFlag(flag) {
223
- return typeof flag === 'string' ? this.#flags.includes(flag) : this.typeError('hasFlag', 'String');
224
- }
225
-
226
- /**
227
- * 是否具有某有效转换类型标记
228
- * @param {string} flag 转换类型标记
229
- * @complexity `n`
230
- */
231
- hasEffectiveFlag(flag) {
232
- return typeof flag === 'string' ? this.getEffectiveFlags().has(flag) : this.typeError('hasFlag', 'String');
233
- }
234
-
235
- /**
236
- * 移除某转换类型标记
237
- * @param {string} flag 转换类型标记
238
- * @complexity `n²`
239
- */
240
- removeFlag(flag) {
241
- for (const token of this.getFlagToken(flag)) {
242
- token.remove();
243
- }
244
- }
245
-
246
- /**
247
- * 添加转换类型标记
248
- * @param {string} flag 转换类型标记
249
- * @complexity `n`
250
- */
251
- #newFlag(flag) {
252
- const token = Parser.run(() => new AtomToken(flag, 'converter-flag', this.getAttribute('config')));
253
- this.insertAt(token);
254
- }
255
-
256
- /**
257
- * 设置转换类型标记
258
- * @param {string} flag 转换类型标记
259
- * @complexity `n`
260
- */
261
- setFlag(flag) {
262
- if (!this.#flags.includes(flag)) {
263
- this.#newFlag(flag);
264
- } else if (!this.getEffectiveFlags().has(flag)) {
265
- Parser.error('此 flag 不会生效', flag);
266
- }
267
- }
268
-
269
- /**
270
- * 开关转换类型标记
271
- * @param {string} flag 转换类型标记
272
- * @complexity `n²`
273
- */
274
- toggleFlag(flag) {
275
- if (this.#flags.includes(flag)) {
276
- this.removeFlag(flag);
277
- } else {
278
- this.#newFlag(flag);
279
- }
280
- }
281
- }
282
-
283
- Parser.classes.ConverterFlagsToken = __filename;
284
- module.exports = ConverterFlagsToken;
@@ -1,256 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- {undo} = require('../util/debug'),
5
- {noWrap} = require('../util/string'),
6
- Token = require('.'),
7
- AtomToken = require('./atom');
8
-
9
- /**
10
- * 转换规则
11
- * @classdesc `{childNodes: ...AtomToken)}`
12
- */
13
- class ConverterRuleToken extends Token {
14
- type = 'converter-rule';
15
-
16
- /** 语言变体 */
17
- get variant() {
18
- return this.childNodes.at(-2)?.text()?.trim() ?? '';
19
- }
20
-
21
- set variant(variant) {
22
- this.setVariant(variant);
23
- }
24
-
25
- /** 是否是单向转换 */
26
- get unidirectional() {
27
- return this.length === 3;
28
- }
29
-
30
- set unidirectional(unidirectional) {
31
- if (unidirectional === false) {
32
- this.makeBidirectional();
33
- }
34
- }
35
-
36
- /** 是否是双向转换 */
37
- get bidirectional() {
38
- return this.length === 2;
39
- }
40
-
41
- /**
42
- * @param {string} rule 转换规则
43
- * @param {boolean} hasColon 是否带有":"
44
- * @param {import('../typings/token').accum} accum
45
- */
46
- constructor(rule, hasColon = true, config = Parser.getConfig(), accum = []) {
47
- super(undefined, config, true, accum);
48
- if (hasColon) {
49
- const i = rule.indexOf(':'),
50
- j = rule.slice(0, i).indexOf('=>'),
51
- v = j === -1 ? rule.slice(0, i) : rule.slice(j + 2, i);
52
- if (config.variants.includes(v.trim())) {
53
- super.insertAt(new AtomToken(v, 'converter-rule-variant', config, accum));
54
- super.insertAt(new AtomToken(rule.slice(i + 1), 'converter-rule-to', config, accum));
55
- if (j !== -1) {
56
- super.insertAt(new AtomToken(rule.slice(0, j), 'converter-rule-from', config, accum), 0);
57
- }
58
- } else {
59
- super.insertAt(new AtomToken(rule, 'converter-rule-noconvert', config, accum));
60
- }
61
- } else {
62
- super.insertAt(new AtomToken(rule, 'converter-rule-noconvert', config, accum));
63
- }
64
- this.getAttribute('protectChildren')('1:');
65
- }
66
-
67
- /**
68
- * @override
69
- * @param {string} selector
70
- * @returns {string}
71
- */
72
- toString(selector) {
73
- if (this.length === 3 && !(selector && this.matches(selector))) {
74
- const {childNodes: [from, variant, to]} = this;
75
- return `${from.toString(selector)}=>${variant.toString(selector)}:${to.toString(selector)}`;
76
- }
77
- return super.toString(selector, ':');
78
- }
79
-
80
- /**
81
- * @override
82
- * @returns {string}
83
- */
84
- text() {
85
- if (this.length === 3) {
86
- const {childNodes: [from, variant, to]} = this;
87
- return `${from.text()}=>${variant.text()}:${to.text()}`;
88
- }
89
- return super.text(':');
90
- }
91
-
92
- /**
93
- * @override
94
- * @param {number} i 子节点序号
95
- */
96
- getGaps(i = 0) {
97
- const {length} = this;
98
- i = i < 0 ? i + length : i;
99
- return i === 0 && length === 3 ? 2 : 1;
100
- }
101
-
102
- /** @override */
103
- print() {
104
- if (this.length === 3) {
105
- const {childNodes: [from, variant, to]} = this;
106
- return `<span class="wpb-converter-rule">${from.print()}=>${variant.print()}:${to.print()}</span>`;
107
- }
108
- return super.print({sep: ':'});
109
- }
110
-
111
- /** @override */
112
- cloneNode() {
113
- const cloned = this.cloneChildNodes(),
114
- placeholders = ['', 'zh:', '=>zh:'],
115
- placeholder = placeholders[cloned.length - 1];
116
- return Parser.run(() => {
117
- const token = new ConverterRuleToken(placeholder, placeholder, this.getAttribute('config'));
118
- for (let i = 0; i < cloned.length; i++) {
119
- token.childNodes[i].safeReplaceWith(cloned[i]);
120
- }
121
- token.afterBuild();
122
- return token;
123
- });
124
- }
125
-
126
- /** @override */
127
- afterBuild() {
128
- const /** @type {import('../typings/event').AstListener} */ converterRuleListener = (e, data) => {
129
- const {prevTarget} = e;
130
- if (this.length > 1 && this.childNodes.at(-2) === prevTarget) {
131
- const v = prevTarget.text().trim(),
132
- {variants} = this.getAttribute('config');
133
- if (!variants.includes(v)) {
134
- undo(e, data);
135
- throw new Error(`无效的语言变体:${v}`);
136
- }
137
- }
138
- };
139
- this.addEventListener(['remove', 'insert', 'text', 'replace'], converterRuleListener);
140
- }
141
-
142
- /**
143
- * @override
144
- * @param {number} i 移除位置
145
- * @returns {AtomToken}
146
- * @throws `Error` 至少保留1个子节点
147
- */
148
- removeAt(i) {
149
- if (this.length === 1) {
150
- throw new Error(`${this.constructor.name} 需至少保留 1 个子节点!`);
151
- }
152
- const removed = super.removeAt(i);
153
- if (this.length === 1) {
154
- this.firstChild.type = 'converter-rule-noconvert';
155
- }
156
- return removed;
157
- }
158
-
159
- /**
160
- * @override
161
- * @throws `Error` 请勿手动插入子节点
162
- */
163
- insertAt() {
164
- throw new Error(`转换规则语法复杂,请勿尝试对 ${this.constructor.name} 手动插入子节点!`);
165
- }
166
-
167
- /** 修改为不转换 */
168
- noConvert() {
169
- const {length} = this;
170
- for (let i = 0; i < length - 1; i++) { // ConverterRuleToken只能从前往后删除子节点
171
- this.removeAt(0);
172
- }
173
- }
174
-
175
- /**
176
- * 设置转换目标
177
- * @param {string} to 转换目标
178
- * @throws `SyntaxError` 非法的转换目标
179
- */
180
- setTo(to) {
181
- to = String(to);
182
- const config = this.getAttribute('config'),
183
- root = Parser.parse(`-{|${config.variants[0]}:${to}}-`, this.getAttribute('include'), undefined, config),
184
- {length, firstChild: converter} = root,
185
- {lastChild: converterRule, type, length: converterLength} = converter;
186
- if (length !== 1 || type !== 'converter' || converterLength !== 2 || converterRule.length !== 2) {
187
- throw new SyntaxError(`非法的转换目标:${noWrap(to)}`);
188
- }
189
- const {lastChild} = converterRule;
190
- converterRule.destroy();
191
- this.lastChild.safeReplaceWith(lastChild);
192
- }
193
-
194
- /**
195
- * 设置语言变体
196
- * @param {string} variant 语言变体
197
- * @throws `RangeError` 无效的语言变体
198
- */
199
- setVariant(variant) {
200
- if (typeof variant !== 'string') {
201
- this.typeError('setVariant', 'String');
202
- }
203
- const config = this.getAttribute('config'),
204
- v = variant.trim();
205
- if (!config.variants.includes(v)) {
206
- throw new RangeError(`无效的语言变体:${v}`);
207
- } else if (this.length === 1) {
208
- super.insertAt(Parser.run(() => new AtomToken(variant, 'converter-rule-variant', config)), 0);
209
- } else {
210
- this.childNodes.at(-2).setText(variant);
211
- }
212
- }
213
-
214
- /**
215
- * 设置转换原文
216
- * @param {string} from 转换原文
217
- * @throws `Error` 尚未指定语言变体
218
- * @throws `SyntaxError` 非法的转换原文
219
- */
220
- setFrom(from) {
221
- const {variant, unidirectional} = this;
222
- if (!variant) {
223
- throw new Error('请先指定语言变体!');
224
- }
225
- from = String(from);
226
- const config = this.getAttribute('config'),
227
- root = Parser.parse(`-{|${from}=>${variant}:}-`, this.getAttribute('include'), undefined, config),
228
- {length, firstChild: converter} = root,
229
- {type, length: converterLength, lastChild: converterRule} = converter;
230
- if (length !== 1 || type !== 'converter' || converterLength !== 2 || converterRule.length !== 3) {
231
- throw new SyntaxError(`非法的转换原文:${noWrap(from)}`);
232
- } else if (unidirectional) {
233
- this.firstChild.safeReplaceWith(converterRule.firstChild);
234
- } else {
235
- super.insertAt(converterRule.firstChild, 0);
236
- }
237
- }
238
-
239
- /**
240
- * 修改为单向转换
241
- * @param {string} from 转换来源
242
- */
243
- makeUnidirectional(from) {
244
- this.setFrom(from);
245
- }
246
-
247
- /** 修改为双向转换 */
248
- makeBidirectional() {
249
- if (this.unidirectional) {
250
- this.removeAt(0);
251
- }
252
- }
253
- }
254
-
255
- Parser.classes.ConverterRuleToken = __filename;
256
- module.exports = ConverterRuleToken;