Skip to content

Commit

Permalink
Merge branch 'noprint' into lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsd-harry committed Jun 10, 2024
2 parents 7906268 + 4e15652 commit 3aed261
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 51 deletions.
16 changes: 7 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,19 @@ const Parser: Parser = {
return new Title(title, defaultNs, config, decode, selfLink);
}
const {Token}: typeof import('./src/index') = require('./src/index');
const token = Shadow.run(() => {
const root = new Token(title, config);
root.type = 'root';
return root.parseOnce(0, include).parseOnce();
}),
titleObj = new Title(token.toString(), defaultNs, config, decode, selfLink);
Shadow.run(() => {
return Shadow.run(() => {
const root = new Token(title, config);
root.type = 'root';
root.parseOnce(0, include).parseOnce();
const titleObj = new Title(root.toString(), defaultNs, config, decode, selfLink);
for (const key of ['main', 'fragment'] as const) {
const str = titleObj[key];
if (str?.includes('\0')) {
titleObj[key] = token.buildFromStr(str, BuildMethod.Text);
titleObj[key] = root.buildFromStr(str, BuildMethod.Text);
}
}
return titleObj;
});
return titleObj;
},

/** @implements */
Expand Down
5 changes: 1 addition & 4 deletions lib/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ export abstract class AstNode implements AstNodeBase {

/** @private */
getAttribute<T extends string>(key: T): TokenAttribute<T> {
if (key === 'padding') {
return 0 as TokenAttribute<T>;
}
return this[key as keyof this] as TokenAttribute<T>;
return (key === 'padding' ? 0 : this[key as keyof this]) as TokenAttribute<T>;
}

/** @private */
Expand Down
21 changes: 13 additions & 8 deletions parser/braces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const closes: Record<string, string> = {
export const parseBraces = (wikitext: string, config: Config, accum: Token[]): string => {
const source = String.raw`${
config.excludes?.includes('heading') ? '' : String.raw`^(\0\d+c\x7F)*={1,6}|`
}\[\[|\{{2,}|-\{(?!\{)`,
}\[\[|-\{(?!\{)`,
openBraces = String.raw`|\{{2,}`,
{parserFunction: [,,, subst]} = config,
stack: BraceExecArrayOrEmpty[] = [];
wikitext = wikitext.replace(re, (m, p1: string) => {
Expand All @@ -33,9 +34,9 @@ export const parseBraces = (wikitext: string, config: Config, accum: Token[]): s
return `\0${accum.length - 2}${marks.get(p1)}\x7F`;
});
const lastBraces = wikitext.lastIndexOf('}}') - wikitext.length;
let regex = new RegExp(source, 'gmu'),
let moreBraces = lastBraces + wikitext.length !== -1,
regex = new RegExp(source + (moreBraces ? openBraces : ''), 'gmu'),
mt: BraceExecArray | null = regex.exec(wikitext),
moreBraces = lastBraces + wikitext.length !== -1,
lastIndex: number | undefined;
while (
mt
Expand Down Expand Up @@ -140,14 +141,18 @@ export const parseBraces = (wikitext: string, config: Config, accum: Token[]): s
}
stack.push(...'0' in top ? [top] : [], mt!);
}
moreBraces &&= lastBraces + wikitext.length >= lastIndex;
let curTop = stack[stack.length - 1];
if (!moreBraces && curTop?.[0]?.startsWith('{')) {
stack.pop();
curTop = stack[stack.length - 1];
if (moreBraces && lastBraces + wikitext.length < lastIndex) {
moreBraces = false;
while (curTop?.[0]?.startsWith('{')) {
stack.pop();
curTop = stack[stack.length - 1];
}
}
regex = new RegExp(
source + (curTop ? `|${closes[curTop[0]![0]!]!}${curTop.findEqual ? '|=' : ''}` : ''),
source
+ (moreBraces ? openBraces : '')
+ (curTop ? `|${closes[curTop[0]![0]!]!}${curTop.findEqual ? '|=' : ''}` : ''),
'gmu',
);
regex.lastIndex = lastIndex;
Expand Down
1 change: 1 addition & 0 deletions src/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ export abstract class AttributeToken extends Token {
this.#tag = this.parentNode.name;
}
this.setAttribute('name', this.firstChild.text().trim().toLowerCase());
super.afterBuild();
}

/** @private */
Expand Down
10 changes: 4 additions & 6 deletions src/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,11 @@ export abstract class AttributesToken extends Token {

/** @private */
override afterBuild(): void {
if (this.type === 'table-attrs') {
const {parentNode} = this as this & {parentNode?: TableToken | TrToken | TdToken};
this.setAttribute(
'name',
parentNode?.type === 'td' && parentNode.subtype === 'caption' ? 'caption' : parentNode?.type,
);
const {parentNode} = this;
if (parentNode?.type === 'td' && parentNode.subtype === 'caption') {
this.setAttribute('name', 'caption');
}
super.afterBuild();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/converterFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export abstract class ConverterFlagsToken extends Token {
/** @private */
override afterBuild(): void {
this.#flags = this.childNodes.map(child => child.text().trim());
super.afterBuild();
}

/** @private */
Expand Down
1 change: 1 addition & 0 deletions src/imageParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export abstract class ImageParameterToken extends Token {
if (this.parentNode?.type === 'gallery-image' && !galleryParams.has(this.name)) {
this.setAttribute('name', 'invalid');
}
super.afterBuild();
}

/** @private */
Expand Down
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
MAX_STAGE,
BuildMethod,
} from '../util/constants';
import {Shadow} from '../util/debug';
import {generateForSelf} from '../util/lint';
import Parser from '../index';
import {AstElement} from '../lib/element';
Expand Down Expand Up @@ -77,7 +78,7 @@ export class Token extends AstElement {
readonly #accum;
#include?: boolean;
#built = false;
#string: string | undefined;
#string: [number, string] | undefined;

/** @class */
constructor(wikitext?: string, config = Parser.getConfig(), accum: Token[] = [], acceptable?: Acceptable) {
Expand Down Expand Up @@ -169,8 +170,8 @@ export class Token extends AstElement {
return nodes;
}

/** 将占位符替换为子Token */
#build(): void {
/** @private */
build(): void {
this.#stage = MAX_STAGE;
const {length, firstChild} = this,
str = String(firstChild);
Expand All @@ -179,7 +180,7 @@ export class Token extends AstElement {
this.normalize();
if (this.type === 'root') {
for (const token of this.#accum) {
token.#build();
token.build();
}
}
}
Expand All @@ -202,7 +203,7 @@ export class Token extends AstElement {
this.parseOnce(this.#stage, include);
}
if (n) {
this.#build();
this.build();
this.afterBuild();
}
return this;
Expand Down Expand Up @@ -334,6 +335,8 @@ export class Token extends AstElement {
return (this.#include ?? Boolean(this.getRootNode().#include)) as TokenAttribute<T>;
case 'accum':
return this.#accum as TokenAttribute<T>;
case 'built':
return this.#built as TokenAttribute<T>;
default:
return super.getAttribute(key);
}
Expand Down Expand Up @@ -457,13 +460,14 @@ export class Token extends AstElement {

/** @private */
override toString(separator?: string): string {
const root = this.getRootNode();
const {rev} = Shadow,
root = this.getRootNode();
if (
root.type === 'root'
&& root.#built
) {
this.#string ??= super.toString(separator);
return this.#string;
this.#string ??= [rev, super.toString(separator)];
return this.#string[1];
}
return super.toString(separator);
}
Expand Down
1 change: 1 addition & 0 deletions src/link/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export abstract class LinkBaseToken extends Token {
this.#delimiter = this.buildFromStr(this.#delimiter, BuildMethod.String);
}
this.setAttribute('name', this.#title.title);
super.afterBuild();
}

/** @private */
Expand Down
1 change: 1 addition & 0 deletions src/link/galleryImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ export abstract class GalleryImageToken extends FileToken {
/** @private */
override afterBuild(): void {
this.#setName(this.getTitle());
super.afterBuild();
}
}
1 change: 1 addition & 0 deletions src/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export abstract class ParameterToken extends Token {
parentNode.getArgs(name, false, false).add(this);
}
}
super.afterBuild();
}

/** @private */
Expand Down
10 changes: 7 additions & 3 deletions src/table/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import {SyntaxToken} from '../syntax';
import {AttributesToken} from '../attributes';
import type {AttributesParentBase} from '../../mixin/attributesParent';

declare type TableTypes = 'table' | 'tr' | 'td';

export interface TableBaseToken extends AttributesParentBase {}

/**
* 表格行,含开头的换行,不含结尾的换行
* @classdesc `{childNodes: [SyntaxToken, AttributesToken, ...Token]}`
*/
export abstract class TableBaseToken extends attributesParent(1)(Token) {
declare type: 'table' | 'tr' | 'td';
declare type: TableTypes;

declare readonly childNodes: readonly [SyntaxToken, AttributesToken, ...Token[]];
abstract override get firstChild(): SyntaxToken;
Expand All @@ -21,11 +23,13 @@ export abstract class TableBaseToken extends attributesParent(1)(Token) {
/**
* @param pattern 表格语法正则
* @param syntax 表格语法
* @param type 节点类型
* @param attr 表格属性
*/
constructor(
pattern: RegExp,
syntax?: string,
syntax: string,
type: TableTypes,
attr?: string,
config = Parser.getConfig(),
accum: Token[] = [],
Expand All @@ -36,7 +40,7 @@ export abstract class TableBaseToken extends attributesParent(1)(Token) {
new SyntaxToken(syntax, pattern, 'table-syntax', config, accum, {
}),
// @ts-expect-error abstract class
new AttributesToken(attr, 'table-attrs', this.type, config, accum) as AttributesToken,
new AttributesToken(attr, 'table-attrs', type, config, accum) as AttributesToken,
);
}
}
14 changes: 6 additions & 8 deletions src/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class TableToken extends TrBaseToken {
* @param attr 表格属性
*/
constructor(syntax: string, attr?: string, config?: Config, accum?: Token[]) {
super(/^(?:\{\||\{\{\{\s*!\s*\}\}|\{\{\s*\(!\s*\}\})$/u, syntax, attr, config, accum, {
super(/^(?:\{\||\{\{\{\s*!\s*\}\}|\{\{\s*\(!\s*\}\})$/u, syntax, 'table', attr, config, accum, {
});
}

Expand Down Expand Up @@ -100,13 +100,11 @@ export abstract class TableToken extends TrBaseToken {
const config = this.getAttribute('config'),
accum = this.getAttribute('accum'),
inner = halfParsed ? [syntax] : Parser.parse(syntax, this.getAttribute('include'), 2, config).childNodes;
const token = Shadow.run(() => super.insertAt(
new SyntaxToken(undefined, closingPattern, 'table-syntax', config, accum, {
}),
));
if (!halfParsed) {
token.afterBuild();
}
Shadow.run(() => {
const token = new SyntaxToken(undefined, closingPattern, 'table-syntax', config, accum, {
});
super.insertAt(token);
});
(this.lastChild as SyntaxToken).replaceChildren(...inner);
}

Expand Down
10 changes: 7 additions & 3 deletions src/table/td.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {BoundingRect} from '../../lib/rect';
import {
BuildMethod,
} from '../../util/constants';
import {Shadow} from '../../util/debug';
import Parser from '../../index';
import {Token} from '../index';
import {TableBaseToken} from './base';
Expand All @@ -28,7 +29,7 @@ declare type TdAttrGetter<T extends string> = T extends keyof TdSpanAttrs ? numb
export abstract class TdToken extends TableBaseToken {
override readonly type = 'td';
#innerSyntax = '';
#syntax: TdSyntax | undefined;
#syntax: [number, TdSyntax] | undefined;

declare readonly childNodes: readonly [SyntaxToken, AttributesToken, Token];
abstract override get parentNode(): TrToken | TableToken | undefined;
Expand Down Expand Up @@ -64,6 +65,7 @@ export abstract class TdToken extends TableBaseToken {
super(
/^(?:\n[^\S\n]*(?:[|!]|\|\+|\{\{\s*!\s*\}\}\+?)|(?:\||\{\{\s*!\s*\}\}){2}|!!|\{\{\s*!!\s*\}\})$/u,
syntax,
'td',
attr,
config,
accum,
Expand All @@ -83,8 +85,9 @@ export abstract class TdToken extends TableBaseToken {

/** 表格语法信息 */
#getSyntax(): TdSyntax {
this.#syntax ??= this.#computeSyntax();
return this.#syntax;
const {rev} = Shadow;
this.#syntax ??= [rev, this.#computeSyntax()];
return this.#syntax[1];
}

/** 表格语法信息 */
Expand Down Expand Up @@ -112,6 +115,7 @@ export abstract class TdToken extends TableBaseToken {
if (this.#innerSyntax.includes('\0')) {
this.#innerSyntax = this.buildFromStr(this.#innerSyntax, BuildMethod.String);
}
super.afterBuild();
}

/** @private */
Expand Down
2 changes: 1 addition & 1 deletion src/table/tr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export abstract class TrToken extends TrBaseToken {
* @param attr 表格属性
*/
constructor(syntax: string, attr?: string, config?: Config, accum?: Token[]) {
super(/^\n[^\S\n]*(?:\|-+|\{\{\s*!\s*\}\}-+|\{\{\s*!-\s*\}\}-*)$/u, syntax, attr, config, accum, {
super(/^\n[^\S\n]*(?:\|-+|\{\{\s*!\s*\}\}-+|\{\{\s*!-\s*\}\}-*)$/u, syntax, 'tr', attr, config, accum, {
});
}
}
1 change: 1 addition & 0 deletions src/transclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export abstract class TranscludeToken extends Token {
if (this.modifier.includes('\0')) {
this.setAttribute('modifier', this.buildFromStr(this.modifier, BuildMethod.String));
}
super.afterBuild();
}

/** @private */
Expand Down
2 changes: 1 addition & 1 deletion typings/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare global {
T extends 'accum' ? Token[] :
T extends 'parentNode' ? Token | undefined :
T extends 'childNodes' ? AstNodes[] :
T extends 'bracket' | 'include' | 'plain' ? boolean :
T extends 'bracket' | 'include' | 'plain' | 'built' ? boolean :
T extends 'title' ? Title :
unknown;
}
6 changes: 6 additions & 0 deletions util/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ export const Shadow = {
const {running} = this;
this.running = true;
try {
const {Token: AnyToken}: typeof import('../src/index') = require('../src/index');
const result = callback();
if (result instanceof AnyToken && !result.getAttribute('built')) {
result.afterBuild();
}
this.running = running;
return result;
} catch (e) {
this.running = running;
throw e;
}
},

rev: 0,
};

/**
Expand Down

0 comments on commit 3aed261

Please sign in to comment.