-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.js
66 lines (56 loc) · 1.47 KB
/
gen.js
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
var fs=require("fs");
var parsehtml=require("./parsehtml");
var lists=[
['dn','lst/dn.lst','genxml/dn.js'],
// ['mn','lst/mn.lst','genxml/mn.js'],
// ['sn','lst/sn.lst','genxml/sn.js'],
// ['an','lst/an.lst','genxml/an.js'],
]
var stringify=function(json){
var out=[],r;
for (var i in json) {
if (!json[i])continue;
r="";
var r="{text:`"+json[i].text+"`\n,links:[\n";
for (j=0;j< json[i].links.length;j++) {
r+=JSON.stringify(json[i].links[j])+"\n";
}
r+="]},\n";
out.push(r);
}
return "module.exports=[\n"+out.join("\n")+"]";
}
var getsid=function(fn,prefix){
var n1="",n2="";
var first2=prefix.substr(0,2);
var at=fn.lastIndexOf("/");
var filename=fn.substr(at+1);
n1=parseInt(filename);
var at=fn.substr(0,at).lastIndexOf("/");
var parent=fn.substr(at+1);
n2=parseInt(parent);
if (first2=="dn" || first2=="mn") {
return prefix+n1;
} else {
return prefix+n2+"."+n1;
}
}
var processfile=function(prefix,fn){
var content=fs.readFileSync(fn,'utf8').replace(/\r?\n/g,"\n");
var sid=getsid(fn,prefix);
var body=parsehtml.getBody(content,prefix,fn);
if (!body) {
console.log("empty file",fn);
return;
}
var parsed=parsehtml.parseBody(body,fn);
return {text:parsed.text,links:parsed.links};
}
var processlist=function(lst){
var files=fs.readFileSync(lst[1],'utf8').trim().split(/\r?\n/);
//files.shift();
//files.length=1;
var out=files.map(processfile.bind(this,lst[0]));
fs.writeFileSync(lst[2], stringify(out),"utf8");
}
lists.forEach(processlist);