forked from DerekJxy/My-First-SSG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
128 lines (110 loc) · 3.63 KB
/
server.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
const path = require("path");
const { version } = require('./package.json');
let fs = require('fs');
let argv = require('yargs/yargs')(process.argv.slice(2))
.usage('This is my awesome program\n\nUsage: $0 [options]')
.help('help').alias('help', 'h')
.version('version', version).alias('version', 'v')
.alias('i', 'input')
.command("--input", "filename")
.options({
input: {
alias: 'input',
demandOption: true,
default: '.',
describe: 'convert .txt file to html file',
type: 'string'
},
output: {
alias: 'o',
demand: true,
default: './dist',
type: 'string'
}
})
.argv;
// Check ./dist folder
if(fs.existsSync("./dist")){
fs.rmdirSync("./dist",{recursive: true});
fs.mkdir("./dist", err=>{
if(err) throw err;
});
}
else{
fs.mkdir("./dist", err=>{
if(err) throw err;
});
}
//Define variables
let stats = fs.statSync(argv.input);
let tempHtml;
let footer = '<p class="center">© 2021 OSD600 Seneca</p>';
let fileType ='';
if(stats.isDirectory()){
fs.readdirSync(argv.input).forEach(file =>{
//Display all the files in the directory
console.log("File name: ", file);
fileType = file.split('.').pop();
//Only convert the .txt file into a HTML file
if(fileType == 'txt'){
fs.readFile(argv.input + "/"+ file.toString(), 'utf-8', function(err, fullText){
if(err) return console.log(err);
let fname = path.parse(file).name;
//name the file without space
let validFname = fname.split(' ').join('');
let t = fullText.split(/\r?\n\r?\n/);
//console.log("Title is :", t[0]);
let content = t.slice(1,t.length);
let html = content
.map(para =>
`<p>${para.replace(/\r?\n/, ' ')}</p> </br>`
).join(' ');
//HTML
tempHtml = `<!doctype html>\n` + `<html lang="en">\n<head>\n<meta charset="UTF-8">\n<title>${t[0]}</title>\n` +
`<link rel="stylesheet" href="../src/css/style.css">\n</head>\n` +
`<body>\n` + `<div class = "container">\n`+`<h1>${t[0]} </h1>\n` + `${html}` + `</div>\n</body>\n` +
`<footer> \n ${footer}\n</footer>\n</html>`;
//Write file
fs.writeFile(`./dist/${validFname}.html`, tempHtml, err=>{
if(err) throw err;
});
})
}
})
console.log('The HTML files have been saved to ./dist!');
}
else{
fileType = argv.input.split('.').pop();
//console.log(fileType);
//Only convert the .txt file into a HTML file
if(fileType == 'txt'){
fs.readFile(argv.input, 'utf8', function(err, fullText){
if(err) return console.log(err);
let fname = argv.input.split(".");
//console.log(fname) //[ 'Silver Blaze', 'txt' ]
let validFname = fname[0].split(' ').join('');
let t = fullText.split(/\r?\n\r?\n/);
//console.log("Title is :", t[0]);
let content = t.slice(1,t.length);
let html = content
.map(para =>
`<p>${para.replace(/\r?\n/, ' ')}</p> </br>`
).join(' ');
//HTML
tempHtml = `<!doctype html>\n` + `<html lang="en">\n<head>\n<meta charset="UTF-8">\n<title>${t[0]}</title>\n` +
`<link rel="stylesheet" href="../src/css/style.css">\n</head>\n` +
`<body>\n` + `<div class = "container">\n`+`<h1>${t[0]} </h1>\n` + `${html}` + `</div>\n</body>\n` +
`<footer> \n ${footer}\n</footer>\n</html>`;
//Write file
//console.log(validFname);
fs.writeFile(`./dist/${validFname}.html`, tempHtml, err=>{
if(err) throw err;
console.log('The HTML file has been saved to ./dist!');
});
});
}
else{
fileType = 'Sorry, only .txt file allowed! Please try again!'
console.log(fileType);
}
}