This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
site-generator.ts
162 lines (140 loc) · 4.53 KB
/
site-generator.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
import * as fs from "fs";
import * as path from "path";
let outputDirName : string = "output-site";
let inputFilename : string = "site-generator-config.json";
// https://stackoverflow.com/questions/13786160/copy-folder-recursively-in-node-js
function copyFileSync(source: string, target: string) : void {
let targetFile : string = target;
if (fs.existsSync(target) && fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync(source: string, target: string) : void {
let files : Array<string> = [];
let targetFolder : string = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
//copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function(file: string) {
let curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
function copyFilesInFolderSync(source: string, target: string) : void {
let files: Array<string> = [];
//copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function(file: string) {
let curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, target);
} else {
copyFileSync(curSource, target);
}
});
}
}
class InputType {
copyDirectories: Array<string>;
copyFilesInDirectories: Array<string>;
constructor(input: any) {
if (input.hasOwnProperty("copy-directories"))
this.copyDirectories = <Array<string>>input["copy-directories"];
else
this.copyDirectories = [];
if (input.hasOwnProperty("copy-files-in-directories"))
this.copyFilesInDirectories = <Array<string>>input["copy-files-in-directories"];
else
this.copyFilesInDirectories = [];
}
}
if (!fs.existsSync(outputDirName)) {
fs.mkdirSync(outputDirName);
}
let input : InputType = new InputType(require("../" + inputFilename));
input.copyDirectories.forEach(function(directory: string) {
copyFolderRecursiveSync(directory, outputDirName);
});
input.copyFilesInDirectories.forEach(function(file:string){
copyFilesInFolderSync(file, outputDirName);
});
//generate main page
let indexPage : string = fs.readFileSync("README.md", "utf8");
let indexHeader : string =
"---\n" +
"layout: readme\n" +
"---\n";
//replace first line with indexHeader
indexPage = indexHeader + indexPage.substring(indexPage.indexOf('\n') + 1);
fs.writeFile(path.join(outputDirName, "README.md"), indexPage, ()=>{});
//generate trie tree
namespace SearchTree {
class Branch {
children: any | undefined;
//if value is not null, then we are at the end
value: undefined | number;
constructor() {
this.children = undefined;
}
createChild(key: string) : Branch {
if (this.children === undefined)
this.children = {};
this.children[key] = new Branch();
let l = this.children[key];
return l !== undefined ? l : new Branch();
}
setValue(value:number) : void {
this.value = value;
}
};
export class Tree {
allKeys: Array<string>;
root:Branch;
constructor() {
this.allKeys = [];
this.root = new Branch();
}
insert(sourceKey: string) : void {
let key: string = sourceKey.substr(0, sourceKey.lastIndexOf('.'));
let value:number = this.allKeys.push(key) - 1;
let position : Branch = this.root;
for (let i:number = 0; i < key.length; ++i) {
let letter: string = key[i];
//basically position = position.children[letter];
if (position === undefined)
throw "position is undefined";
if (position.children === undefined) {
position = position.createChild(letter);
} else {
let next: Branch | undefined = position.children[letter];
if (next === undefined)
position = position.createChild(letter);
else
position = next;
}
if (position.value === undefined)
position.setValue(value);
}
}
}
}
let searchTree : SearchTree.Tree = new SearchTree.Tree();
let waifuFiles = fs.readdirSync("waifus");
waifuFiles.sort().forEach(function(file:string) {
searchTree.insert(file);
});
let searchTreeJson: string = JSON.stringify(searchTree);
//replace repeated variables with shorten names
searchTreeJson = searchTreeJson.replace(/"children":/g, "\"c\":");
searchTreeJson = searchTreeJson.replace(/"value":/g, "\"v\":");
fs.writeFile(path.join(outputDirName, "search-tree.json"), searchTreeJson, ()=>{});