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