Skip to content

Commit

Permalink
Add optional specifying language feature
Browse files Browse the repository at this point in the history
  • Loading branch information
hlavu committed Sep 28, 2021
1 parent d1a7bc8 commit 8a6bbec
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
20 changes: 16 additions & 4 deletions bin/index.js
Expand Up @@ -12,7 +12,7 @@ const htmlContainer = "./dist";
let cssLink = "";

const argv = require("yargs")
.usage("Usage: $0 --input <filename> [-s <css-link>]")
.usage("Usage: $0 --input <filename> [-s <css-link>] [-l <lang-code>]")
.option("i", {
alias: "input",
describe: ".txt file name",
Expand All @@ -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")
Expand All @@ -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");
}
Expand Down
11 changes: 9 additions & 2 deletions bin/modules/generateHTML.js
Expand Up @@ -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: `<meta name="viewport" content="width=device-width, initial-scale=1">`,
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") {
Expand Down
9 changes: 7 additions & 2 deletions bin/modules/readFile.js
Expand Up @@ -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
Expand All @@ -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;
};
10 changes: 9 additions & 1 deletion bin/modules/readFolder.js
Expand Up @@ -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);
Expand All @@ -19,6 +24,7 @@ module.exports.readFolder = function (inputPath, cssLink, outputContainer) {
const fileName = fileModule.readFile(
`${inputPath}/${file}`,
cssLink,
language,
outputContainer
);

Expand All @@ -37,6 +43,7 @@ module.exports.readFolder = function (inputPath, cssLink, outputContainer) {
const fileName = readMDFile(
`${inputPath}/${file}`,
cssLink,
language,
outputContainer
);

Expand All @@ -48,6 +55,7 @@ module.exports.readFolder = function (inputPath, cssLink, outputContainer) {

// create index.html
html.generateHTML(
language,
"index",
cssLink,
`<h4>Generated Sites</h4>\n${body}`,
Expand Down
9 changes: 7 additions & 2 deletions bin/modules/readMDFile.js
Expand Up @@ -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
Expand All @@ -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;
};

0 comments on commit 8a6bbec

Please sign in to comment.