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
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Token from '../src';
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface AstEvent extends Event {
|
|
5
|
+
readonly target: Token;
|
|
6
|
+
currentTarget: Token;
|
|
7
|
+
prevTarget: ?Token;
|
|
8
|
+
};
|
|
9
|
+
interface AstEventData {
|
|
10
|
+
position: number;
|
|
11
|
+
removed: string|Token;
|
|
12
|
+
inserted: string|Token;
|
|
13
|
+
oldToken: Token;
|
|
14
|
+
newToken: Token;
|
|
15
|
+
oldText: string;
|
|
16
|
+
newText: string;
|
|
17
|
+
oldKey: string;
|
|
18
|
+
newKey: string;
|
|
19
|
+
}
|
|
20
|
+
type AstListener = (e: AstEvent, data: AstEventData) => any;
|
|
21
|
+
|
|
22
|
+
type pseudo = 'root'|'is'|'not'|'nth-child'|'nth-of-type'|'nth-last-child'|'nth-last-of-type'
|
|
23
|
+
|'first-child'|'first-of-type'|'last-child'|'last-of-type'|'only-child'|'only-of-type'|'empty'
|
|
24
|
+
|'contains'|'has'|'header'|'parent'|'hidden'|'visible';
|
|
25
|
+
type pseudoCall = Record<pseudo, string[]>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import Token from '../src';
|
|
2
|
+
import $ from '../tool';
|
|
3
|
+
import Title from '../lib/title';
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
interface Parser {
|
|
7
|
+
warning: boolean;
|
|
8
|
+
debugging: boolean;
|
|
9
|
+
|
|
10
|
+
/** 默认输出到console.warn */
|
|
11
|
+
warn(msg: string, ...args: any[]): void;
|
|
12
|
+
/** 默认不输出到console.debug */
|
|
13
|
+
debug(msg: string, ...args: any[]): void;
|
|
14
|
+
/** 总是输出到console.error */
|
|
15
|
+
error(msg: string, ...args: any[]): void;
|
|
16
|
+
/** 总是输出到console.info */
|
|
17
|
+
info(msg: string, ...args: any[]): void;
|
|
18
|
+
|
|
19
|
+
running: boolean;
|
|
20
|
+
|
|
21
|
+
run<T>(callback: () => T): T;
|
|
22
|
+
|
|
23
|
+
/** 只储存导出各个Class的文件路径 */
|
|
24
|
+
classes: Record<string, string>;
|
|
25
|
+
mixins: Record<string, string>;
|
|
26
|
+
parsers: Record<string, string>;
|
|
27
|
+
/** 清除各模块的缓存 */
|
|
28
|
+
clearCache(): void;
|
|
29
|
+
|
|
30
|
+
log(f: Function): void;
|
|
31
|
+
|
|
32
|
+
readonly aliases: string[][];
|
|
33
|
+
|
|
34
|
+
config: string;
|
|
35
|
+
getConfig(): ParserConfig;
|
|
36
|
+
|
|
37
|
+
isInterwiki(title: string, config?: ParserConfig): RegExpMatchArray;
|
|
38
|
+
normalizeTitle(
|
|
39
|
+
title: string, defaultNs?: number, include?: boolean, config?: ParserConfig, halfParsed?: boolean
|
|
40
|
+
): Title;
|
|
41
|
+
|
|
42
|
+
readonly MAX_STAGE: number;
|
|
43
|
+
parse(wikitext: string|Token, include?: boolean, maxStage?: number, config?: ParserConfig): Token;
|
|
44
|
+
|
|
45
|
+
getTool(): typeof $;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Ranges from '../lib/ranges';
|
|
2
|
+
import Token from '../src';
|
|
3
|
+
import ParameterToken from '../src/parameter';
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
type TokenAttribute<T> =
|
|
7
|
+
T extends 'childNodes' ? (string|Token)[] :
|
|
8
|
+
T extends 'parentNode' ? Token|undefined :
|
|
9
|
+
T extends 'optional'|'tags' ? string[] :
|
|
10
|
+
T extends 'stage' ? number :
|
|
11
|
+
T extends 'config' ? ParserConfig :
|
|
12
|
+
T extends 'accum' ? accum :
|
|
13
|
+
T extends 'acceptable' ? Record<string, Ranges> :
|
|
14
|
+
T extends 'protectedChildren' ? Ranges :
|
|
15
|
+
T extends 'keys' ? Set<string> :
|
|
16
|
+
T extends 'args' ? Record<string, Set<ParameterToken>> :
|
|
17
|
+
T extends 'attr' ? Map<string, string|true> :
|
|
18
|
+
T extends 'include'|'selfLink' ? boolean :
|
|
19
|
+
T extends 'pattern' ? RegExp :
|
|
20
|
+
string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Token from '../src';
|
|
2
|
+
import Ranges from '../lib/ranges';
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
interface ParserConfig {
|
|
6
|
+
ext: string[];
|
|
7
|
+
html: [string[], string[], string[]];
|
|
8
|
+
namespaces: Record<string, string>;
|
|
9
|
+
nsid: Record<string, number>;
|
|
10
|
+
parserFunction: [string[], string[], string[], string[]];
|
|
11
|
+
doubleUnderscore: [string[], string[]];
|
|
12
|
+
protocol: string;
|
|
13
|
+
interwiki: string[];
|
|
14
|
+
img: Record<string, string>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type accum = Token[];
|
|
18
|
+
type acceptable = Record<string, number|string|Ranges|(number|string)[]>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export {};
|
package/util/debug.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {ObjectConstructor} constructor
|
|
5
|
+
* @param {string} method
|
|
6
|
+
* @param {...string} args
|
|
7
|
+
*/
|
|
8
|
+
const typeError = ({constructor}, method, ...args) => {
|
|
9
|
+
throw new TypeError(`${constructor.name}.${method} 方法仅接受 ${args.join('、')} 作为输入参数!`);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 不是被构造器或原型方法调用
|
|
14
|
+
* @param {string} name
|
|
15
|
+
*/
|
|
16
|
+
const externalUse = (name, proxy = false) => {
|
|
17
|
+
if (!proxy && require('..').running) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const regex = new RegExp(`^${
|
|
21
|
+
proxy ? 'Proxy' : 'new \\w*Token$|^(?:AstNode|AstElement|\\w*Token)'
|
|
22
|
+
}\\.(?!${name}$)`);
|
|
23
|
+
try {
|
|
24
|
+
throw new Error();
|
|
25
|
+
} catch (e) {
|
|
26
|
+
if (e instanceof Error) {
|
|
27
|
+
const mt = e.stack.match(/(?<=^\s+at )(?:new )?[\w.]+(?= \(\/)/gm);
|
|
28
|
+
return !mt.slice(2).some(func => regex.test(func));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {AstEvent} e
|
|
35
|
+
* @param {AstEventData} data
|
|
36
|
+
*/
|
|
37
|
+
const undo = (e, data) => {
|
|
38
|
+
const {target, type} = e;
|
|
39
|
+
switch (type) {
|
|
40
|
+
case 'remove': {
|
|
41
|
+
const childNodes = [...target.childNodes];
|
|
42
|
+
childNodes.splice(data.position, 0, data.removed);
|
|
43
|
+
target.setAttribute('childNodes', childNodes);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 'insert': {
|
|
47
|
+
const childNodes = [...target.childNodes];
|
|
48
|
+
childNodes.splice(data.position, 1);
|
|
49
|
+
target.setAttribute('childNodes', childNodes);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case 'replace': {
|
|
53
|
+
const {parentNode} = target,
|
|
54
|
+
childNodes = [...parentNode.childNodes];
|
|
55
|
+
childNodes.splice(data.position, 1, data.oldToken);
|
|
56
|
+
parentNode.setAttribute('childNodes', childNodes);
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
case 'text': {
|
|
60
|
+
const childNodes = [...target.childNodes];
|
|
61
|
+
childNodes[data.position] = data.oldText;
|
|
62
|
+
target.setAttribute('childNodes', childNodes);
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
default:
|
|
66
|
+
throw new RangeError(`无法撤销未知类型的事件:${type}`);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
module.exports = {typeError, externalUse, undo};
|
package/util/string.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* optionally convert to lower cases
|
|
5
|
+
* @param {string} val
|
|
6
|
+
* @param {boolean} i
|
|
7
|
+
*/
|
|
8
|
+
const toCase = (val, i) => i ? val.toLowerCase() : val;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* remove half-parsed comment-like tokens
|
|
12
|
+
* @param {string} str
|
|
13
|
+
*/
|
|
14
|
+
const removeComment = str => str.replace(/\x00\d+c\x7f/g, '');
|
|
15
|
+
|
|
16
|
+
/** @param {string} str */
|
|
17
|
+
const ucfirst = str => str && `${str[0].toUpperCase()}${str.slice(1)}`;
|
|
18
|
+
|
|
19
|
+
/** @param {string} str */
|
|
20
|
+
const escapeRegExp = str => str.replace(/[\\{}()|.?*+\-^$[\]]/g, '\\$&');
|
|
21
|
+
|
|
22
|
+
/** @param {(string|AstNode)[]} childNodes */
|
|
23
|
+
const text = (childNodes, separator = '') => {
|
|
24
|
+
const AstNode = require('../lib/node'); // eslint-disable-line no-unused-vars
|
|
25
|
+
return childNodes.map(child => typeof child === 'string' ? child : child.text()).join(separator);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} start
|
|
30
|
+
* @param {string} end
|
|
31
|
+
* @param {string} separator
|
|
32
|
+
* @param {string} str
|
|
33
|
+
*/
|
|
34
|
+
const explode = (start, end, separator, str) => {
|
|
35
|
+
if (str === undefined) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
const regex = new RegExp(`${[start, end, separator].map(escapeRegExp).join('|')}`, 'g'),
|
|
39
|
+
/** @type {string[]} */ exploded = [];
|
|
40
|
+
let mt = regex.exec(str),
|
|
41
|
+
depth = 0,
|
|
42
|
+
lastIndex = 0;
|
|
43
|
+
while (mt) {
|
|
44
|
+
const {0: match, index} = mt;
|
|
45
|
+
if (match !== separator) {
|
|
46
|
+
depth += match === start ? 1 : -1;
|
|
47
|
+
} else if (depth === 0) {
|
|
48
|
+
exploded.push(str.slice(lastIndex, index));
|
|
49
|
+
({lastIndex} = regex);
|
|
50
|
+
}
|
|
51
|
+
mt = regex.exec(str);
|
|
52
|
+
}
|
|
53
|
+
exploded.push(str.slice(lastIndex));
|
|
54
|
+
return exploded;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const extUrlChar = '(?:[\\d.]+|\\[[\\da-f:.]+\\]|[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd])'
|
|
58
|
+
+ '[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd]*';
|
|
59
|
+
|
|
60
|
+
module.exports = {toCase, removeComment, ucfirst, escapeRegExp, text, explode, extUrlChar};
|