wikiparser-node 0.7.0 → 0.8.0-b

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/bundle/bundle.min.js +38 -0
  2. package/extensions/editor.css +60 -0
  3. package/extensions/editor.js +317 -0
  4. package/extensions/ui.css +119 -0
  5. package/package.json +12 -11
  6. package/README.md +0 -39
  7. package/config/default.json +0 -832
  8. package/config/llwiki.json +0 -630
  9. package/config/moegirl.json +0 -728
  10. package/config/zhwiki.json +0 -1269
  11. package/index.js +0 -321
  12. package/lib/element.js +0 -611
  13. package/lib/node.js +0 -772
  14. package/lib/ranges.js +0 -130
  15. package/lib/text.js +0 -215
  16. package/lib/title.js +0 -80
  17. package/mixin/attributeParent.js +0 -117
  18. package/mixin/fixedToken.js +0 -40
  19. package/mixin/hidden.js +0 -21
  20. package/mixin/singleLine.js +0 -31
  21. package/mixin/sol.js +0 -65
  22. package/parser/brackets.js +0 -120
  23. package/parser/commentAndExt.js +0 -62
  24. package/parser/converter.js +0 -46
  25. package/parser/externalLinks.js +0 -33
  26. package/parser/hrAndDoubleUnderscore.js +0 -38
  27. package/parser/html.js +0 -42
  28. package/parser/links.js +0 -94
  29. package/parser/list.js +0 -59
  30. package/parser/magicLinks.js +0 -41
  31. package/parser/quotes.js +0 -64
  32. package/parser/selector.js +0 -177
  33. package/parser/table.js +0 -114
  34. package/src/arg.js +0 -203
  35. package/src/atom/hidden.js +0 -13
  36. package/src/atom/index.js +0 -43
  37. package/src/attribute.js +0 -420
  38. package/src/attributes.js +0 -452
  39. package/src/charinsert.js +0 -97
  40. package/src/converter.js +0 -176
  41. package/src/converterFlags.js +0 -284
  42. package/src/converterRule.js +0 -258
  43. package/src/extLink.js +0 -179
  44. package/src/gallery.js +0 -151
  45. package/src/hasNowiki/index.js +0 -44
  46. package/src/hasNowiki/pre.js +0 -40
  47. package/src/heading.js +0 -134
  48. package/src/html.js +0 -248
  49. package/src/imageParameter.js +0 -277
  50. package/src/imagemap.js +0 -199
  51. package/src/imagemapLink.js +0 -41
  52. package/src/index.js +0 -913
  53. package/src/link/category.js +0 -49
  54. package/src/link/file.js +0 -282
  55. package/src/link/galleryImage.js +0 -120
  56. package/src/link/index.js +0 -383
  57. package/src/magicLink.js +0 -149
  58. package/src/nested/choose.js +0 -24
  59. package/src/nested/combobox.js +0 -23
  60. package/src/nested/index.js +0 -96
  61. package/src/nested/references.js +0 -23
  62. package/src/nowiki/comment.js +0 -71
  63. package/src/nowiki/dd.js +0 -59
  64. package/src/nowiki/doubleUnderscore.js +0 -56
  65. package/src/nowiki/hr.js +0 -41
  66. package/src/nowiki/index.js +0 -56
  67. package/src/nowiki/list.js +0 -16
  68. package/src/nowiki/noinclude.js +0 -28
  69. package/src/nowiki/quote.js +0 -69
  70. package/src/onlyinclude.js +0 -64
  71. package/src/paramTag/index.js +0 -89
  72. package/src/paramTag/inputbox.js +0 -44
  73. package/src/parameter.js +0 -239
  74. package/src/syntax.js +0 -91
  75. package/src/table/index.js +0 -984
  76. package/src/table/td.js +0 -339
  77. package/src/table/tr.js +0 -319
  78. package/src/tagPair/ext.js +0 -138
  79. package/src/tagPair/include.js +0 -60
  80. package/src/tagPair/index.js +0 -126
  81. package/src/transclude.js +0 -824
  82. package/tool/index.js +0 -1202
  83. package/util/base.js +0 -17
  84. package/util/debug.js +0 -73
  85. package/util/diff.js +0 -76
  86. package/util/lint.js +0 -54
  87. package/util/string.js +0 -107
package/index.js DELETED
@@ -1,321 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('fs'),
4
- path = require('path');
5
-
6
- const /** @type {Parser} */ Parser = {
7
- config: './config/default',
8
-
9
- MAX_STAGE: 11,
10
-
11
- warning: true,
12
- debugging: false,
13
- running: false,
14
-
15
- classes: {},
16
- mixins: {},
17
- parsers: {},
18
- tool: {},
19
-
20
- aliases: [
21
- ['AstText'],
22
- ['CommentToken', 'ExtToken', 'IncludeToken', 'NoincludeToken'],
23
- ['ArgToken', 'TranscludeToken', 'HeadingToken'],
24
- ['HtmlToken'],
25
- ['TableToken'],
26
- ['HrToken', 'DoubleUnderscoreToken'],
27
- ['LinkToken', 'FileToken', 'CategoryToken'],
28
- ['QuoteToken'],
29
- ['ExtLinkToken'],
30
- ['MagicLinkToken'],
31
- ['ListToken', 'DdToken'],
32
- ['ConverterToken'],
33
- ],
34
- typeAliases: {
35
- text: ['string', 'str'],
36
- plain: ['regular', 'normal'],
37
- // comment and extension
38
- onlyinclude: ['only-include'],
39
- noinclude: ['no-include'],
40
- include: ['includeonly', 'include-only'],
41
- comment: undefined,
42
- ext: ['extension'],
43
- 'ext-attrs': ['extension-attrs', 'ext-attributes', 'extension-attributes'],
44
- 'ext-attr-dirty': ['extension-attr-dirty', 'ext-attribute-dirty', 'extension-attribute-dirty'],
45
- 'ext-attr': ['extension-attr', 'ext-attribute', 'extension-attribute'],
46
- 'attr-key': ['attribute-key'],
47
- 'attr-value': ['attribute-value', 'attr-val', 'attribute-val'],
48
- 'ext-inner': ['extension-inner'],
49
- // triple brackets
50
- arg: ['argument'],
51
- 'arg-name': ['argument-name'],
52
- 'arg-default': ['argument-default'],
53
- hidden: ['arg-redundant'],
54
- // double brackets
55
- 'magic-word': ['parser-function', 'parser-func'],
56
- 'magic-word-name': ['parser-function-name', 'parser-func-name'],
57
- 'invoke-function': ['invoke-func', 'lua-function', 'lua-func', 'module-function', 'module-func'],
58
- 'invoke-module': ['lua-module'],
59
- template: undefined,
60
- 'template-name': undefined,
61
- parameter: ['param'],
62
- 'parameter-key': ['param-key'],
63
- 'parameter-value': ['parameter-val', 'param-value', 'param-val'],
64
- // heading
65
- heading: ['header'],
66
- 'heading-title': ['header-title'],
67
- 'heading-trail': ['header-trail'],
68
- // html
69
- html: undefined,
70
- 'html-attrs': ['html-attributes'],
71
- 'html-attr-dirty': ['html-attribute-dirty'],
72
- 'html-attr': ['html-attribute'],
73
- // table
74
- table: undefined,
75
- tr: ['table-row'],
76
- td: ['table-cell', 'table-data'],
77
- 'table-syntax': undefined,
78
- 'table-attrs': ['tr-attrs', 'td-attrs', 'table-attributes', 'tr-attributes', 'td-attributes'],
79
- 'table-attr-dirty':
80
- ['tr-attr-dirty', 'td-attr-dirty', 'table-attribute-dirty', 'tr-attribute-dirty', 'td-attribute-dirty'],
81
- 'table-attr': ['tr-attr', 'td-attr', 'table-attribute', 'tr-attribute', 'td-attribute'],
82
- 'table-inter': undefined,
83
- 'td-inner': ['table-cell-inner', 'table-data-inner'],
84
- // hr and double-underscore
85
- hr: ['horizontal'],
86
- 'double-underscore': ['underscore', 'behavior-switch', 'behaviour-switch'],
87
- // link
88
- link: ['wikilink'],
89
- 'link-target': ['wikilink-target'],
90
- 'link-text': ['wikilink-text'],
91
- category: ['category-link', 'cat', 'cat-link'],
92
- file: ['file-link', 'image', 'image-link', 'img', 'img-link'],
93
- 'gallery-image': ['gallery-file', 'gallery-img'],
94
- 'imagemap-image': ['imagemap-file', 'imagemap-img', 'image-map-image', 'image-map-file', 'image-map-img'],
95
- 'image-parameter': ['img-parameter', 'image-param', 'img-param'],
96
- // quotes
97
- quote: ['quotes', 'quot', 'apostrophe', 'apostrophes', 'apos'],
98
- // external link
99
- 'ext-link': ['external-link'],
100
- 'ext-link-text': ['external-link-text'],
101
- 'ext-link-url': ['external-link-url'],
102
- // magic link
103
- 'free-ext-link': ['free-external-link', 'magic-link'],
104
- // list
105
- list: ['ol', 'ordered-list', 'ul', 'unordered-list', 'dl', 'description-list'],
106
- dd: ['indent', 'indentation'],
107
- // converter
108
- converter: ['convert', 'conversion'],
109
- 'converter-flags': ['convert-flags', 'conversion-flags'],
110
- 'converter-flag': ['convert-flag', 'conversion-flag'],
111
- 'converter-rule': ['convert-rule', 'conversion-rule'],
112
- 'converter-rule-noconvert': ['convert-rule-noconvert', 'conversion-rule-noconvert'],
113
- 'converter-rule-variant': ['convert-rule-variant', 'conversion-rule-variant'],
114
- 'converter-rule-to': ['convert-rule-to', 'conversion-rule-to'],
115
- 'converter-rule-from': ['convert-rule-from', 'conversion-rule-from'],
116
- // specific extensions
117
- 'param-line': ['parameter-line'],
118
- 'charinsert-line': undefined,
119
- 'imagemap-link': ['image-map-link'],
120
- },
121
-
122
- promises: [Promise.resolve()],
123
-
124
- getConfig() {
125
- return require(this.config);
126
- },
127
-
128
- normalizeTitle(
129
- title,
130
- defaultNs = 0,
131
- include = false,
132
- config = Parser.getConfig(),
133
- halfParsed = false,
134
- decode = false,
135
- selfLink = false,
136
- ) {
137
- let /** @type {Token} */ token;
138
- if (!halfParsed) {
139
- const Token = require('./src');
140
- token = this.run(() => {
141
- const newToken = new Token(String(title), config),
142
- parseOnce = newToken.getAttribute('parseOnce');
143
- parseOnce(0, include);
144
- return parseOnce();
145
- });
146
- title = token.firstChild;
147
- }
148
- const Title = require('./lib/title');
149
- const titleObj = new Title(String(title), defaultNs, config, decode, selfLink);
150
- if (token) {
151
- /**
152
- * 重建部分属性值
153
- * @param {string[]} keys 属性键
154
- */
155
- const build = keys => {
156
- for (const key of keys) {
157
- if (titleObj[key].includes('\0')) {
158
- titleObj[key] = token.getAttribute('buildFromStr')(titleObj[key], 'text');
159
- }
160
- }
161
- };
162
- this.run(() => {
163
- build(['title', 'main', 'fragment']);
164
- });
165
- }
166
- return titleObj;
167
- },
168
-
169
- parse(wikitext, include, maxStage = Parser.MAX_STAGE, config = Parser.getConfig()) {
170
- if (typeof wikitext !== 'string') {
171
- throw new TypeError('待解析的内容应为 String!');
172
- }
173
- const Token = require('./src');
174
- let /** @type {Token} */ token;
175
- this.run(() => {
176
- token = new Token(wikitext, config);
177
- try {
178
- token.parse(maxStage, include);
179
- } catch (e) {
180
- if (e instanceof Error) {
181
- const file = path.join(__dirname, 'errors', new Date().toISOString()),
182
- stage = token.getAttribute('stage');
183
- fs.writeFileSync(file, stage === this.MAX_STAGE ? wikitext : String(token));
184
- fs.writeFileSync(`${file}.err`, e.stack);
185
- fs.writeFileSync(`${file}.json`, JSON.stringify({
186
- stage, include: token.getAttribute('include'), config: this.config,
187
- }, null, '\t'));
188
- }
189
- throw e;
190
- }
191
- });
192
- if (this.debugging) {
193
- let restored = String(token),
194
- process = '解析';
195
- if (restored === wikitext) {
196
- const entities = {lt: '<', gt: '>', amp: '&'};
197
- restored = token.print().replace(
198
- /<[^<]+?>|&([lg]t|amp);/gu,
199
- /** @param {string} s */ (_, s) => s ? entities[s] : '',
200
- );
201
- process = '渲染HTML';
202
- }
203
- if (restored !== wikitext) {
204
- const diff = require('./util/diff');
205
- const {promises: {0: cur, length}} = this;
206
- this.promises.unshift((async () => {
207
- await cur;
208
- this.error(`${process}过程中不可逆地修改了原始文本!`);
209
- return diff(wikitext, restored, length);
210
- })());
211
- }
212
- }
213
- return token;
214
- },
215
-
216
- run(callback) {
217
- const {running} = this;
218
- this.running = true;
219
- try {
220
- const result = callback();
221
- this.running = running;
222
- return result;
223
- } catch (e) {
224
- this.running = running;
225
- throw e;
226
- }
227
- },
228
-
229
- warn(msg, ...args) {
230
- if (this.warning) {
231
- console.warn('\x1B[33m%s\x1B[0m', msg, ...args);
232
- }
233
- },
234
- debug(msg, ...args) {
235
- if (this.debugging) {
236
- console.debug('\x1B[34m%s\x1B[0m', msg, ...args);
237
- }
238
- },
239
- error(msg, ...args) {
240
- console.error('\x1B[31m%s\x1B[0m', msg, ...args);
241
- },
242
- info(msg, ...args) {
243
- console.info('\x1B[32m%s\x1B[0m', msg, ...args);
244
- },
245
-
246
- log(f) {
247
- if (typeof f === 'function') {
248
- console.log(String(f));
249
- }
250
- },
251
-
252
- clearCache() {
253
- const entries = [
254
- ...Object.entries(this.classes),
255
- ...Object.entries(this.mixins),
256
- ...Object.entries(this.parsers),
257
- ...Object.entries(this.tool),
258
- ];
259
- for (const [, filePath] of entries) {
260
- try {
261
- delete require.cache[require.resolve(filePath)];
262
- } catch {}
263
- }
264
- for (const [name, filePath] of entries) {
265
- if (name in global) {
266
- global[name] = require(filePath);
267
- }
268
- }
269
- },
270
-
271
- isInterwiki(title, {interwiki} = Parser.getConfig()) {
272
- title = String(title);
273
- return new RegExp(`^(${interwiki.join('|')})\\s*:`, 'iu')
274
- .exec(title.replaceAll('_', ' ').replace(/^\s*:?\s*/u, ''));
275
- },
276
-
277
- reparse(date) {
278
- const main = fs.readdirSync(path.join(__dirname, 'errors'))
279
- .find(name => name.startsWith(date) && name.at(-1) === 'Z');
280
- if (!main) {
281
- throw new RangeError(`找不到对应时间戳的错误记录:${date}`);
282
- }
283
- const file = path.join(__dirname, 'errors', main),
284
- wikitext = fs.readFileSync(file, 'utf8');
285
- const {stage, include, config} = require(`${file}.json`),
286
- Token = require('./src');
287
- this.config = config;
288
- return this.run(() => {
289
- const halfParsed = stage < this.MAX_STAGE,
290
- token = new Token(wikitext, this.getConfig(), halfParsed);
291
- if (halfParsed) {
292
- token.setAttribute('stage', stage).getAttribute('parseOnce')(stage, include);
293
- } else {
294
- token.parse(undefined, include);
295
- }
296
- fs.unlinkSync(file);
297
- fs.unlinkSync(`${file}.err`);
298
- fs.unlinkSync(`${file}.json`);
299
- return token;
300
- });
301
- },
302
-
303
- getTool() {
304
- delete require.cache[require.resolve('./tool')];
305
- return require('./tool');
306
- },
307
- };
308
-
309
- const /** @type {PropertyDescriptorMap} */ def = {},
310
- immutable = new Set(['MAX_STAGE', 'aliases', 'typeAliases', 'promises']),
311
- enumerable = new Set(['config', 'normalizeTitle', 'parse', 'isInterwiki', 'getTool']);
312
- for (const key in Parser) {
313
- if (immutable.has(key)) {
314
- def[key] = {enumerable: false, writable: false};
315
- } else if (!enumerable.has(key)) {
316
- def[key] = {enumerable: false};
317
- }
318
- }
319
- Object.defineProperties(Parser, def);
320
-
321
- module.exports = Parser;