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
package/index.js CHANGED
@@ -5,17 +5,18 @@ const fs = require('fs'),
5
5
  {text} = require('./util/string');
6
6
 
7
7
  const /** @type {Parser} */ Parser = {
8
- warning: true,
9
- debugging: false,
10
- running: false,
11
-
12
8
  config: './config/default',
13
9
 
14
10
  MAX_STAGE: 11,
15
11
 
12
+ warning: true,
13
+ debugging: false,
14
+ running: false,
15
+
16
16
  classes: {},
17
17
  mixins: {},
18
18
  parsers: {},
19
+ tool: {},
19
20
 
20
21
  aliases: [
21
22
  ['AstText'],
@@ -32,6 +33,8 @@ const /** @type {Parser} */ Parser = {
32
33
  ['ConverterToken'],
33
34
  ],
34
35
  typeAliases: {
36
+ text: ['string', 'str'],
37
+ plain: ['regular', 'normal'],
35
38
  include: ['includeonly'],
36
39
  ext: ['extension'],
37
40
  'ext-attr': ['extension-attr'],
@@ -54,9 +57,12 @@ const /** @type {Parser} */ Parser = {
54
57
  'td-inner': ['table-cell-inner', 'table-data-inner'],
55
58
  'double-underscore': ['underscore', 'behavior-switch', 'behaviour-switch'],
56
59
  hr: ['horizontal'],
60
+ link: ['wikilink'],
57
61
  category: ['category-link', 'cat', 'cat-link'],
58
62
  file: ['file-link', 'image', 'image-link', 'img', 'img-link'],
63
+ 'link-text': ['wikilink-text'],
59
64
  'gallery-image': ['gallery-file', 'gallery-img'],
65
+ 'imagemap-image': ['imagemap-file', 'imagemap-img', 'image-map-image', 'image-map-file', 'image-map-img'],
60
66
  'image-parameter': ['img-parameter', 'image-param', 'img-param'],
61
67
  quote: ['quotes', 'quot', 'apostrophe', 'apostrophes', 'apos'],
62
68
  'ext-link': ['external-link'],
@@ -73,70 +79,16 @@ const /** @type {Parser} */ Parser = {
73
79
  'converter-rule-variant': ['convert-rule-variant', 'conversion-rule-variant'],
74
80
  'converter-rule-to': ['convert-rule-to', 'conversion-rule-to'],
75
81
  'converter-rule-from': ['convert-rule-from', 'conversion-rule-from'],
82
+ 'imagemap-link': ['image-map-link'],
83
+ 'param-line': ['parameter-line'],
76
84
  },
77
85
 
78
- warn(msg, ...args) {
79
- if (this.warning) {
80
- console.warn('\x1B[33m%s\x1B[0m', msg, ...args);
81
- }
82
- },
83
- debug(msg, ...args) {
84
- if (this.debugging) {
85
- console.debug('\x1B[34m%s\x1B[0m', msg, ...args);
86
- }
87
- },
88
- error(msg, ...args) {
89
- console.error('\x1B[31m%s\x1B[0m', msg, ...args);
90
- },
91
- info(msg, ...args) {
92
- console.info('\x1B[32m%s\x1B[0m', msg, ...args);
93
- },
94
-
95
- log(f) {
96
- if (typeof f === 'function') {
97
- console.log(String(f));
98
- }
99
- },
86
+ promises: [Promise.resolve()],
100
87
 
101
88
  getConfig() {
102
89
  return require(this.config);
103
90
  },
104
91
 
105
- run(callback) {
106
- const {running} = this;
107
- this.running = true;
108
- try {
109
- const result = callback();
110
- this.running = running;
111
- return result;
112
- } catch (e) {
113
- this.running = running;
114
- throw e;
115
- }
116
- },
117
-
118
- clearCache() {
119
- const entries = [
120
- ...Object.entries(this.classes),
121
- ...Object.entries(this.mixins),
122
- ...Object.entries(this.parsers),
123
- ];
124
- for (const [, filePath] of entries) {
125
- delete require.cache[require.resolve(filePath)];
126
- }
127
- for (const [name, filePath] of entries) {
128
- if (name in global) {
129
- global[name] = require(filePath);
130
- }
131
- }
132
- },
133
-
134
- isInterwiki(title, {interwiki} = Parser.getConfig()) {
135
- title = String(title);
136
- return new RegExp(`^(${interwiki.join('|')})\\s*:`, 'iu')
137
- .exec(title.replaceAll('_', ' ').replace(/^\s*:?\s*/u, ''));
138
- },
139
-
140
92
  normalizeTitle(title, defaultNs = 0, include = false, config = Parser.getConfig(), halfParsed = false) {
141
93
  let /** @type {Token} */ token;
142
94
  if (!halfParsed) {
@@ -164,7 +116,7 @@ const /** @type {Parser} */ Parser = {
164
116
  }
165
117
  };
166
118
  this.run(() => {
167
- build(['title', 'fragment']);
119
+ build(['title', 'main', 'fragment']);
168
120
  });
169
121
  }
170
122
  return titleObj;
@@ -172,7 +124,7 @@ const /** @type {Parser} */ Parser = {
172
124
 
173
125
  parse(wikitext, include, maxStage = Parser.MAX_STAGE, config = Parser.getConfig()) {
174
126
  const Token = require('./src');
175
- let token;
127
+ let /** @type {Token} */ token;
176
128
  this.run(() => {
177
129
  if (typeof wikitext === 'string') {
178
130
  token = new Token(wikitext, config);
@@ -197,9 +149,89 @@ const /** @type {Parser} */ Parser = {
197
149
  throw e;
198
150
  }
199
151
  });
152
+ if (this.debugging) {
153
+ let restored = String(token),
154
+ process = '解析';
155
+ if (restored === wikitext) {
156
+ const entities = {lt: '<', gt: '>', amp: '&'};
157
+ restored = token.print().replaceAll(
158
+ /<[^<]+?>|&([lg]t|amp);/gu,
159
+ /** @param {string} s */ (_, s) => s ? entities[s] : '',
160
+ );
161
+ process = '渲染HTML';
162
+ }
163
+ if (restored !== wikitext) {
164
+ const diff = require('./util/diff');
165
+ const {promises: {0: cur, length}} = this;
166
+ this.promises.unshift((async () => {
167
+ await cur;
168
+ this.error(`${process}过程中不可逆地修改了原始文本!`);
169
+ return diff(wikitext, restored, length);
170
+ })());
171
+ }
172
+ }
200
173
  return token;
201
174
  },
202
175
 
176
+ run(callback) {
177
+ const {running} = this;
178
+ this.running = true;
179
+ try {
180
+ const result = callback();
181
+ this.running = running;
182
+ return result;
183
+ } catch (e) {
184
+ this.running = running;
185
+ throw e;
186
+ }
187
+ },
188
+
189
+ warn(msg, ...args) {
190
+ if (this.warning) {
191
+ console.warn('\x1B[33m%s\x1B[0m', msg, ...args);
192
+ }
193
+ },
194
+ debug(msg, ...args) {
195
+ if (this.debugging) {
196
+ console.debug('\x1B[34m%s\x1B[0m', msg, ...args);
197
+ }
198
+ },
199
+ error(msg, ...args) {
200
+ console.error('\x1B[31m%s\x1B[0m', msg, ...args);
201
+ },
202
+ info(msg, ...args) {
203
+ console.info('\x1B[32m%s\x1B[0m', msg, ...args);
204
+ },
205
+
206
+ log(f) {
207
+ if (typeof f === 'function') {
208
+ console.log(String(f));
209
+ }
210
+ },
211
+
212
+ clearCache() {
213
+ const entries = [
214
+ ...Object.entries(this.classes),
215
+ ...Object.entries(this.mixins),
216
+ ...Object.entries(this.parsers),
217
+ ...Object.entries(this.tool),
218
+ ];
219
+ for (const [, filePath] of entries) {
220
+ delete require.cache[require.resolve(filePath)];
221
+ }
222
+ for (const [name, filePath] of entries) {
223
+ if (name in global) {
224
+ global[name] = require(filePath);
225
+ }
226
+ }
227
+ },
228
+
229
+ isInterwiki(title, {interwiki} = Parser.getConfig()) {
230
+ title = String(title);
231
+ return new RegExp(`^(${interwiki.join('|')})\\s*:`, 'iu')
232
+ .exec(title.replaceAll('_', ' ').replace(/^\s*:?\s*/u, ''));
233
+ },
234
+
203
235
  reparse(date) {
204
236
  const main = fs.readdirSync(path.join(__dirname, 'errors'))
205
237
  .find(name => name.startsWith(date) && name.at(-1) === 'Z');
@@ -234,9 +266,9 @@ const /** @type {Parser} */ Parser = {
234
266
 
235
267
  const /** @type {PropertyDescriptorMap} */ def = {};
236
268
  for (const key in Parser) {
237
- if (['aliases', 'MAX_STAGE', 'typeAliases'].includes(key)) {
269
+ if (['MAX_STAGE', 'aliases', 'typeAliases', 'promises'].includes(key)) {
238
270
  def[key] = {enumerable: false, writable: false};
239
- } else if (!['config', 'isInterwiki', 'normalizeTitle', 'parse', 'getTool'].includes(key)) {
271
+ } else if (!['config', 'normalizeTitle', 'parse', 'isInterwiki', 'getTool'].includes(key)) {
240
272
  def[key] = {enumerable: false};
241
273
  }
242
274
  }