wikiparser-node 0.0.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 (65) hide show
  1. package/.eslintrc.json +229 -0
  2. package/LICENSE +674 -0
  3. package/README.md +1896 -0
  4. package/config/default.json +766 -0
  5. package/config/llwiki.json +686 -0
  6. package/config/moegirl.json +721 -0
  7. package/index.js +159 -0
  8. package/jsconfig.json +7 -0
  9. package/lib/element.js +690 -0
  10. package/lib/node.js +357 -0
  11. package/lib/ranges.js +122 -0
  12. package/lib/title.js +57 -0
  13. package/mixin/attributeParent.js +67 -0
  14. package/mixin/fixedToken.js +32 -0
  15. package/mixin/hidden.js +22 -0
  16. package/package.json +30 -0
  17. package/parser/brackets.js +107 -0
  18. package/parser/commentAndExt.js +61 -0
  19. package/parser/externalLinks.js +30 -0
  20. package/parser/hrAndDoubleUnderscore.js +26 -0
  21. package/parser/html.js +41 -0
  22. package/parser/links.js +92 -0
  23. package/parser/magicLinks.js +40 -0
  24. package/parser/quotes.js +63 -0
  25. package/parser/table.js +97 -0
  26. package/src/arg.js +150 -0
  27. package/src/atom/hidden.js +10 -0
  28. package/src/atom/index.js +33 -0
  29. package/src/attribute.js +342 -0
  30. package/src/extLink.js +116 -0
  31. package/src/heading.js +91 -0
  32. package/src/html.js +144 -0
  33. package/src/imageParameter.js +172 -0
  34. package/src/index.js +602 -0
  35. package/src/link/category.js +88 -0
  36. package/src/link/file.js +201 -0
  37. package/src/link/index.js +214 -0
  38. package/src/listToken.js +47 -0
  39. package/src/magicLink.js +66 -0
  40. package/src/nowiki/comment.js +45 -0
  41. package/src/nowiki/doubleUnderscore.js +42 -0
  42. package/src/nowiki/hr.js +41 -0
  43. package/src/nowiki/index.js +37 -0
  44. package/src/nowiki/noinclude.js +24 -0
  45. package/src/nowiki/quote.js +37 -0
  46. package/src/onlyinclude.js +42 -0
  47. package/src/parameter.js +165 -0
  48. package/src/syntax.js +80 -0
  49. package/src/table/index.js +867 -0
  50. package/src/table/td.js +259 -0
  51. package/src/table/tr.js +244 -0
  52. package/src/tagPair/ext.js +85 -0
  53. package/src/tagPair/include.js +45 -0
  54. package/src/tagPair/index.js +91 -0
  55. package/src/transclude.js +627 -0
  56. package/tool/index.js +898 -0
  57. package/typings/element.d.ts +28 -0
  58. package/typings/index.d.ts +49 -0
  59. package/typings/node.d.ts +23 -0
  60. package/typings/parser.d.ts +9 -0
  61. package/typings/table.d.ts +14 -0
  62. package/typings/token.d.ts +21 -0
  63. package/typings/tool.d.ts +10 -0
  64. package/util/debug.js +70 -0
  65. package/util/string.js +60 -0
package/index.js ADDED
@@ -0,0 +1,159 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs'),
4
+ {text} = require('./util/string');
5
+
6
+ const /** @type {Parser} */ Parser = {
7
+ warning: true,
8
+ debugging: false,
9
+
10
+ warn(msg, ...args) {
11
+ if (this.warning) {
12
+ console.warn('\x1b[33m%s\x1b[0m', msg, ...args);
13
+ }
14
+ },
15
+ debug(msg, ...args) {
16
+ if (this.debugging) {
17
+ console.debug('\x1b[34m%s\x1b[0m', msg, ...args);
18
+ }
19
+ },
20
+ error(msg, ...args) {
21
+ console.error('\x1b[31m%s\x1b[0m', msg, ...args);
22
+ },
23
+ info(msg, ...args) {
24
+ console.info('\x1b[32m%s\x1b[0m', msg, ...args);
25
+ },
26
+
27
+ running: false,
28
+
29
+ run(callback) {
30
+ const {running} = this;
31
+ this.running = true;
32
+ try {
33
+ const result = callback();
34
+ this.running = running;
35
+ return result;
36
+ } catch (e) {
37
+ this.running = running;
38
+ throw e;
39
+ }
40
+ },
41
+
42
+ classes: {},
43
+ mixins: {},
44
+ parsers: {},
45
+
46
+ clearCache() {
47
+ const entries = [
48
+ ...Object.entries(this.classes),
49
+ ...Object.entries(this.mixins),
50
+ ...Object.entries(this.parsers),
51
+ ];
52
+ for (const [, path] of entries) {
53
+ delete require.cache[require.resolve(path)];
54
+ }
55
+ for (const [name, path] of entries) {
56
+ if (name in global) {
57
+ global[name] = require(path);
58
+ }
59
+ }
60
+ },
61
+
62
+ log(f) {
63
+ if (typeof f === 'function') {
64
+ console.log(f.toString());
65
+ }
66
+ },
67
+
68
+ aliases: [
69
+ ['String'],
70
+ ['CommentToken', 'ExtToken', 'IncludeToken', 'NoincludeToken'],
71
+ ['ArgToken', 'TranscludeToken', 'HeadingToken'],
72
+ ['HtmlToken'],
73
+ ['TableToken'],
74
+ ['HrToken', 'DoubleUnderscoreToken'],
75
+ ['LinkToken', 'FileToken', 'CategoryToken'],
76
+ ['QuoteToken'],
77
+ ['ExtLinkToken'],
78
+ ['MagicLinkToken'],
79
+ ],
80
+
81
+ config: './config/default',
82
+
83
+ getConfig() {
84
+ return require(this.config);
85
+ },
86
+
87
+ isInterwiki(title, {interwiki} = Parser.getConfig()) {
88
+ title = String(title);
89
+ return title.replaceAll('_', ' ').replace(/^\s*:?\s*/, '')
90
+ .match(new RegExp(`^(${interwiki.join('|')})\\s*:`, 'i'));
91
+ },
92
+
93
+ normalizeTitle(title, defaultNs = 0, include = false, config = Parser.getConfig(), halfParsed = false) {
94
+ title = String(title);
95
+ let /** @type {Token} */ token;
96
+ if (!halfParsed) {
97
+ const Token = require('./src');
98
+ token = this.run(() => new Token(title, config).parseOnce(0, include).parseOnce());
99
+ title = token.firstChild;
100
+ }
101
+ const Title = require('./lib/title'),
102
+ titleObj = new Title(title, defaultNs, config);
103
+ if (token) {
104
+ const build = /** @param {string[]} keys */ keys => {
105
+ for (const key of keys) {
106
+ if (titleObj[key].includes('\x00')) {
107
+ titleObj[key] = text(token.buildFromStr(titleObj[key]));
108
+ }
109
+ }
110
+ };
111
+ Parser.run(() => {
112
+ build(['title', 'fragment']);
113
+ });
114
+ }
115
+ return titleObj;
116
+ },
117
+
118
+ MAX_STAGE: 11,
119
+
120
+ parse(wikitext, include = false, maxStage = Parser.MAX_STAGE, config = Parser.getConfig()) {
121
+ const Token = require('./src');
122
+ this.run(() => {
123
+ if (typeof wikitext === 'string') {
124
+ wikitext = new Token(wikitext, config);
125
+ } else if (!(wikitext instanceof Token)) {
126
+ throw new TypeError('待解析的内容应为 String 或 Token!');
127
+ }
128
+ try {
129
+ wikitext.parse(maxStage, include);
130
+ } catch (e) {
131
+ if (e instanceof Error) {
132
+ fs.writeFileSync(
133
+ `${__dirname}/errors/${new Date().toISOString()}`,
134
+ `${e.stack}\n\n\n${wikitext.toString()}`,
135
+ );
136
+ }
137
+ throw e;
138
+ }
139
+ });
140
+ return wikitext;
141
+ },
142
+
143
+ getTool() {
144
+ delete require.cache[require.resolve('./tool')];
145
+ return require('./tool');
146
+ },
147
+ };
148
+
149
+ const /** @type {PropertyDescriptorMap} */ def = {};
150
+ for (const key in Parser) {
151
+ if (['alises', 'MAX_STAGE'].includes(key)) {
152
+ def[key] = {enumerable: false, writable: false};
153
+ } else if (!['config', 'isInterwiki', 'normalizeTitle', 'parse', 'getTool'].includes(key)) {
154
+ def[key] = {enumerable: false};
155
+ }
156
+ }
157
+ Object.defineProperties(Parser, def);
158
+
159
+ module.exports = Parser;
package/jsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "esnext",
4
+ "module": "commonJS",
5
+ "moduleResolution": "node"
6
+ }
7
+ }