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