Skip to content

Commit

Permalink
Moving stuff to utils.js
Browse files Browse the repository at this point in the history
Changed website generation feedback message

Removed unused variables in run()

Changed error message in readConfig()

Removed unused arguments

Improved readme formatting
  • Loading branch information
sirinoks committed Oct 18, 2021
1 parent 8d5ff17 commit 9e70243
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 436 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
/Sherlock Holmes Selected Stories/
z
dist
dist
z.txt
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ node index.js -argument --argument input
```
To get quick instructions on the arguments do:

`-h` or `--help`
`-h` or `--help`


To check the version of the app:

`-v` or `--version`



To change language of the page (it's set as `en` by default. It will be put in as an attribute to `<html>`, such as `<html lang="en">` ):

`-l` or `--lang`



To change the default directory from where the files would be read or the specific file:

`-i` or `--input`
Expand All @@ -60,7 +58,7 @@ To specify a config file to read from:

`-c` or `--config`

\

Example of usage:
```
node index.js -i cats
Expand Down
158 changes: 70 additions & 88 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
const fs = require ('fs')
const fs = require('fs')
const path = require('path')
const { exit } = require('process')
const { name, version } = require('../SSGNode/package.json')
const myUtils = require("./utils")

let sourcePath = "./Sherlock Holmes Selected Stories/The Adventure of the Six Napoleans.txt"
let endPath = "./dist"
let lang = "en"
let config = ""

function run() {
let texts = "";
let title = "";
let html = "";
if(fs.lstatSync(sourcePath).isDirectory()) {
if (fs.lstatSync(sourcePath).isDirectory()) {
try {
fs.readdir(sourcePath, (err, files)=> {
if (err) {
throw err;
}
files.forEach(function(file){
if(file.match(".*(\.txt|\.md)$")){
console.log(file);
console.log("File mathched:");
console.log(file);
let contentArray = readFile(`${sourcePath}/${file}`)
pageGenerator(contentArray)
fs.readdir(sourcePath, (err, files) => {
if (err) {
throw err;
}
files.forEach(function (file) {
if (file.match(".*(\.txt|\.md)$")) {
console.log(file);
console.log("File mathched:");
console.log(file);
let contentArray = readFile(`${sourcePath}/${file}`)
pageGenerator(contentArray)
}
})
})
})
} catch(err) {
console.log(`Error when reading a directory in ${sourcePath}.`);
console.log(err);
exit(-1);
}
} catch (err) {
console.log(`Error when reading a directory in ${sourcePath}.`);
console.log(err);
exit(-1);
}
} else {
try {
let contentArray = readFile(sourcePath);
Expand All @@ -43,30 +41,30 @@ function run() {
exit(-1);
}
}
console.log("Website generated");
console.log(`Website generated from ${sourcePath}`);
exit(0);
}

function readFile(file) {
if(file.match(".*(\.txt|\.md)$")){
if (file.match(".*(\.txt|\.md)$")) {
//read the file
let fullText = fs.readFileSync(file, 'utf8');
//formatting if it's an .md file
if(file.match(".*(\.md)$")){
//replacing strings
fullText = fullText.replace(/_ /g, "<i>")
fullText = fullText.replace(/ _/g, "</i>")
fullText = fullText.replace(/__ /g, "<b>")
fullText = fullText.replace(/ __/g, "</b>")
fullText = fullText.replace(/### /g, "<h3>")
fullText = fullText.replace(/ ###/g, "</h3>")
fullText = fullText.replace(/## /g, "<h2>")
fullText = fullText.replace(/ ##/g, "</h2>")
fullText = fullText.replace(/# /g, "<h1>")
fullText = fullText.replace(/ #/g, "</h1>")
//formatting if it's an .md file
if (file.match(".*(\.md)$")) {
//replacing strings
fullText = fullText.replace(/_ /g, "<i>")
fullText = fullText.replace(/ _/g, "</i>")
fullText = fullText.replace(/__ /g, "<b>")
fullText = fullText.replace(/ __/g, "</b>")
fullText = fullText.replace(/### /g, "<h3>")
fullText = fullText.replace(/ ###/g, "</h3>")
fullText = fullText.replace(/## /g, "<h2>")
fullText = fullText.replace(/ ##/g, "</h2>")
fullText = fullText.replace(/# /g, "<h1>")
fullText = fullText.replace(/ #/g, "</h1>")

fullText = fullText.replace(/---/g, "<hr>")
}
fullText = fullText.replace(/---/g, "<hr>")
}
//future functionality of choosing the element you want to use
let element = "p";

Expand All @@ -77,8 +75,8 @@ function readFile(file) {
let htmlParagraphsArray = [];

//put them all into an array
for (let i=0;i<paragraphs.length;i++){
if (i==0)//only the first paragraph is the title
for (let i = 0; i < paragraphs.length; i++) {
if (i == 0)//only the first paragraph is the title
htmlParagraphsArray.push(`<h1>${title}</h1>`);
else {
htmlParagraphsArray.push(`<${element}>${paragraphs[i]}</${element}>`);
Expand All @@ -87,7 +85,7 @@ function readFile(file) {

//put them all into a single string, every paragraph starts from a new line
texts = htmlParagraphsArray.join('\n');
return {"texts": texts, "title": title};
return { "texts": texts, "title": title };
}
}

Expand Down Expand Up @@ -124,34 +122,17 @@ function genPage(texts, title) {
}

function output(html) {
mkExist(endPath);
myUtils.mkExist(endPath);
deleteFiles(endPath);
generateFile(html);
genCss(endPath);
}

//if folder/file doesn't exist, create it
function mkExist(toCreate, ifFolder=true) {
try{
if(!fs.existsSync(toCreate)) {
if(ifFolder) {
fs.mkdirSync(toCreate);
} else {
fs.writeFileSync(toCreate, "utf8");
}
}
} catch (err) {
console.log(`Error when creating a ${ifFolder?"folder":"file"} ${toCreate}.`);
console.log(err);
exit(-1);
}
}

//to remove all previous files
function deleteFiles(folder) {
try{
fs.rmSync(folder, {recursive:true}, err => {
if (err) throw {err};
try {
fs.rmSync(folder, { recursive: true }, err => {
if (err) throw { err };
});
} catch (err) {
console.log(`Error when deleting files in ${folder}.`);
Expand All @@ -161,12 +142,12 @@ function deleteFiles(folder) {
}

function generateFile(html) {
mkExist(endPath);
myUtils.mkExist(endPath);

try{
fs.writeFileSync(`${endPath}/output.html`, html, function(err) {
if(err) {
return console.log(err);
try {
fs.writeFileSync(`${endPath}/output.html`, html, function (err) {
if (err) {
return console.log(err);
}
console.log("web page generated");
});
Expand All @@ -177,36 +158,37 @@ function generateFile(html) {
}
}

function configReader(){
function configReader() {
try {
//parse the file and check for the arguments.
const configJSON = JSON.parse(fs.readFileSync(config, 'utf8'));
if(configJSON.lang !== "" && configJSON.lang !== undefined){
if (configJSON.lang) {
lang = configJSON.lang;
}

//validate the input for a file/folder else throws an error and close to program.
if(fs.lstatSync(configJSON.input).isDirectory() || fs.lstatSync(configJSON.input).isFile()){
if (fs.lstatSync(configJSON.input).isDirectory() || fs.lstatSync(configJSON.input).isFile()) {
sourcePath = configJSON.input;
}

//Output folder else default to ./dist
if(configJSON.output !== "" && configJSON.output !== undefined){
if (configJSON.output) {
endPath = configJSON.output;
}
} catch (error) {
console.log(`ERROR: ${error}`);
console.log("Error in reading a config file!");
console.log(error);
exit(-1);
}
}

function genCss(dir) {
mkExist(dir);
mkExist(`${dir}/styles.css`, false);
myUtils.mkExist(dir);
myUtils.mkExist(`${dir}/styles.css`, false);

try{
try {
fs.copyFileSync("./styles.css", `${dir}/styles.css`);
} catch(err) {
} catch (err) {
console.log(`Error when copying a css file to ${dir}.`);
console.log(err);
exit(-1);
Expand All @@ -221,34 +203,34 @@ let pathchanged = false;
let langchanged = false;
let configchanged = false;

process.argv.forEach(function (val, index, array) {
process.argv.forEach(function (val) {
//if path isn't the default one, change it for this next value
if(pathchanged){
if(!val.match("^-")) {
if (pathchanged) {
if (!val.match("^-")) {
//means this might be the directory
sourcePath = val;
console.log(`Path is now ${val}`);
}

}
if(langchanged){
if(!val.match("^-")) {
if (langchanged) {
if (!val.match("^-")) {
//means this might be the lang
lang = val;
console.log(`Lang is now ${val}`);
}
}

if (configchanged) {
if(!val.match("^-")) {
if (!val.match("^-")) {
//means this might be the config
config = val;
console.log(`Config file is now ${config}`);
configReader();
}
}

switch(val) {
switch (val) {
case "--version":
case "-v":
console.log(`${name}, version ${version}.`);
Expand All @@ -271,8 +253,8 @@ process.argv.forEach(function (val, index, array) {
console.log("Hi, nice person. Hope you have a nice time of the day. If not, maybe this will help ( ._.)_ <3");
break;
case "--input":
case "-i":
pathchanged=true;
case "-i":
pathchanged = true;
break;
case "--lang":
case "-l":
Expand Down
Loading

0 comments on commit 9e70243

Please sign in to comment.