-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
225 lines (211 loc) · 5.95 KB
/
index.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
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
216
217
218
219
220
221
222
223
224
225
/*
Name- Anshul Gandhi
Course- DPS 909
Project Name- Static site Generator (SSG)
*/
//start here
const config = {
input: "./test.txt",
output: "./dist",
lang: "en-CA",
};
import readline from "readline";
import path from "path";
import fs from "fs";
export function ssg_(file, language = "en-CA", configPath = null) {
let extension, filename, dir;
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true, force: true });
}
if (configPath) {
applyConfig(configPath);
extension = path.extname(config.input);
filename = path.basename(config.input);
dir = config.output;
file = config.input;
language = config.lang;
} else {
//extension = path.extname(file);
filename = path.basename(file);
dir = "./dist";
}
fs.lstat(file, (err, stats) => {
if (err) {
//console.log(err);
return;
} else {
if (stats.isDirectory()) {
fs.readdir(file, (err, files) => {
files.forEach((fileN) => {
if (err) {
console.log(err);
return;
}
if (path.extname(fileN) == ".txt" || path.extname(fileN) == ".md") {
readFile(file + "/" + fileN).then(function (data) {
writeFile(fileN, data, language);
});
}
});
if (configPath) {
generateIndexHtml(files, true, config.lang);
} else {
generateIndexHtml(files, true);
}
});
} else {
if (
path.extname(filename) == ".txt" ||
path.extname(filename) == ".md"
) {
readFile(file).then((data) => {
writeFile(filename, data, language);
if (configPath) {
generateIndexHtml(filename, false, config.lang);
} else {
generateIndexHtml(filename, false);
}
});
}
}
}
});
}
//function to generate index.html
function generateIndexHtml(inp, Dir, language = "en-CA") {
var content = "";
var htmlFile;
if (Dir) {
for (var file of inp) {
htmlFile = file.substring(0, file.length - 4) + ".html";
content += `<a href="${htmlFile}"> ${htmlFile} </a>\n<br>`;
}
} else {
var htmlFile = inp.substring(0, inp.length - 4) + ".html";
content += `<a href="${htmlFile}"> ${htmlFile} </a>\n<br>`;
}
const template = `<!doctype html><html lang="${language}"><head><meta charset="utf-8"><title>Main Page</title><link rel="stylesheet" href="../style.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
<script>
function addDarkmodeWidget() {
new Darkmode().showWidget();
}
window.addEventListener('load', addDarkmodeWidget);
</script></head>
<body>
${content}
</body></html>`;
fs.writeFile(`./${config.output}/index.html`, template, (err) => {
if (err) {
//console.log(err);
return;
}
console.log(
`Index file created successfully check ${config.output} folder`
);
});
}
//function to read file
export function readFile(file) {
return new Promise(async (resolve, reject) => {
let arr = [];
const rl = readline.createInterface({
input: fs.createReadStream(file),
});
//read line by line
for await (const line of rl) {
if (line !== "") {
arr.push(line);
} else {
arr.push("\n");
}
}
resolve(arr);
});
}
//function to write file
export function writeFile(filename, data, language) {
return new Promise((resolve, reject) => {
let content = "";
let html = "";
let title = filename.substring(0, filename.length - 4);
var filedest = config.output + "/" + filename + ".html";
for (var line of data) {
if (line !== "\n") {
// If the line starts with a '#', then check if h1 or h2.
if (line.charAt(0) == "#") {
// If the 2nd char is a space, then 'content' should be header1.
if (line.charAt(1) == " ") {
var str = new String();
str = line.substring(2);
content += `<h1>${str}</h1>`;
}
// If the 2nd char is also a '#' and the 3rd char is a space, then 'content' should be header2.
else if (line.charAt(1) == "#" && line.charAt(2) == " ") {
var str = new String();
str = line.substring(3);
content += `<h2>${str}</h2>`;
}
}
// Otherwise, 'content' should be placed in a paragraph
else {
content += `<p>${line}</p>`;
//convert --- to hr
if (line == "---") {
content += `<hr>`;
}
}
} else {
content += "\n";
}
}
html = `
<!doctype html><html lang="${language}">
<head>
<meta charset="utf-8">
<title>${title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../style.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
<script>
function addDarkmodeWidget() {
new Darkmode().showWidget();
}
window.addEventListener('load', addDarkmodeWidget);
</script>
</head>
<body >
${content}
</body>
</html>`;
fs.writeFile(filedest, html, (err) => {
if (err) {
//console.log(err);clear
return;
}
console.log(
"File created successfully " + filename + "check the dist folder"
);
});
resolve(html);
});
}
export function applyConfig(filename) {
try {
const rawdata = fs.readFileSync(filename);
const parsedConfig = JSON.parse(rawdata);
if (parsedConfig.input) {
config.input = parsedConfig.input;
}
if (parsedConfig.output) {
config.output = parsedConfig.output;
}
if (parsedConfig.lang) {
config.lang = parsedConfig.lang;
}
} catch (error) {
console.error(error.message);
process.exit();
}
}
export { config };