-
Notifications
You must be signed in to change notification settings - Fork 3
/
textToHTML.js
112 lines (106 loc) · 3.28 KB
/
textToHTML.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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { htmlTemplate } = require("./htmlTemplate");
var argv = require("./yargsConfig");
var md = require("markdown-it")({
html: true,
linkify: true,
typographer: true,
});
const processMarkdown = (data) => {
return md.render(data);
};
const generatePara = (data) => {
return data
.split(/\r?\n\r?\n/)
.map((para) => `\n<p>${para.replace(/\r?\n/, " ")}</p>`)
.join(" ");
};
let tempString;
if (fs.existsSync(argv.input)) {
if (fs.lstatSync(argv.input).isDirectory()) {
//if the input is a directory
fs.readdirSync(argv.input).forEach((file) => {
let fileExt = path.extname(file);
fs.readFile(path.join(argv.input, file), "utf-8", function (error, data) {
if (path.extname(file) === ".txt") {
let fileName = path.basename(file, ".txt");
const html = generatePara(data);
tempString = htmlTemplate(argv.s, fileName, argv.l, html);
fs.writeFile(
`${argv.output}/${path.basename(file, ".txt")}.html`,
tempString,
(error) => {
if (error) {
console.log("Error writing to directory.");
process.exitCode = -1;
}
}
);
}
if (fileExt === ".md") {
let fileName = path.basename(file, ".md");
const html = processMarkdown(data);
tempString = htmlTemplate(argv.s, fileName, argv.l, html);
fs.writeFile(
`${argv.output}/${path.basename(file, ".md")}.html`,
tempString,
(error) => {
if (error) {
console.log("Error writing to directory.");
process.exitCode = -1;
}
}
);
}
});
});
} else {
//if the input is a single file
fs.readFile(argv.input, "utf8", function (error, data) {
if (error) {
console.log("Error reading from input file.");
process.exitCode = -1;
}
let fileExt = path.extname(argv.input);
if (fileExt === ".md") {
let fileName = path.basename(argv.input, ".md");
const html = processMarkdown(data);
tempString = htmlTemplate(argv.s, fileName, argv.l, html);
fs.writeFile(
`${argv.output}/${path.basename(argv.input, ".md")}.html`,
tempString,
(error) => {
if (error) {
console.log("Error creating HTML file from '.md'.");
process.exitCode = -1;
}
}
);
}
if (fileExt === ".txt") {
let fileName = path.basename(argv.input, ".txt");
const html = generatePara(data);
tempString = htmlTemplate(argv.s, fileName, argv.l, html);
fs.writeFile(
`${argv.output}/${path.basename(argv.input, ".txt")}.html`,
tempString,
(error) => {
if (error) {
console.log("Error creating HTML file from '.txt'.");
process.exitCode = -1;
}
}
);
}
});
}
} else {
console.log("File or folder does not exist.");
process.exitCode = -1;
}
process.on("exit", (code) => {
console.log(`Exiting with code: ${code}`);
});
module.exports = { processMarkdown, generatePara };