wikiparser-node 0.4.0 → 0.6.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 (87) hide show
  1. package/config/default.json +129 -66
  2. package/config/zhwiki.json +4 -4
  3. package/index.js +97 -65
  4. package/lib/element.js +159 -302
  5. package/lib/node.js +384 -198
  6. package/lib/ranges.js +3 -4
  7. package/lib/text.js +65 -36
  8. package/lib/title.js +9 -8
  9. package/mixin/fixedToken.js +4 -4
  10. package/mixin/hidden.js +2 -0
  11. package/mixin/sol.js +16 -7
  12. package/package.json +14 -3
  13. package/parser/brackets.js +8 -2
  14. package/parser/commentAndExt.js +1 -1
  15. package/parser/converter.js +1 -1
  16. package/parser/externalLinks.js +2 -2
  17. package/parser/hrAndDoubleUnderscore.js +8 -7
  18. package/parser/links.js +8 -9
  19. package/parser/magicLinks.js +1 -1
  20. package/parser/selector.js +5 -5
  21. package/parser/table.js +18 -16
  22. package/src/arg.js +71 -42
  23. package/src/atom/index.js +7 -5
  24. package/src/attribute.js +102 -64
  25. package/src/charinsert.js +91 -0
  26. package/src/converter.js +34 -15
  27. package/src/converterFlags.js +87 -40
  28. package/src/converterRule.js +59 -53
  29. package/src/extLink.js +45 -37
  30. package/src/gallery.js +71 -16
  31. package/src/hasNowiki/index.js +42 -0
  32. package/src/hasNowiki/pre.js +40 -0
  33. package/src/heading.js +41 -18
  34. package/src/html.js +76 -48
  35. package/src/imageParameter.js +73 -51
  36. package/src/imagemap.js +205 -0
  37. package/src/imagemapLink.js +43 -0
  38. package/src/index.js +243 -138
  39. package/src/link/category.js +10 -14
  40. package/src/link/file.js +112 -56
  41. package/src/link/galleryImage.js +74 -10
  42. package/src/link/index.js +86 -61
  43. package/src/magicLink.js +48 -21
  44. package/src/nested/choose.js +24 -0
  45. package/src/nested/combobox.js +23 -0
  46. package/src/nested/index.js +88 -0
  47. package/src/nested/references.js +23 -0
  48. package/src/nowiki/comment.js +18 -4
  49. package/src/nowiki/dd.js +2 -2
  50. package/src/nowiki/doubleUnderscore.js +16 -11
  51. package/src/nowiki/index.js +12 -0
  52. package/src/nowiki/quote.js +28 -1
  53. package/src/onlyinclude.js +15 -8
  54. package/src/paramTag/index.js +83 -0
  55. package/src/paramTag/inputbox.js +42 -0
  56. package/src/parameter.js +73 -46
  57. package/src/syntax.js +9 -1
  58. package/src/table/index.js +58 -44
  59. package/src/table/td.js +63 -63
  60. package/src/table/tr.js +52 -35
  61. package/src/tagPair/ext.js +60 -43
  62. package/src/tagPair/include.js +11 -1
  63. package/src/tagPair/index.js +29 -20
  64. package/src/transclude.js +214 -166
  65. package/tool/index.js +720 -439
  66. package/util/base.js +17 -0
  67. package/util/debug.js +1 -1
  68. package/{test/util.js → util/diff.js} +15 -19
  69. package/util/lint.js +40 -0
  70. package/util/string.js +37 -20
  71. package/.eslintrc.json +0 -714
  72. package/errors/README +0 -1
  73. package/jsconfig.json +0 -7
  74. package/printed/README +0 -1
  75. package/printed/example.json +0 -120
  76. package/test/api.js +0 -83
  77. package/test/real.js +0 -133
  78. package/test/test.js +0 -28
  79. package/typings/api.d.ts +0 -13
  80. package/typings/array.d.ts +0 -28
  81. package/typings/event.d.ts +0 -24
  82. package/typings/index.d.ts +0 -94
  83. package/typings/node.d.ts +0 -29
  84. package/typings/parser.d.ts +0 -16
  85. package/typings/table.d.ts +0 -14
  86. package/typings/token.d.ts +0 -22
  87. package/typings/tool.d.ts +0 -11
@@ -0,0 +1,205 @@
1
+ 'use strict';
2
+
3
+ const {generateForSelf, generateForChild} = require('../util/lint'),
4
+ Parser = require('..'),
5
+ AstText = require('../lib/text'),
6
+ Token = require('.'),
7
+ NoincludeToken = require('./nowiki/noinclude'),
8
+ GalleryImageToken = require('./link/galleryImage'),
9
+ ImagemapLinkToken = require('./imagemapLink');
10
+
11
+ /**
12
+ * `<imagemap>`
13
+ * @classdesc `{childNodes: ...NoincludeToken, GalleryImageToken, ...(NoincludeToken|ImagemapLinkToken|AstText)}`
14
+ */
15
+ class ImagemapToken extends Token {
16
+ type = 'ext-inner';
17
+ name = 'imagemap';
18
+
19
+ /**
20
+ * 图片
21
+ * @returns {GalleryImageToken}
22
+ */
23
+ get image() {
24
+ return this.childNodes.find(({type}) => type === 'imagemap-image');
25
+ }
26
+
27
+ /**
28
+ * 链接
29
+ * @returns {ImagemapLinkToken[]}
30
+ */
31
+ get links() {
32
+ return this.childNodes.filter(({type}) => type === 'imagemap-link');
33
+ }
34
+
35
+ /**
36
+ * @param {string} inner 标签内部wikitext
37
+ * @param {accum} accum
38
+ * @throws `SyntaxError` 没有合法图片
39
+ */
40
+ constructor(inner, config = Parser.getConfig(), accum = []) {
41
+ super(undefined, config, true, accum, {
42
+ GalleryImageToken: ':', ImagemapLinkToken: ':', NoincludeToken: ':', AstText: ':',
43
+ });
44
+ if (!inner) {
45
+ return;
46
+ }
47
+ const lines = inner.split('\n'),
48
+ fallback = /** @param {string} line 一行文本 */ line => {
49
+ super.insertAt(new NoincludeToken(line, config, accum));
50
+ };
51
+ let first = true,
52
+ error = false;
53
+ for (const line of lines) {
54
+ const trimmed = line.trim();
55
+ if (error || !trimmed || trimmed[0] === '#') {
56
+ //
57
+ } else if (first) {
58
+ const [file, ...options] = line.split('|');
59
+ let title;
60
+ try {
61
+ title = this.normalizeTitle(decodeURIComponent(file), 0, true);
62
+ } catch {
63
+ title = this.normalizeTitle(file, 0, true);
64
+ }
65
+ if (title.valid && !title.interwiki && title.ns === 6) {
66
+ const token = new GalleryImageToken(
67
+ file, options.length > 0 ? options.join('|') : undefined, title, config, accum,
68
+ );
69
+ token.type = 'imagemap-image';
70
+ super.insertAt(token);
71
+ first = false;
72
+ continue;
73
+ } else {
74
+ Parser.error('<imagemap>标签内必须先包含一张合法图片!', line);
75
+ error = true;
76
+ }
77
+ } else if (line.trim().split(/[\t ]/u)[0] === 'desc') {
78
+ super.insertAt(new AstText(line));
79
+ continue;
80
+ } else if (line.includes('[')) {
81
+ const i = line.indexOf('['),
82
+ substr = line.slice(i),
83
+ mtIn = /^\[{2}([^|]+)(?:\|([^\]]+))?\]{2}[\w\s]*$/u.exec(substr);
84
+ if (mtIn) {
85
+ const title = this.normalizeTitle(mtIn[1], 0, true);
86
+ if (title.valid) {
87
+ super.insertAt(new ImagemapLinkToken(
88
+ line.slice(0, i),
89
+ [...mtIn.slice(1), title],
90
+ substr.slice(substr.indexOf(']]') + 2),
91
+ config,
92
+ accum,
93
+ ));
94
+ continue;
95
+ }
96
+ } else {
97
+ const protocols = config.protocol.split('|');
98
+ if (protocols.includes(substr.slice(1, substr.indexOf(':') + 1))
99
+ || protocols.includes(substr.slice(1, substr.indexOf('//') + 2))
100
+ ) {
101
+ const mtEx = /^\[([^\]\s]+)(?:(\s+)(\S[^\]]*)?)?\][\w\s]*$/u.exec(substr);
102
+ if (mtEx) {
103
+ super.insertAt(new ImagemapLinkToken(
104
+ line.slice(0, i),
105
+ mtEx.slice(1),
106
+ substr.slice(substr.indexOf(']') + 1),
107
+ config,
108
+ accum,
109
+ ));
110
+ continue;
111
+ }
112
+ }
113
+ }
114
+ }
115
+ fallback(line);
116
+ }
117
+ }
118
+
119
+ /**
120
+ * @override
121
+ * @param {string} selector
122
+ */
123
+ toString(selector) {
124
+ return super.toString(selector, '\n');
125
+ }
126
+
127
+ /** @override */
128
+ getGaps() {
129
+ return 1;
130
+ }
131
+
132
+ /** @override */
133
+ print() {
134
+ return super.print({sep: '\n'});
135
+ }
136
+
137
+ /**
138
+ * @override
139
+ * @param {number} start 起始位置
140
+ */
141
+ lint(start = 0) {
142
+ const errors = super.lint(start),
143
+ rect = this.getRootNode().posFromIndex(start);
144
+ if (this.image) {
145
+ errors.push(
146
+ ...this.childNodes.filter(child => {
147
+ const str = String(child).trim();
148
+ return child.type === 'noinclude' && str && str[0] !== '#';
149
+ }).map(child => generateForChild(child, rect, '无效的<imagemap>链接')),
150
+ );
151
+ } else {
152
+ errors.push(generateForSelf(this, rect, '缺少图片的<imagemap>'));
153
+ }
154
+ return errors;
155
+ }
156
+
157
+ /**
158
+ * @override
159
+ * @template {string|Token} T
160
+ * @param {T} token 待插入的节点
161
+ * @param {number} i 插入位置
162
+ * @throws `Error` 当前缺少合法图片
163
+ * @throws `RangeError` 已有一张合法图片
164
+ */
165
+ insertAt(token, i = 0) {
166
+ const {image} = this;
167
+ if (!image && (token.type === 'imagemap-link' || token.type === 'text')) {
168
+ throw new Error('当前缺少一张合法图片!');
169
+ } else if (image && token.type === 'imagemap-image') {
170
+ throw new RangeError('已有一张合法图片!');
171
+ }
172
+ return super.insertAt(token, i);
173
+ }
174
+
175
+ /**
176
+ * @override
177
+ * @param {number} i 移除位置
178
+ * @throws `Error` 禁止移除图片
179
+ */
180
+ removeAt(i) {
181
+ const child = this.childNodes[i];
182
+ if (child.type === 'imagemap-image') {
183
+ throw new Error('禁止移除<imagemap>内的图片!');
184
+ }
185
+ return super.removeAt(i);
186
+ }
187
+
188
+ /** @override */
189
+ cloneNode() {
190
+ const cloned = this.cloneChildNodes();
191
+ return Parser.run(() => {
192
+ const token = new ImagemapToken(undefined, this.getAttribute('config'));
193
+ token.append(...cloned);
194
+ return token;
195
+ });
196
+ }
197
+
198
+ /** @override */
199
+ text() {
200
+ return super.text('\n').replaceAll(/\n{2,}/gu, '\n');
201
+ }
202
+ }
203
+
204
+ Parser.classes.ImagemapToken = __filename;
205
+ module.exports = ImagemapToken;
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ const fixedToken = require('../mixin/fixedToken'),
4
+ Parser = require('..'),
5
+ Token = require('.'),
6
+ LinkToken = require('./link'),
7
+ ExtLinkToken = require('./extLink');
8
+
9
+ /**
10
+ * `<imagemap>`内的链接
11
+ * @classdesc `{childNodes: [AstText, LinkToken|ExtLinkToken, NoincludeToken]}`
12
+ */
13
+ class ImagemapLinkToken extends fixedToken(Token) {
14
+ type = 'imagemap-link';
15
+
16
+ /**
17
+ * 内外链接
18
+ * @this {{childNodes: (LinkToken|ExtLinkToken)[]}}
19
+ */
20
+ get link() {
21
+ return this.childNodes[1].link;
22
+ }
23
+
24
+ /**
25
+ * @param {string} pre 链接前的文本
26
+ * @param {[string, string, string|Title]} linkStuff 内外链接
27
+ * @param {string} post 链接后的文本
28
+ * @param {accum} accum
29
+ */
30
+ constructor(pre, linkStuff, post, config, accum) {
31
+ const Title = require('../lib/title'),
32
+ AstText = require('../lib/text'),
33
+ NoincludeToken = require('./nowiki/noinclude');
34
+ const SomeLinkToken = linkStuff[2] instanceof Title ? LinkToken : ExtLinkToken;
35
+ super(undefined, config, true, accum);
36
+ this.append(
37
+ new AstText(pre), new SomeLinkToken(...linkStuff, config, accum), new NoincludeToken(post, config, accum),
38
+ );
39
+ }
40
+ }
41
+
42
+ Parser.classes.ImagemapLinkToken = __filename;
43
+ module.exports = ImagemapLinkToken;