- Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.ts
215 lines (199 loc) · 4.7 KB
/
parser.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import*aspathfrom"path";
import*astsfrom"ntypescript";
import*ashtmlparser2from"htmlparser2";
import"deno.land/std/fs/mod.ts";
import"deno.land/std/path/mod.ts";
import{download}from"./utils";
importtype{ISub}from"./typings";
classSubmissionimplementsISub{
name: "";
info: "";
time: "";
status: "Accepted";
runtime: "";
memory: "";
code: "";
}
/**
* 解析器
* @author yalda
* @date 2019.08
*/
exportclassParserTS{
privatesubmissions: Submission[]=[];
/**
* 加载文件
*/
publicload=({ filePath }: any): Parser=>{
filePath=path.join(filePath,"index.ts");
letfile=fs.readFileSync(filePath,{encoding: "utf8"}).trim();
constsourseFile: ts.SourceFile=ts.createSourceFile(
"code.ts",
file,
ts.ScriptTarget.ES5,
true,
);
this.parseSourse(sourseFile);
returnthis;
};
/**
* 递归遍历 AST
*/
privateparseSourse(node: ts.SourceFile){
constsyntax: ts.Node=node
.getChildren()
.find((n: ts.Node)=>ts.formatSyntaxKind(n.kind)=="SyntaxList");
this.parseSyntax(syntax);
}
/**
* 函数语句 & 箭头函数定义语句进行格式化
*/
privateparseSyntax(syntax: ts.Node){
const_this=this;
syntax.getChildren().forEach((stmt: ts.Node)=>{
constkind=ts.formatSyntaxKind(stmt.kind);
if(kind=="FunctionDeclaration"||kind=="VariableStatement"){
constsubmission=newSubmission();
submission.sourse=stmt.getText();
constinfo: Info=_this.parseDoc(stmt);
submission.info=info;
submission.name=info.title;
_this.submissions.push(submission);
}
});
}
/**
* 解析注解进行格式化
*/
privateparseDoc(stmt: ts.Node): Info{
constinfo=newInfo();
constcommNode: any=stmt
.getChildren()
.find((n: ts.Node)=>ts.formatSyntaxKind(n.kind)==="JSDocComment");
let[title, ...comment]=(commNode.comment||"").split("\n");
info.title=title;
info.comment=comment.join("\n");
commNode
.getChildren()
.forEach((n: any)=>(info[n.tagName.text]=n.comment));
returninfo;
}
/**
* 获取 sourse map
*/
getgetSourse(): Submission[]{
returnthis.submissions;
}
}
// parse html
classParseHtml{}
letstr: string=fs.readFileSync("./test.md",{encoding: "utf8"});
str=str.replace(/\\n/g,"");
str=str.replace(/\\t/g,"");
str=str.replace(/\n/g,"");
str=str.replace(/\t/g,"");
lets=newSet();
letc={
value: "",
cache: "",
};// context
constparser=newhtmlparser2.Parser(
{
asynconopentag(name: any,attribs){
// 块级作用域
if(name==="p"){
// 段落
return"\n**\n";
}
if(name==="code"){
return"`";
}
if(name==="pre"){
return"```bash\n";
}
if(name==="em"||name==="i"){
return"**";
}
if(name==="strong"||name==="b"){
return"*";
}
if(name==="ul"||name==="ol"){
// <ul> \n
}
if(name==="li"){
// <li> - ***\n
}
if(name==="img"){
// \
const{ src }=attribs;
// let buffer = await download(src);
// fs.writeFileSync('./assets/images', buffer, {
// encoding: 'binary'
// });
return``;
}
if(name==="a"){
return`[]()`;
}
},
ontext(text: string){
// Example
if(
text==="Example:"||
text==="Example 1:"||
text==="Example 2:"||
text==="Example 3:"||
text==="Example 4:"||
text==="Example 5:"
){
}
// Note
if(text==="Note:"||text==="NOTE:"){
}
// Allow Up
if(text==="Follow up:"){
}
// Constraints:
if(text==="Constraints:"){
}
// Credits
if(text==="Credits:"){
}
},
onclosetag(name: string){
if(name==="p"){
// 段落
return"\n";
}
if(name==="code"){
return"`";
}
if(name==="pre"){
return"```\n\n";
}
if(name==="em"||name==="i"){
return"**";
}
if(name==="strong"||name==="b"){
return"*";
}
if(name==="ul"||name==="ol"){
// </ul> \n
}
if(name==="li"){
// </li> - ***\n
}
if(name==="img"){
// \n
}
if(name==="a"){
return`[]()`;
}
},
onerror(e){},
onend(){},
},
{decodeEntities: true},
);
parser.write(str);
parser.end();