Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f02a3e3

Browse files
committed
Make basic prototype
1 parent 7f6e872 commit f02a3e3

File tree

5 files changed

+384
-56
lines changed

5 files changed

+384
-56
lines changed

package-lock.json

Lines changed: 6 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "<package-name>",
2+
"name": "bbcode-ast",
33
"version": "1.0.0",
4-
"description": "<package-description>",
4+
"description": "Generate an AST of a BBCode fragment.",
55
"main": "dist/index.js",
66
"type": "commonjs",
77
"types": "dist/index.d.ts",
@@ -13,24 +13,19 @@
1313
},
1414
"repository": {
1515
"type": "git",
16-
"url": "git+https://github.com/PythonCoderAS/<package-name>.git"
16+
"url": "git+https://github.com/PythonCoderAS/bbcode-ast.git"
1717
},
1818
"keywords": [],
1919
"author": "PythonCoderAS",
2020
"license": "MIT",
2121
"bugs": {
22-
"url": "https://github.com/PythonCoderAS/<package-name>/issues"
23-
},
24-
"homepage": "https://github.com/PythonCoderAS/<package-name>#readme",
25-
"dependencies": {
22+
"url": "https://github.com/PythonCoderAS/bbcode-ast/issues"
2623
},
24+
"homepage": "https://github.com/PythonCoderAS/bbcode-ast#readme",
2725
"devDependencies": {
2826
"@types/chai": "^4.3.3",
29-
"@types/chai-as-promised": "^7.1.5",
3027
"@types/mocha": "^9.1.1",
31-
"@types/node": "^18.7.6",
3228
"chai": "^4.3.6",
33-
"chai-as-promised": "^7.1.1",
3429
"eslint": "^8.22.0",
3530
"eslint-config-prettier": "^8.5.0",
3631
"eslint-config-pythoncoderas-combo": "^1.1.3",

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Parser from "./parser";
2+
3+
export * from "./node";
4+
export { default as Parser } from "./parser";
5+
6+
const defaultTags = ["b", "u", "i", "s", "center", "right", "color", "size", "yt", "list", "url", "img", "spoiler", "code"]
7+
const defaultParser = new Parser(defaultTags, false);
8+
export default defaultParser;

src/node.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
export interface NodeConstructorParams {
2+
name: string;
3+
attributes?: { [key: string]: string };
4+
value?: string;
5+
}
6+
7+
export abstract class BaseNode {
8+
abstract name: string;
9+
10+
abstract clone(): BaseNode;
11+
abstract toString(): string;
12+
}
13+
14+
export interface ChildrenHolder {
15+
children: BaseNode[];
16+
17+
addChild(child: BaseNode): void;
18+
}
19+
20+
export interface AttributeHolder {
21+
attributes: { [key: string]: string };
22+
23+
setAttribute(key: string, value: string): void;
24+
}
25+
26+
export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
27+
name: string;
28+
attributes: { [key: string]: string };
29+
children: BaseNode[] = [];
30+
// For simple parameterized values, like [x=y]...[/x]
31+
value?: string;
32+
33+
constructor(params: NodeConstructorParams) {
34+
super();
35+
this.name = params.name;
36+
this.attributes = params.attributes || {};
37+
}
38+
39+
clone(): Node {
40+
const node = new Node({name: this.name, attributes: this.attributes, value: this.value});
41+
node.children = this.children.map(child => child.clone());
42+
return node;
43+
}
44+
45+
addChild(child: Node): void {
46+
this.children.push(child.clone());
47+
}
48+
49+
setValue(value: string): void {
50+
this.value = value;
51+
}
52+
53+
setAttribute(key: string, value: string): void {
54+
this.attributes[key] = value;
55+
}
56+
57+
58+
toString(): string {
59+
let nodeString = `[${this.name}`;
60+
if (this.value){
61+
nodeString += `=${this.value}`;
62+
}
63+
Object.entries(this.attributes).forEach(([key, value]) => {
64+
nodeString += ` ${key}="${value}"`;
65+
});
66+
nodeString += ']';
67+
this.children.forEach(child => {
68+
nodeString += child.toString();
69+
});
70+
nodeString += `[/${this.name}]`;
71+
return nodeString;
72+
}
73+
}
74+
75+
export class TextNode extends BaseNode {
76+
text: string;
77+
name: string = 'TextNode';
78+
79+
constructor(text: string) {
80+
super();
81+
this.text = text;
82+
}
83+
84+
clone(): TextNode {
85+
return new TextNode(this.text);
86+
}
87+
88+
toString(): string {
89+
return this.text;
90+
}
91+
}
92+
93+
export class RootNode extends BaseNode implements ChildrenHolder {
94+
name = "RootNode";
95+
children: BaseNode[];
96+
97+
constructor(children: BaseNode[] = []) {
98+
super();
99+
this.children = children;
100+
}
101+
102+
addChild(child: Node): void {
103+
this.children.push(child.clone());
104+
}
105+
106+
clone(): RootNode {
107+
return new RootNode(this.children.map(child => child.clone()));
108+
}
109+
110+
toString(): string {
111+
return this.children.map(child => child.toString()).join('');
112+
}
113+
}
114+
115+
export class ListItemNode extends RootNode {
116+
name = "ListItemNode";
117+
118+
toString(): string {
119+
return "[*]" + super.toString();
120+
}
121+
}

0 commit comments

Comments
 (0)