-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
226 lines (212 loc) · 6.95 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
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
226
const path = require('path');
const { version } = require('./package.json');
const { htmlGenerator } = require('./htmlGenerator');
const { mdFileNewFeature } = require('./mdFileNewFeature');
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',
},
lang: {
alias: 'l',
demandOption: true,
default: '.',
describe: 'generate the lang attribute',
type: 'string',
},
config: {
alias: 'c',
//default: 'default.json',
describe: 'accept a file path to a JSON config file',
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;
});
}
if (argv.lang == '.') {
var lang = 'en-CA';
} else {
lang = argv.lang;
}
//check for config argument
if (argv.config != null) {
// When it has a config argument, then do this:
let notValid = false;
console.log(`argv.config argument -- '${argv.config}' is not null!`);
if (fs.existsSync(argv.config)) {
// contains the json file
console.log('argv.config is exist!');
fs.readFile(`${argv.config}`, 'utf8', (err, jsonString) => {
if (err) {
console.log('File read failed:', err);
return;
}
let obj = JSON.parse(jsonString);
obj.input == undefined ? (notValid = true) : (argv.input = obj.input);
obj.lang == undefined ? (lang = 'en-CA') : (lang = obj.lang);
obj.output == undefined
? (argv.output = './dist')
: (argv.output = obj.output); // static output path
if (!notValid) {
let filePath = fs.statSync(obj.input, obj.lang);
convert(filePath);
} else {
console.log(
`Input filename is empty in the ${argv.config}! Nothing to convert this time!`
);
}
});
} else {
//not such a json file exist
let errMsg = `Sorry, we can't find your config json file, please try again!!`;
console.log(errMsg);
notValid = true;
}
} else {
// When it doesn't have a config argument, then do this:
console.log(`No config argument detected this time! `);
let filePath = fs.statSync(argv.input, argv.lang);
convert(filePath);
}
// put the convert code into a function
function convert(filePath) {
if (filePath.isDirectory()) {
console.log('argv.input is a folder!');
fs.readdirSync(argv.input).forEach((file) => {
//Display all the files in the directory
//console.log("File name: ", file);
let fileType = file.split('.').pop();
//Convert the .txt or .md file into a HTML file
if (fileType == 'txt' || fileType == 'md') {
fs.readFile(
argv.input + '/' + file.toString(),
'utf-8',
function (err, fullText) {
if (err) return console.log(err);
let fileName = path.parse(file).name;
//name the file without space
let validFileName = fileName.split(' ').join('');
// let validFname = fname[0].split(' ').join('');
htmlConversion(fileName, validFileName, fileType, fullText);
}
);
}
});
console.log('The HTML files have been saved to ./dist!');
} else {
console.log('argv.input is not a FOLDER!');
let fileType = argv.input.split('.').pop();
//console.log(fileType);
//convert the .txt or .md file into a HTML file
if (fileType == 'txt' || fileType == 'md') {
fs.readFile(argv.input, 'utf8', function (err, fullText) {
if (err) return console.log(err);
let fileName = argv.input.split('.');
//console.log(fname) //[ 'Silver Blaze', 'txt' ]
let validFileName = fileName[0].split(' ').join('');
htmlConversion(fileName, validFileName, fileType, fullText);
});
} else {
fileType =
'Sorry, only .txt and .md files are allowed! Please try again!'; //md
console.log(fileType);
}
}
}
// function mdFileNewFeature(content) {
// const html = [];
// content.forEach((e) => {
// if (e.includes('### ')) {
// html.push(
// `<h3>${e.replace('###', '').replace('---', '<hr>')}</h3> <br />`
// );
// } else if (e.includes('## ')) {
// html.push(
// `<h2>${e.replace('##', '').replace('---', '<hr>')}</h2> <br />`
// );
// } else if (e.includes('# ')) {
// html.push(
// `<h1>${e
// .replace('#', '')
// .replace('---', '<hr>')}</h1> <br /><hr /><br />`
// );
// } else {
// html.push(
// `<p>${e.replace(/\r?\n/, ' ').replace('---', '<hr>')}</p> <br />`
// );
// }
// });
// return html;
// }
// function htmlGenerator(lang, title, html, footer) {
// return typeof html == 'object'
// ? `<!doctype html>\n` +
// `<html lang="${lang}">\n<head>\n<meta charset="UTF-8">\n<title>${title}</title>\n` +
// `<link rel="stylesheet" href="../src/css/style.css">\n</head>\n` +
// `<body>\n` +
// `<div class = "container">\n` +
// `${html.join(' ')}` +
// `</div>\n</body>\n` +
// `<footer> \n ${footer}\n</footer>\n</html>`
// : `<!doctype html>\n` +
// `<html lang="${lang}">\n<head>\n<meta charset="UTF-8">\n<title>${title}</title>\n` +
// `<link rel="stylesheet" href="../src/css/style.css">\n</head>\n` +
// `<body>\n` +
// `<div class = "container">\n` +
// `<h1>${title} </h1>\n` +
// `${html}` +
// `</div>\n</body>\n` +
// `<footer> \n ${footer}\n</footer>\n</html>`;
// }
function htmlConversion(fileName, validFileName, fileType, fullText) {
let footer = '<p class="center">© 2021 OSD600 Seneca</p>';
if (fileType == 'txt') {
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(' ');
let tempHtml = htmlGenerator(lang, t[0], html, footer);
//Write file
fs.writeFile(`./dist/${validFileName}.html`, tempHtml, (err) => {
if (err) throw err;
});
} else if (fileType == 'md') {
let contents = fullText.split(/\r?\n\r?\n/);
console.log('Title is :', validFileName);
let html = mdFileNewFeature(contents);
let tempHtml = htmlGenerator(lang, fileName[0], html, footer);
//Write file
fs.writeFile(`./dist/${validFileName}.html`, tempHtml, (err) => {
if (err) throw err;
});
}
}