wikiparser-node 0.11.0 → 1.0.0-beta.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/dist/index.d.ts +59 -113
- package/dist/index.js +307 -0
- package/dist/lib/element.d.ts +122 -108
- package/dist/lib/element.js +656 -0
- package/dist/lib/node.d.ts +115 -221
- package/dist/lib/node.js +473 -0
- package/dist/lib/ranges.d.ts +27 -26
- package/dist/lib/ranges.js +130 -0
- package/dist/lib/text.d.ts +36 -28
- package/dist/lib/text.js +215 -0
- package/dist/lib/title.d.ts +25 -12
- package/dist/lib/title.js +100 -0
- package/dist/mixin/attributesParent.js +90 -0
- package/dist/mixin/fixed.js +29 -0
- package/dist/mixin/hidden.js +19 -0
- package/dist/mixin/singleLine.js +23 -0
- package/dist/mixin/sol.js +41 -0
- package/dist/parser/brackets.js +117 -0
- package/dist/parser/commentAndExt.js +48 -0
- package/dist/parser/converter.js +31 -0
- package/dist/parser/externalLinks.js +22 -0
- package/dist/parser/hrAndDoubleUnderscore.js +33 -0
- package/dist/parser/html.js +29 -0
- package/dist/parser/links.js +86 -0
- package/dist/parser/list.js +51 -0
- package/dist/parser/magicLinks.js +32 -0
- package/dist/parser/quotes.js +57 -0
- package/dist/parser/selector.js +158 -0
- package/dist/parser/table.js +108 -0
- package/dist/src/arg.d.ts +47 -23
- package/dist/src/arg.js +196 -0
- package/dist/src/atom.d.ts +12 -0
- package/dist/src/atom.js +22 -0
- package/dist/src/attribute.d.ts +74 -33
- package/dist/src/attribute.js +433 -0
- package/dist/src/attributes.d.ts +61 -55
- package/dist/src/attributes.js +371 -0
- package/dist/src/converter.d.ts +45 -71
- package/dist/src/converter.js +135 -0
- package/dist/src/converterFlags.d.ts +64 -39
- package/dist/src/converterFlags.js +235 -0
- package/dist/src/converterRule.d.ts +49 -27
- package/dist/src/converterRule.js +255 -0
- package/dist/src/extLink.d.ts +41 -38
- package/dist/src/extLink.js +154 -0
- package/dist/src/gallery.d.ts +59 -18
- package/dist/src/gallery.js +132 -0
- package/dist/src/heading.d.ts +60 -22
- package/dist/src/heading.js +135 -0
- package/dist/src/hidden.d.ts +20 -0
- package/dist/src/hidden.js +24 -0
- package/dist/src/html.d.ts +83 -29
- package/dist/src/html.js +242 -0
- package/dist/src/imageParameter.d.ts +59 -40
- package/dist/src/imageParameter.js +251 -0
- package/dist/src/imagemap.d.ts +65 -21
- package/dist/src/imagemap.js +164 -0
- package/dist/src/imagemapLink.d.ts +46 -14
- package/dist/src/imagemapLink.js +38 -0
- package/dist/src/index.d.ts +71 -105
- package/dist/src/index.js +826 -0
- package/dist/src/link/base.d.ts +71 -0
- package/dist/src/link/base.js +225 -0
- package/dist/src/link/category.d.ts +10 -11
- package/dist/src/link/category.js +26 -0
- package/dist/src/link/file.d.ts +61 -39
- package/dist/src/link/file.js +242 -0
- package/dist/src/link/galleryImage.d.ts +34 -12
- package/dist/src/link/galleryImage.js +98 -0
- package/dist/src/link/index.d.ts +25 -63
- package/dist/src/link/index.js +136 -0
- package/dist/src/magicLink.d.ts +22 -15
- package/dist/src/magicLink.js +126 -0
- package/dist/src/nested.d.ts +47 -0
- package/dist/src/nested.js +84 -0
- package/dist/src/nowiki/base.d.ts +39 -0
- package/dist/src/nowiki/base.js +29 -0
- package/dist/src/nowiki/comment.d.ts +31 -20
- package/dist/src/nowiki/comment.js +61 -0
- package/dist/src/nowiki/dd.d.ts +17 -11
- package/dist/src/nowiki/dd.js +50 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +28 -13
- package/dist/src/nowiki/doubleUnderscore.js +45 -0
- package/dist/src/nowiki/hr.d.ts +28 -10
- package/dist/src/nowiki/hr.js +33 -0
- package/dist/src/nowiki/index.d.ts +17 -23
- package/dist/src/nowiki/index.js +21 -0
- package/dist/src/nowiki/list.d.ts +15 -7
- package/dist/src/nowiki/list.js +11 -0
- package/dist/src/nowiki/noinclude.d.ts +20 -7
- package/dist/src/nowiki/noinclude.js +22 -0
- package/dist/src/nowiki/quote.d.ts +25 -10
- package/dist/src/nowiki/quote.js +55 -0
- package/dist/src/onlyinclude.d.ts +28 -12
- package/dist/src/onlyinclude.js +64 -0
- package/dist/src/paramTag/index.d.ts +40 -17
- package/dist/src/paramTag/index.js +76 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -7
- package/dist/src/paramTag/inputbox.js +19 -0
- package/dist/src/parameter.d.ts +62 -41
- package/dist/src/parameter.js +201 -0
- package/dist/src/pre.d.ts +32 -0
- package/dist/src/pre.js +39 -0
- package/dist/src/syntax.d.ts +17 -14
- package/dist/src/syntax.js +65 -0
- package/dist/src/table/base.d.ts +55 -0
- package/dist/src/table/base.js +77 -0
- package/dist/src/table/index.d.ts +123 -156
- package/dist/src/table/index.js +811 -0
- package/dist/src/table/td.d.ts +90 -67
- package/dist/src/table/td.js +276 -0
- package/dist/src/table/tr.d.ts +30 -85
- package/dist/src/table/tr.js +48 -0
- package/dist/src/table/trBase.d.ts +72 -0
- package/dist/src/table/trBase.js +153 -0
- package/dist/src/tagPair/ext.d.ts +47 -11
- package/dist/src/tagPair/ext.js +127 -0
- package/dist/src/tagPair/include.d.ts +32 -13
- package/dist/src/tagPair/include.js +40 -0
- package/dist/src/tagPair/index.d.ts +44 -29
- package/dist/src/tagPair/index.js +111 -0
- package/dist/src/transclude.d.ts +88 -85
- package/dist/src/transclude.js +739 -0
- package/dist/util/base.js +26 -0
- package/dist/util/debug.js +52 -0
- package/dist/util/diff.js +69 -0
- package/dist/util/lint.js +38 -0
- package/dist/util/string.js +103 -0
- package/errors/README +1 -0
- package/i18n/zh-hans.json +1 -0
- package/i18n/zh-hant.json +1 -0
- package/package.json +11 -14
- package/printed/README +1 -0
- package/dist/mixin/attributeParent.d.ts +0 -9
- package/dist/mixin/fixedToken.d.ts +0 -8
- package/dist/mixin/hidden.d.ts +0 -8
- package/dist/mixin/singleLine.d.ts +0 -8
- package/dist/mixin/sol.d.ts +0 -8
- package/dist/parser/brackets.d.ts +0 -12
- package/dist/parser/commentAndExt.d.ts +0 -8
- package/dist/parser/converter.d.ts +0 -7
- package/dist/parser/externalLinks.d.ts +0 -7
- package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
- package/dist/parser/html.d.ts +0 -7
- package/dist/parser/links.d.ts +0 -7
- package/dist/parser/list.d.ts +0 -7
- package/dist/parser/magicLinks.d.ts +0 -7
- package/dist/parser/quotes.d.ts +0 -7
- package/dist/parser/selector.d.ts +0 -12
- package/dist/parser/table.d.ts +0 -11
- package/dist/src/atom/hidden.d.ts +0 -5
- package/dist/src/atom/index.d.ts +0 -15
- package/dist/src/charinsert.d.ts +0 -32
- package/dist/src/hasNowiki/index.d.ts +0 -14
- package/dist/src/hasNowiki/pre.d.ts +0 -13
- package/dist/src/nested/choose.d.ts +0 -13
- package/dist/src/nested/combobox.d.ts +0 -13
- package/dist/src/nested/index.d.ts +0 -18
- package/dist/src/nested/references.d.ts +0 -13
- package/dist/tool/index.d.ts +0 -420
- package/dist/util/base.d.ts +0 -10
- package/dist/util/debug.d.ts +0 -20
- package/dist/util/diff.d.ts +0 -8
- package/dist/util/lint.d.ts +0 -28
- package/dist/util/string.d.ts +0 -55
- package/index.js +0 -333
- package/lib/element.js +0 -618
- package/lib/node.js +0 -730
- package/lib/ranges.js +0 -130
- package/lib/text.js +0 -265
- package/lib/title.js +0 -83
- package/mixin/attributeParent.js +0 -117
- package/mixin/fixedToken.js +0 -40
- package/mixin/hidden.js +0 -21
- package/mixin/singleLine.js +0 -31
- package/mixin/sol.js +0 -54
- package/parser/brackets.js +0 -128
- package/parser/commentAndExt.js +0 -62
- package/parser/converter.js +0 -46
- package/parser/externalLinks.js +0 -33
- package/parser/hrAndDoubleUnderscore.js +0 -49
- package/parser/html.js +0 -42
- package/parser/links.js +0 -94
- package/parser/list.js +0 -59
- package/parser/magicLinks.js +0 -41
- package/parser/quotes.js +0 -64
- package/parser/selector.js +0 -180
- package/parser/table.js +0 -114
- package/src/arg.js +0 -207
- package/src/atom/hidden.js +0 -13
- package/src/atom/index.js +0 -43
- package/src/attribute.js +0 -472
- package/src/attributes.js +0 -453
- package/src/charinsert.js +0 -97
- package/src/converter.js +0 -176
- package/src/converterFlags.js +0 -284
- package/src/converterRule.js +0 -256
- package/src/extLink.js +0 -180
- package/src/gallery.js +0 -149
- package/src/hasNowiki/index.js +0 -44
- package/src/hasNowiki/pre.js +0 -40
- package/src/heading.js +0 -134
- package/src/html.js +0 -254
- package/src/imageParameter.js +0 -303
- package/src/imagemap.js +0 -199
- package/src/imagemapLink.js +0 -41
- package/src/index.js +0 -938
- package/src/link/category.js +0 -44
- package/src/link/file.js +0 -287
- package/src/link/galleryImage.js +0 -120
- package/src/link/index.js +0 -388
- package/src/magicLink.js +0 -151
- package/src/nested/choose.js +0 -24
- package/src/nested/combobox.js +0 -23
- package/src/nested/index.js +0 -96
- package/src/nested/references.js +0 -23
- package/src/nowiki/comment.js +0 -71
- package/src/nowiki/dd.js +0 -59
- package/src/nowiki/doubleUnderscore.js +0 -56
- package/src/nowiki/hr.js +0 -41
- package/src/nowiki/index.js +0 -56
- package/src/nowiki/list.js +0 -16
- package/src/nowiki/noinclude.js +0 -28
- package/src/nowiki/quote.js +0 -69
- package/src/onlyinclude.js +0 -64
- package/src/paramTag/index.js +0 -89
- package/src/paramTag/inputbox.js +0 -35
- package/src/parameter.js +0 -239
- package/src/syntax.js +0 -91
- package/src/table/index.js +0 -985
- package/src/table/td.js +0 -343
- package/src/table/tr.js +0 -319
- package/src/tagPair/ext.js +0 -146
- package/src/tagPair/include.js +0 -50
- package/src/tagPair/index.js +0 -131
- package/src/transclude.js +0 -843
- package/tool/index.js +0 -1209
- package/typings/api.d.ts +0 -9
- package/typings/array.d.ts +0 -29
- package/typings/event.d.ts +0 -22
- package/typings/index.d.ts +0 -118
- package/typings/node.d.ts +0 -35
- package/typings/parser.d.ts +0 -12
- package/typings/table.d.ts +0 -10
- package/typings/token.d.ts +0 -31
- package/typings/tool.d.ts +0 -6
- package/util/base.js +0 -17
- package/util/debug.js +0 -73
- package/util/diff.js +0 -76
- package/util/lint.js +0 -57
- package/util/string.js +0 -126
package/dist/index.d.ts
CHANGED
|
@@ -1,114 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
declare const Parser:
|
|
57
|
-
|
|
1
|
+
declare interface Config {
|
|
2
|
+
ext: string[];
|
|
3
|
+
html: [string[], string[], string[]];
|
|
4
|
+
namespaces: Record<string, string>;
|
|
5
|
+
nsid: Record<string, number>;
|
|
6
|
+
parserFunction: [Record<string, string>, string[], string[], string[]];
|
|
7
|
+
doubleUnderscore: [string[], string[]];
|
|
8
|
+
protocol: string;
|
|
9
|
+
img: Record<string, string>;
|
|
10
|
+
variants: string[];
|
|
11
|
+
interwiki: string[];
|
|
12
|
+
excludes?: string[];
|
|
13
|
+
}
|
|
14
|
+
declare interface LintError {
|
|
15
|
+
message: string;
|
|
16
|
+
severity: 'error' | 'warning';
|
|
17
|
+
startIndex: number;
|
|
18
|
+
endIndex: number;
|
|
19
|
+
startLine: number;
|
|
20
|
+
startCol: number;
|
|
21
|
+
endLine: number;
|
|
22
|
+
endCol: number;
|
|
23
|
+
excerpt: string;
|
|
24
|
+
}
|
|
25
|
+
declare interface Parser {
|
|
26
|
+
/** @browser */
|
|
27
|
+
config: string | Config;
|
|
28
|
+
/** @browser */
|
|
29
|
+
i18n: string | Record<string, string> | undefined;
|
|
30
|
+
conversionTable: Map<string, string>;
|
|
31
|
+
redirects: Map<string, string>;
|
|
32
|
+
/**
|
|
33
|
+
* 规范化页面标题
|
|
34
|
+
* @browser
|
|
35
|
+
* @param title 标题(含或不含命名空间前缀)
|
|
36
|
+
* @param defaultNs 命名空间
|
|
37
|
+
* @param include 是否嵌入
|
|
38
|
+
* @param halfParsed 是否是半解析状态
|
|
39
|
+
* @param decode 是否需要解码
|
|
40
|
+
* @param selfLink 是否允许selfLink
|
|
41
|
+
*/
|
|
42
|
+
normalizeTitle(title: string, defaultNs?: number, include?: boolean, config?: Config, halfParsed?: boolean, decode?: boolean, selfLink?: boolean): import('./lib/title');
|
|
43
|
+
/**
|
|
44
|
+
* 解析wikitext
|
|
45
|
+
* @browser
|
|
46
|
+
* @param include 是否嵌入
|
|
47
|
+
* @param maxStage 最大解析层级
|
|
48
|
+
*/
|
|
49
|
+
parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): import('./src');
|
|
50
|
+
/**
|
|
51
|
+
* 是否是跨维基链接
|
|
52
|
+
* @param title 链接标题
|
|
53
|
+
*/
|
|
54
|
+
isInterwiki(title: string, config?: Config): [string, string] | null;
|
|
55
|
+
}
|
|
56
|
+
declare const Parser: Parser;
|
|
58
57
|
declare namespace Parser {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
Ranges,
|
|
63
|
-
AstText,
|
|
64
|
-
Title,
|
|
65
|
-
ArgToken,
|
|
66
|
-
HiddenToken,
|
|
67
|
-
AtomToken,
|
|
68
|
-
AttributeToken,
|
|
69
|
-
AttributesToken,
|
|
70
|
-
CharinsertToken,
|
|
71
|
-
ConverterToken,
|
|
72
|
-
ConverterFlagsToken,
|
|
73
|
-
ConverterRuleToken,
|
|
74
|
-
ExtLinkToken,
|
|
75
|
-
GalleryToken,
|
|
76
|
-
HasNowikiToken,
|
|
77
|
-
PreToken,
|
|
78
|
-
HeadingToken,
|
|
79
|
-
HtmlToken,
|
|
80
|
-
ImageParameterToken,
|
|
81
|
-
ImagemapToken,
|
|
82
|
-
ImagemapLinkToken,
|
|
83
|
-
Token,
|
|
84
|
-
CategoryToken,
|
|
85
|
-
FileToken,
|
|
86
|
-
GalleryImageToken,
|
|
87
|
-
LinkToken,
|
|
88
|
-
MagicLinkToken,
|
|
89
|
-
ChooseToken,
|
|
90
|
-
ComboboxToken,
|
|
91
|
-
NestedToken,
|
|
92
|
-
ReferencesToken,
|
|
93
|
-
CommentToken,
|
|
94
|
-
DdToken,
|
|
95
|
-
DoubleUnderscoreToken,
|
|
96
|
-
HrToken,
|
|
97
|
-
NowikiToken,
|
|
98
|
-
ListToken,
|
|
99
|
-
NoincludeToken,
|
|
100
|
-
QuoteToken,
|
|
101
|
-
OnlyincludeToken,
|
|
102
|
-
ParamTagToken,
|
|
103
|
-
InputboxToken,
|
|
104
|
-
ParameterToken,
|
|
105
|
-
SyntaxToken,
|
|
106
|
-
TableToken,
|
|
107
|
-
TdToken,
|
|
108
|
-
TrToken,
|
|
109
|
-
ExtToken,
|
|
110
|
-
IncludeToken,
|
|
111
|
-
TagPairToken,
|
|
112
|
-
TranscludeToken,
|
|
113
|
-
}
|
|
114
|
-
}
|
|
58
|
+
export type { Config, LintError, };
|
|
59
|
+
}
|
|
60
|
+
export = Parser;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
/**
|
|
5
|
+
* 从根路径require
|
|
6
|
+
* @param file 文件名
|
|
7
|
+
* @param dir 子路径
|
|
8
|
+
*/
|
|
9
|
+
const rootRequire = (file, dir = '') => require(`${file.includes('/') ? '' : `../${dir}`}${file}`);
|
|
10
|
+
const Parser = {
|
|
11
|
+
config: 'default',
|
|
12
|
+
i18n: undefined,
|
|
13
|
+
conversionTable: new Map(),
|
|
14
|
+
redirects: new Map(),
|
|
15
|
+
MAX_STAGE: 11,
|
|
16
|
+
warning: true,
|
|
17
|
+
debugging: false,
|
|
18
|
+
running: false,
|
|
19
|
+
classes: {},
|
|
20
|
+
mixins: {},
|
|
21
|
+
parsers: {},
|
|
22
|
+
aliases: [
|
|
23
|
+
['AstText'],
|
|
24
|
+
['CommentToken', 'ExtToken', 'IncludeToken', 'NoincludeToken'],
|
|
25
|
+
['ArgToken', 'TranscludeToken', 'HeadingToken'],
|
|
26
|
+
['HtmlToken'],
|
|
27
|
+
['TableToken'],
|
|
28
|
+
['HrToken', 'DoubleUnderscoreToken'],
|
|
29
|
+
['LinkToken', 'FileToken', 'CategoryToken'],
|
|
30
|
+
['QuoteToken'],
|
|
31
|
+
['ExtLinkToken'],
|
|
32
|
+
['MagicLinkToken'],
|
|
33
|
+
['ListToken', 'DdToken'],
|
|
34
|
+
['ConverterToken'],
|
|
35
|
+
],
|
|
36
|
+
typeAliases: {
|
|
37
|
+
text: ['string', 'str'],
|
|
38
|
+
plain: ['regular', 'normal'],
|
|
39
|
+
// comment and extension
|
|
40
|
+
onlyinclude: ['only-include'],
|
|
41
|
+
noinclude: ['no-include'],
|
|
42
|
+
include: ['includeonly', 'include-only'],
|
|
43
|
+
comment: undefined,
|
|
44
|
+
ext: ['extension'],
|
|
45
|
+
'ext-attrs': ['extension-attrs', 'ext-attributes', 'extension-attributes'],
|
|
46
|
+
'ext-attr-dirty': ['extension-attr-dirty', 'ext-attribute-dirty', 'extension-attribute-dirty'],
|
|
47
|
+
'ext-attr': ['extension-attr', 'ext-attribute', 'extension-attribute'],
|
|
48
|
+
'attr-key': ['attribute-key'],
|
|
49
|
+
'attr-value': ['attribute-value', 'attr-val', 'attribute-val'],
|
|
50
|
+
'ext-inner': ['extension-inner'],
|
|
51
|
+
// triple brackets
|
|
52
|
+
arg: ['argument'],
|
|
53
|
+
'arg-name': ['argument-name'],
|
|
54
|
+
'arg-default': ['argument-default'],
|
|
55
|
+
hidden: ['arg-redundant'],
|
|
56
|
+
// double brackets
|
|
57
|
+
'magic-word': ['parser-function', 'parser-func'],
|
|
58
|
+
'magic-word-name': ['parser-function-name', 'parser-func-name'],
|
|
59
|
+
'invoke-function': ['invoke-func', 'lua-function', 'lua-func', 'module-function', 'module-func'],
|
|
60
|
+
'invoke-module': ['lua-module'],
|
|
61
|
+
template: undefined,
|
|
62
|
+
'template-name': undefined,
|
|
63
|
+
parameter: ['param'],
|
|
64
|
+
'parameter-key': ['param-key'],
|
|
65
|
+
'parameter-value': ['parameter-val', 'param-value', 'param-val'],
|
|
66
|
+
// heading
|
|
67
|
+
heading: ['header'],
|
|
68
|
+
'heading-title': ['header-title'],
|
|
69
|
+
'heading-trail': ['header-trail'],
|
|
70
|
+
// html
|
|
71
|
+
html: undefined,
|
|
72
|
+
'html-attrs': ['html-attributes'],
|
|
73
|
+
'html-attr-dirty': ['html-attribute-dirty'],
|
|
74
|
+
'html-attr': ['html-attribute'],
|
|
75
|
+
// table
|
|
76
|
+
table: undefined,
|
|
77
|
+
tr: ['table-row'],
|
|
78
|
+
td: ['table-cell', 'table-data'],
|
|
79
|
+
'table-syntax': undefined,
|
|
80
|
+
'table-attrs': ['tr-attrs', 'td-attrs', 'table-attributes', 'tr-attributes', 'td-attributes'],
|
|
81
|
+
'table-attr-dirty': ['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
|
+
promises: [Promise.resolve()],
|
|
123
|
+
/** @implements */
|
|
124
|
+
getConfig() {
|
|
125
|
+
if (typeof this.config === 'string') {
|
|
126
|
+
this.config = rootRequire(this.config, 'config/');
|
|
127
|
+
return this.getConfig();
|
|
128
|
+
}
|
|
129
|
+
return { ...this.config, excludes: [] };
|
|
130
|
+
},
|
|
131
|
+
/** @implements */
|
|
132
|
+
msg(msg, arg = '') {
|
|
133
|
+
if (typeof this.i18n === 'string') {
|
|
134
|
+
this.i18n = rootRequire(this.i18n, 'i18n/');
|
|
135
|
+
return this.msg(msg, arg);
|
|
136
|
+
}
|
|
137
|
+
return (this.i18n?.[msg] ?? msg).replace('$1', arg);
|
|
138
|
+
},
|
|
139
|
+
/** @implements */
|
|
140
|
+
normalizeTitle(title, defaultNs = 0, include = false, config = Parser.getConfig(), halfParsed = false, decode = false, selfLink = false) {
|
|
141
|
+
const Title = require('./lib/title');
|
|
142
|
+
if (halfParsed) {
|
|
143
|
+
return new Title(title, defaultNs, config, decode, selfLink);
|
|
144
|
+
}
|
|
145
|
+
const Token = require('./src');
|
|
146
|
+
const token = this.run(() => new Token(title, config).parseOnce(0, include).parseOnce()), titleObj = new Title(String(token.firstChild), defaultNs, config, decode, selfLink);
|
|
147
|
+
/**
|
|
148
|
+
* 重建部分属性值
|
|
149
|
+
* @param keys 属性键
|
|
150
|
+
*/
|
|
151
|
+
const build = (keys) => {
|
|
152
|
+
for (const key of keys) {
|
|
153
|
+
if (titleObj[key]?.includes('\0')) {
|
|
154
|
+
titleObj[key] = token.buildFromStr(titleObj[key], 'text');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
this.run(() => {
|
|
159
|
+
build(['main', 'fragment']);
|
|
160
|
+
});
|
|
161
|
+
titleObj.autoConvert(this.conversionTable);
|
|
162
|
+
titleObj.redirects = this.redirects;
|
|
163
|
+
return titleObj;
|
|
164
|
+
},
|
|
165
|
+
/** @implements */
|
|
166
|
+
parse(wikitext, include, maxStage = Parser.MAX_STAGE, config = Parser.getConfig()) {
|
|
167
|
+
if (typeof wikitext !== 'string') {
|
|
168
|
+
throw new TypeError('待解析的内容应为 String!');
|
|
169
|
+
}
|
|
170
|
+
const Token = require('./src');
|
|
171
|
+
let token;
|
|
172
|
+
this.run(() => {
|
|
173
|
+
token = new Token(wikitext, config);
|
|
174
|
+
try {
|
|
175
|
+
token.parse(maxStage, include);
|
|
176
|
+
}
|
|
177
|
+
catch (e) {
|
|
178
|
+
if (e instanceof Error) {
|
|
179
|
+
const file = path.join(__dirname, '..', 'errors', new Date().toISOString()), stage = token.getAttribute('stage');
|
|
180
|
+
fs.writeFileSync(file, stage === this.MAX_STAGE ? wikitext : String(token));
|
|
181
|
+
fs.writeFileSync(`${file}.err`, e.stack);
|
|
182
|
+
fs.writeFileSync(`${file}.json`, JSON.stringify({
|
|
183
|
+
stage, include: token.getAttribute('include'), config: this.config,
|
|
184
|
+
}, null, '\t'));
|
|
185
|
+
}
|
|
186
|
+
throw e;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (this.debugging) {
|
|
190
|
+
let restored = String(token), process = '解析';
|
|
191
|
+
if (restored === wikitext) {
|
|
192
|
+
const entities = { lt: '<', gt: '>', amp: '&' };
|
|
193
|
+
restored = token.print().replace(/<[^<]+?>|&([lg]t|amp);/gu, (_, s) => s ? entities[s] : '');
|
|
194
|
+
process = '渲染HTML';
|
|
195
|
+
}
|
|
196
|
+
if (restored !== wikitext) {
|
|
197
|
+
const diff = require('./util/diff');
|
|
198
|
+
const { promises: { 0: cur, length } } = this;
|
|
199
|
+
this.promises.unshift((async () => {
|
|
200
|
+
await cur;
|
|
201
|
+
this.error(`${process}过程中不可逆地修改了原始文本!`);
|
|
202
|
+
return diff(wikitext, restored, length);
|
|
203
|
+
})());
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return token;
|
|
207
|
+
},
|
|
208
|
+
/** @implements */
|
|
209
|
+
run(callback) {
|
|
210
|
+
const { running } = this;
|
|
211
|
+
this.running = true;
|
|
212
|
+
try {
|
|
213
|
+
const result = callback();
|
|
214
|
+
this.running = running;
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
catch (e) {
|
|
218
|
+
this.running = running;
|
|
219
|
+
throw e;
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
/** @implements */
|
|
223
|
+
warn(msg, ...args) {
|
|
224
|
+
if (this.warning) {
|
|
225
|
+
console.warn('\x1B[33m%s\x1B[0m', msg, ...args);
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
/** @implements */
|
|
229
|
+
debug(msg, ...args) {
|
|
230
|
+
if (this.debugging) {
|
|
231
|
+
console.debug('\x1B[34m%s\x1B[0m', msg, ...args);
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
/** @implements */
|
|
235
|
+
error(msg, ...args) {
|
|
236
|
+
console.error('\x1B[31m%s\x1B[0m', msg, ...args);
|
|
237
|
+
},
|
|
238
|
+
/** @implements */
|
|
239
|
+
info(msg, ...args) {
|
|
240
|
+
console.info('\x1B[32m%s\x1B[0m', msg, ...args);
|
|
241
|
+
},
|
|
242
|
+
/** @implements */
|
|
243
|
+
log(f) {
|
|
244
|
+
if (typeof f === 'function') {
|
|
245
|
+
console.log(String(f));
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
/** @implements */
|
|
249
|
+
clearCache() {
|
|
250
|
+
const entries = [
|
|
251
|
+
...Object.entries(this.classes),
|
|
252
|
+
...Object.entries(this.mixins),
|
|
253
|
+
...Object.entries(this.parsers),
|
|
254
|
+
];
|
|
255
|
+
for (const [, filePath] of entries) {
|
|
256
|
+
try {
|
|
257
|
+
delete require.cache[require.resolve(filePath)];
|
|
258
|
+
}
|
|
259
|
+
catch { }
|
|
260
|
+
}
|
|
261
|
+
for (const [name, filePath] of entries) {
|
|
262
|
+
if (name in global) {
|
|
263
|
+
global[name] = require(filePath);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
/** @implements */
|
|
268
|
+
isInterwiki(title, { interwiki } = Parser.getConfig()) {
|
|
269
|
+
return new RegExp(`^(${interwiki.join('|')})\\s*:`, 'iu')
|
|
270
|
+
.exec(title.replaceAll('_', ' ').replace(/^\s*:?\s*/u, ''));
|
|
271
|
+
},
|
|
272
|
+
/** @implements */
|
|
273
|
+
reparse(date) {
|
|
274
|
+
const main = fs.readdirSync(path.join(__dirname, '..', 'errors'))
|
|
275
|
+
.find(name => name.startsWith(date) && name.endsWith('Z'));
|
|
276
|
+
if (!main) {
|
|
277
|
+
throw new RangeError(`找不到对应时间戳的错误记录:${date}`);
|
|
278
|
+
}
|
|
279
|
+
const file = path.join(__dirname, '..', 'errors', main), wikitext = fs.readFileSync(file, 'utf8');
|
|
280
|
+
const { stage, include, config } = require(`${file}.json`), Token = require('./src');
|
|
281
|
+
this.config = config;
|
|
282
|
+
return this.run(() => {
|
|
283
|
+
const halfParsed = stage < this.MAX_STAGE, token = new Token(wikitext, this.getConfig(), halfParsed);
|
|
284
|
+
if (halfParsed) {
|
|
285
|
+
token.setAttribute('stage', stage).parseOnce(stage, include);
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
token.parse(undefined, include);
|
|
289
|
+
}
|
|
290
|
+
fs.unlinkSync(file);
|
|
291
|
+
fs.unlinkSync(`${file}.err`);
|
|
292
|
+
fs.unlinkSync(`${file}.json`);
|
|
293
|
+
return token;
|
|
294
|
+
});
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
const def = {}, immutable = new Set(['MAX_STAGE', 'aliases', 'typeAliases', 'promises']), enumerable = new Set(['config', 'conversionTable', 'redirects', 'normalizeTitle', 'parse', 'isInterwiki']);
|
|
298
|
+
for (const key in Parser) {
|
|
299
|
+
if (immutable.has(key)) {
|
|
300
|
+
def[key] = { enumerable: false, writable: false };
|
|
301
|
+
}
|
|
302
|
+
else if (!enumerable.has(key)) {
|
|
303
|
+
def[key] = { enumerable: false };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
Object.defineProperties(Parser, def);
|
|
307
|
+
module.exports = Parser;
|