From 8a6bbec146f97682f18fde0229ac8717c3691f9a Mon Sep 17 00:00:00 2001 From: Vivian Vu Date: Mon, 27 Sep 2021 22:17:22 -0400 Subject: [PATCH] Add optional specifying language feature --- bin/index.js | 20 ++++++++++++++++---- bin/modules/generateHTML.js | 11 +++++++++-- bin/modules/readFile.js | 9 +++++++-- bin/modules/readFolder.js | 10 +++++++++- bin/modules/readMDFile.js | 9 +++++++-- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/bin/index.js b/bin/index.js index 081211c..f537675 100644 --- a/bin/index.js +++ b/bin/index.js @@ -12,7 +12,7 @@ const htmlContainer = "./dist"; let cssLink = ""; const argv = require("yargs") - .usage("Usage: $0 --input [-s ]") + .usage("Usage: $0 --input [-s ] [-l ]") .option("i", { alias: "input", describe: ".txt file name", @@ -26,6 +26,13 @@ const argv = require("yargs") type: "string", demandOption: false, }) + .option("l", { + alias: "lang", + describe: "language used in HTML", + default: "en-CA", + type: "string", + demandOption: false, + }) .alias("v", "version") .version(pjson.name + " " + pjson.version) .alias("h", "help") @@ -47,14 +54,19 @@ async function checkInput() { } if (stats.isDirectory()) { - folder.readFolder(argv.input.join(" "), cssLink, htmlContainer); // folder + folder.readFolder( + argv.input.join(" "), + cssLink, + argv.lang, + htmlContainer + ); // folder } else if ( stats.isFile() && path.extname(argv.input.join(" ")) === ".txt" ) { - file.readFile(argv.input.join(" "), cssLink, htmlContainer); // text file + file.readFile(argv.input.join(" "), cssLink, argv.lang, htmlContainer); // text file } else if (stats.isFile() && path.extname(argv.input.join(" ")) === ".md") { - readMDFile(argv.input.join(" "), cssLink, htmlContainer); // markdown file + readMDFile(argv.input.join(" "), cssLink, argv.lang, htmlContainer); // markdown file } else { console.log("Invalid file extension, it should be .txt or .md"); } diff --git a/bin/modules/generateHTML.js b/bin/modules/generateHTML.js index 4daa0c2..ad9234f 100644 --- a/bin/modules/generateHTML.js +++ b/bin/modules/generateHTML.js @@ -2,16 +2,23 @@ const fs = require("fs"); const createHTML = require("create-html"); const chalk = require("chalk"); -module.exports.generateHTML = function (title, cssLink, body, htmlContainer) { +module.exports.generateHTML = function ( + language, + title, + cssLink, + body, + htmlContainer +) { const html = createHTML({ title: `${title}`, head: ``, body: `${body}`, css: `${cssLink}`, + lang: `${language}`, }); fs.writeFile(`${htmlContainer}/${title}.html`, html, (err) => { - if (err) console.log(err); + if (err) console.log("Here" + err); }); if (title === "index") { diff --git a/bin/modules/readFile.js b/bin/modules/readFile.js index 60fcb79..6a96aaa 100644 --- a/bin/modules/readFile.js +++ b/bin/modules/readFile.js @@ -3,7 +3,12 @@ const html = require("./generateHTML"); const path = require("path"); let body = ""; -module.exports.readFile = function (inputPath, cssLink, outputContainer) { +module.exports.readFile = function ( + inputPath, + cssLink, + language, + outputContainer +) { try { const data = fs.readFileSync(inputPath, "utf8"); body = data @@ -16,6 +21,6 @@ module.exports.readFile = function (inputPath, cssLink, outputContainer) { const title = path.basename(inputPath, ".txt"); - html.generateHTML(title, cssLink, body, outputContainer); + html.generateHTML(language, title, cssLink, body, outputContainer); return title; }; diff --git a/bin/modules/readFolder.js b/bin/modules/readFolder.js index 1cac099..d8f5460 100644 --- a/bin/modules/readFolder.js +++ b/bin/modules/readFolder.js @@ -5,7 +5,12 @@ const html = require("./generateHTML"); const path = require("path"); let body = ""; -module.exports.readFolder = function (inputPath, cssLink, outputContainer) { +module.exports.readFolder = function ( + inputPath, + cssLink, + language, + outputContainer +) { fs.readdir(inputPath, (err, files) => { if (err) { return console.log(err); @@ -19,6 +24,7 @@ module.exports.readFolder = function (inputPath, cssLink, outputContainer) { const fileName = fileModule.readFile( `${inputPath}/${file}`, cssLink, + language, outputContainer ); @@ -37,6 +43,7 @@ module.exports.readFolder = function (inputPath, cssLink, outputContainer) { const fileName = readMDFile( `${inputPath}/${file}`, cssLink, + language, outputContainer ); @@ -48,6 +55,7 @@ module.exports.readFolder = function (inputPath, cssLink, outputContainer) { // create index.html html.generateHTML( + language, "index", cssLink, `

Generated Sites

\n${body}`, diff --git a/bin/modules/readMDFile.js b/bin/modules/readMDFile.js index 8bc59e9..dec9441 100644 --- a/bin/modules/readMDFile.js +++ b/bin/modules/readMDFile.js @@ -3,7 +3,12 @@ const html = require("./generateHTML"); const path = require("path"); let body = ""; -module.exports.readMDFile = function (inputPath, cssLink, outputContainer) { +module.exports.readMDFile = function ( + inputPath, + cssLink, + language, + outputContainer +) { try { const data = fs.readFileSync(inputPath, "utf8"); body = data @@ -26,6 +31,6 @@ module.exports.readMDFile = function (inputPath, cssLink, outputContainer) { const title = path.basename(inputPath, ".md"); - html.generateHTML(title, cssLink, body, outputContainer); + html.generateHTML(language, title, cssLink, body, outputContainer); return title; };