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.
- package/.eslintrc.json +229 -0
- package/LICENSE +674 -0
- package/README.md +1896 -0
- package/config/default.json +766 -0
- package/config/llwiki.json +686 -0
- package/config/moegirl.json +721 -0
- package/index.js +159 -0
- package/jsconfig.json +7 -0
- package/lib/element.js +690 -0
- package/lib/node.js +357 -0
- package/lib/ranges.js +122 -0
- package/lib/title.js +57 -0
- package/mixin/attributeParent.js +67 -0
- package/mixin/fixedToken.js +32 -0
- package/mixin/hidden.js +22 -0
- package/package.json +30 -0
- package/parser/brackets.js +107 -0
- package/parser/commentAndExt.js +61 -0
- package/parser/externalLinks.js +30 -0
- package/parser/hrAndDoubleUnderscore.js +26 -0
- package/parser/html.js +41 -0
- package/parser/links.js +92 -0
- package/parser/magicLinks.js +40 -0
- package/parser/quotes.js +63 -0
- package/parser/table.js +97 -0
- package/src/arg.js +150 -0
- package/src/atom/hidden.js +10 -0
- package/src/atom/index.js +33 -0
- package/src/attribute.js +342 -0
- package/src/extLink.js +116 -0
- package/src/heading.js +91 -0
- package/src/html.js +144 -0
- package/src/imageParameter.js +172 -0
- package/src/index.js +602 -0
- package/src/link/category.js +88 -0
- package/src/link/file.js +201 -0
- package/src/link/index.js +214 -0
- package/src/listToken.js +47 -0
- package/src/magicLink.js +66 -0
- package/src/nowiki/comment.js +45 -0
- package/src/nowiki/doubleUnderscore.js +42 -0
- package/src/nowiki/hr.js +41 -0
- package/src/nowiki/index.js +37 -0
- package/src/nowiki/noinclude.js +24 -0
- package/src/nowiki/quote.js +37 -0
- package/src/onlyinclude.js +42 -0
- package/src/parameter.js +165 -0
- package/src/syntax.js +80 -0
- package/src/table/index.js +867 -0
- package/src/table/td.js +259 -0
- package/src/table/tr.js +244 -0
- package/src/tagPair/ext.js +85 -0
- package/src/tagPair/include.js +45 -0
- package/src/tagPair/index.js +91 -0
- package/src/transclude.js +627 -0
- package/tool/index.js +898 -0
- package/typings/element.d.ts +28 -0
- package/typings/index.d.ts +49 -0
- package/typings/node.d.ts +23 -0
- package/typings/parser.d.ts +9 -0
- package/typings/table.d.ts +14 -0
- package/typings/token.d.ts +21 -0
- package/typings/tool.d.ts +10 -0
- package/util/debug.js +70 -0
- 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;
|