wikiparser-node 0.8.0 → 0.8.1-m

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 (77) hide show
  1. package/config/minimum.json +142 -0
  2. package/index.js +11 -253
  3. package/lib/element.js +7 -481
  4. package/lib/node.js +6 -552
  5. package/lib/text.js +70 -110
  6. package/lib/title.js +0 -21
  7. package/mixin/hidden.js +0 -3
  8. package/package.json +5 -5
  9. package/parser/brackets.js +0 -1
  10. package/parser/commentAndExt.js +3 -4
  11. package/parser/converter.js +0 -1
  12. package/parser/externalLinks.js +0 -1
  13. package/parser/hrAndDoubleUnderscore.js +0 -1
  14. package/parser/html.js +0 -1
  15. package/parser/links.js +4 -5
  16. package/parser/list.js +0 -1
  17. package/parser/magicLinks.js +4 -5
  18. package/parser/quotes.js +1 -2
  19. package/parser/table.js +0 -1
  20. package/src/arg.js +2 -116
  21. package/src/atom/hidden.js +0 -2
  22. package/src/atom/index.js +0 -17
  23. package/src/attribute.js +15 -182
  24. package/src/attributes.js +4 -308
  25. package/src/converter.js +2 -108
  26. package/src/converterFlags.js +0 -187
  27. package/src/converterRule.js +1 -184
  28. package/src/extLink.js +1 -120
  29. package/src/gallery.js +6 -57
  30. package/src/hasNowiki/index.js +0 -12
  31. package/src/hasNowiki/pre.js +0 -12
  32. package/src/heading.js +4 -55
  33. package/src/html.js +3 -118
  34. package/src/imageParameter.js +19 -189
  35. package/src/imagemap.js +1 -60
  36. package/src/imagemapLink.js +1 -13
  37. package/src/index.js +3 -529
  38. package/src/link/category.js +1 -37
  39. package/src/link/file.js +2 -159
  40. package/src/link/galleryImage.js +1 -59
  41. package/src/link/index.js +1 -259
  42. package/src/magicLink.js +9 -90
  43. package/src/nested/choose.js +0 -1
  44. package/src/nested/combobox.js +0 -1
  45. package/src/nested/index.js +3 -30
  46. package/src/nested/references.js +0 -1
  47. package/src/nowiki/comment.js +1 -25
  48. package/src/nowiki/dd.js +1 -47
  49. package/src/nowiki/doubleUnderscore.js +1 -31
  50. package/src/nowiki/hr.js +1 -20
  51. package/src/nowiki/index.js +1 -23
  52. package/src/nowiki/list.js +2 -5
  53. package/src/nowiki/noinclude.js +0 -14
  54. package/src/nowiki/quote.js +2 -16
  55. package/src/onlyinclude.js +1 -26
  56. package/src/paramTag/index.js +1 -24
  57. package/src/paramTag/inputbox.js +1 -4
  58. package/src/parameter.js +6 -148
  59. package/src/syntax.js +0 -68
  60. package/src/table/index.js +2 -940
  61. package/src/table/td.js +5 -225
  62. package/src/table/tr.js +2 -247
  63. package/src/tagPair/ext.js +3 -24
  64. package/src/tagPair/include.js +0 -24
  65. package/src/tagPair/index.js +2 -51
  66. package/src/transclude.js +20 -519
  67. package/util/string.js +1 -48
  68. package/README.md +0 -39
  69. package/lib/ranges.js +0 -130
  70. package/mixin/attributeParent.js +0 -117
  71. package/mixin/fixedToken.js +0 -40
  72. package/mixin/singleLine.js +0 -31
  73. package/mixin/sol.js +0 -65
  74. package/parser/selector.js +0 -177
  75. package/src/charinsert.js +0 -97
  76. package/tool/index.js +0 -1202
  77. package/util/debug.js +0 -73
@@ -0,0 +1,142 @@
1
+ {
2
+ "ext": [],
3
+ "html": [
4
+ [
5
+ "b",
6
+ "bdi",
7
+ "del",
8
+ "i",
9
+ "ins",
10
+ "u",
11
+ "font",
12
+ "big",
13
+ "small",
14
+ "sub",
15
+ "sup",
16
+ "h1",
17
+ "h2",
18
+ "h3",
19
+ "h4",
20
+ "h5",
21
+ "h6",
22
+ "cite",
23
+ "code",
24
+ "em",
25
+ "s",
26
+ "strike",
27
+ "strong",
28
+ "tt",
29
+ "var",
30
+ "div",
31
+ "center",
32
+ "blockquote",
33
+ "ol",
34
+ "ul",
35
+ "dl",
36
+ "table",
37
+ "caption",
38
+ "pre",
39
+ "ruby",
40
+ "rb",
41
+ "rp",
42
+ "rt",
43
+ "rtc",
44
+ "p",
45
+ "span",
46
+ "abbr",
47
+ "dfn",
48
+ "kbd",
49
+ "samp",
50
+ "data",
51
+ "time",
52
+ "mark",
53
+ "tr",
54
+ "td",
55
+ "th",
56
+ "q",
57
+ "bdo"
58
+ ],
59
+ [
60
+ "li",
61
+ "dt",
62
+ "dd"
63
+ ],
64
+ [
65
+ "br",
66
+ "wbr",
67
+ "hr",
68
+ "meta",
69
+ "link",
70
+ "img"
71
+ ]
72
+ ],
73
+ "namespaces": {},
74
+ "nsid": {},
75
+ "parserFunction": [
76
+ {
77
+ "#language": "language",
78
+ "#special": "special",
79
+ "#speciale": "speciale",
80
+ "#tag": "tag",
81
+ "#formatdate": "formatdate",
82
+ "#invoke": "invoke",
83
+ "#while": "while",
84
+ "#dowhile": "dowhile",
85
+ "#loop": "loop",
86
+ "#forargs": "forargs",
87
+ "#fornumargs": "fornumargs",
88
+ "#if": "if",
89
+ "#ifeq": "ifeq",
90
+ "#switch": "switch",
91
+ "#ifexist": "ifexist",
92
+ "#ifexpr": "ifexpr",
93
+ "#iferror": "iferror",
94
+ "#time": "time",
95
+ "#timel": "timel",
96
+ "#expr": "expr",
97
+ "#rel2abs": "rel2abs",
98
+ "#titleparts": "titleparts",
99
+ "#categorytree": "categorytree",
100
+ "#urldecode": "urldecode",
101
+ "#choose": "choose",
102
+ "#var": "var",
103
+ "#varexists": "varexists",
104
+ "#var_final": "var_final",
105
+ "#vardefine": "vardefine",
106
+ "#vardefineecho": "vardefineecho",
107
+ "#widget": "widget",
108
+ "#related": "related",
109
+ "#cscore": "cscore"
110
+ },
111
+ [],
112
+ [
113
+ "msg",
114
+ "原始",
115
+ "raw"
116
+ ],
117
+ [
118
+ "替代",
119
+ "subst",
120
+ "安全替代",
121
+ "safesubst"
122
+ ]
123
+ ],
124
+ "doubleUnderscore": [
125
+ [],
126
+ []
127
+ ],
128
+ "protocol": "bitcoin:|ftp://|ftps://|geo:|git://|gopher://|http://|https://|irc://|ircs://|magnet:|mailto:|mms://|news:|nntp://|redis://|sftp://|sip:|sips:|sms:|ssh://|svn://|tel:|telnet://|urn:|worldwind://|xmpp:",
129
+ "interwiki": [],
130
+ "img": {},
131
+ "variants": [
132
+ "zh",
133
+ "zh-hans",
134
+ "zh-hant",
135
+ "zh-cn",
136
+ "zh-tw",
137
+ "zh-hk",
138
+ "zh-sg",
139
+ "zh-my",
140
+ "zh-mo"
141
+ ]
142
+ }
package/index.js CHANGED
@@ -1,128 +1,16 @@
1
1
  'use strict';
2
2
 
3
- const fs = require('fs'),
4
- path = require('path');
5
-
6
3
  const /** @type {Parser} */ Parser = {
7
- config: './config/default',
4
+ config: undefined,
5
+ minConfig: require('./config/minimum'),
8
6
 
9
7
  MAX_STAGE: 11,
10
8
 
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), excludes: []};
9
+ getConfig(path) {
10
+ if (path) {
11
+ this.config = require(path);
12
+ }
13
+ return {...this.minConfig, ...this.config, excludes: []};
126
14
  },
127
15
 
128
16
  normalizeTitle(
@@ -147,22 +35,6 @@ const /** @type {Parser} */ Parser = {
147
35
  }
148
36
  const Title = require('./lib/title');
149
37
  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
38
  return titleObj;
167
39
  },
168
40
 
@@ -176,96 +48,13 @@ const /** @type {Parser} */ Parser = {
176
48
  token = new Token(wikitext, config);
177
49
  try {
178
50
  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
- }
51
+ } catch {}
191
52
  });
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
53
  return token;
214
54
  },
215
55
 
216
56
  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
- }
57
+ return callback();
269
58
  },
270
59
 
271
60
  isInterwiki(title, {interwiki} = Parser.getConfig()) {
@@ -273,42 +62,11 @@ const /** @type {Parser} */ Parser = {
273
62
  return new RegExp(`^(${interwiki.join('|')})\\s*:`, 'iu')
274
63
  .exec(title.replaceAll('_', ' ').replace(/^\s*:?\s*/u, ''));
275
64
  },
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
65
  };
308
66
 
309
67
  const /** @type {PropertyDescriptorMap} */ def = {},
310
- immutable = new Set(['MAX_STAGE', 'aliases', 'typeAliases', 'promises']),
311
- enumerable = new Set(['config', 'normalizeTitle', 'parse', 'isInterwiki', 'getTool']);
68
+ immutable = new Set(['MAX_STAGE', 'minConfig']),
69
+ enumerable = new Set(['config', 'normalizeTitle', 'parse', 'isInterwiki']);
312
70
  for (const key in Parser) {
313
71
  if (immutable.has(key)) {
314
72
  def[key] = {enumerable: false, writable: false};