generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ParserCollection.ts
42 lines (38 loc) · 1.01 KB
/
ParserCollection.ts
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
import { Pattern } from './Pattern';
import { Card } from './card';
// 解析器集合
export class ParserCollection implements PatternParser {
private parsers: PatternParser[];
private static instance: ParserCollection;
private constructor() {
this.parsers = [];
}
public Parse(card: Card): Pattern[] {
if (this.parsers.length == 0) {
throw new Error('No parsers implemented.');
}
let tmpResults: Pattern[] = [];
for (let parser of this.parsers) {
let results = parser.Parse(card);
tmpResults.push(...results);
}
return tmpResults;
}
public static getInstance(): ParserCollection {
if (!ParserCollection.instance) {
ParserCollection.instance = new ParserCollection();
}
return ParserCollection.instance;
}
public registerParser(parser: PatternParser): void {
this.parsers.push(parser);
}
public clean() {
this.parsers = []
}
}
// 卡片解析器 将卡片解析成为视图
// 卡片解析器负责解析卡片
export interface PatternParser {
Parse(card: Card): Pattern[];
}