Skip to content

Commit

Permalink
custom output dir completed
Browse files Browse the repository at this point in the history
  • Loading branch information
AParris21 committed Oct 2, 2022
1 parent c3ecd42 commit 7517130
Show file tree
Hide file tree
Showing 28 changed files with 45,632 additions and 28 deletions.
15 changes: 15 additions & 0 deletions dir/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>This is a Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>This is a Test</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, <br>sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <br></p>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris <br>nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in <br>reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla <br>pariatur.<br></p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in <br>culpa qui officia deserunt mollit anim id est laborum.<br></p>

</body>
</html>
15 changes: 15 additions & 0 deletions dist/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>This is a Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>This is a Test</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, <br>sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <br></p>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris <br>nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in <br>reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla <br>pariatur.<br></p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in <br>culpa qui officia deserunt mollit anim id est laborum.<br></p>

</body>
</html>
150 changes: 129 additions & 21 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,50 @@ import fs, { stat } from 'fs';
import path from 'path';
import readline from 'readline';

const dist_path = './dist';
var output_path = dist_path;
const dist_path = "./dist";
var output_path = "";
var htmlLangAttribute = "en-CA";

export function setHtmlLang(lang) {
if (lang.length > 0)
{
var lang_ = new String(lang);
htmlLangAttribute = lang_;
}
}

function setOutputFolder(outputDir) {
var output_ = "./";

if (outputDir.length > 0)
{
var temp = new String(outputDir);
var pathStartWithDot = new RegExp('^\.\/.*');
var pathStartNoDot = new RegExp('^\/.*');

if (pathStartWithDot.test(temp)) {
output_ += temp.substring(2);
}
else if (pathStartNoDot.test(temp)) {
output_ += temp.substring(1);
}
else {
output_ = temp;
}
}
else
{
output_ = new String(dist_path);
}

output_path = output_;

makeOutputFolder(output_.valueOf());
}

export function generateWebsite(inputStr)
{
makeOutputFolder();
setOutputFolder("");

// Get the filename from the full pathname.
const fileName = path.basename(inputStr);
Expand Down Expand Up @@ -40,8 +77,6 @@ export function generateWebsite(inputStr)
});
}
else if (path.extname(oneFile) == '.md') {
console.log(".md file found");

readFileMd(inputStr + '/' + oneFile)
.then(function (data) {
writeFile(oneFile, data);
Expand Down Expand Up @@ -93,27 +128,100 @@ export function generateWebsite(inputStr)
return 0;
}

export function setHtmlLang(lang) {
if (lang.length > 0) {
var lang_ = new String(lang);
htmlLangAttribute = lang_;
}
}
export function generateWebsiteWithOutput(inputStr, outputStr)
{
setOutputFolder(outputStr);

export function setOutputFolder(output_path) {
// Get the filename from the full pathname.
const fileName = path.basename(inputStr);

// Check whether the filepath is a single file or a folder.
fs.lstat(inputStr, (err, stats) => {
if (err) {
console.log(err);
return -1;
}
else {
// If a folder was specified, parse each file individually.
if (stats.isDirectory()) {
fs.readdir(inputStr, (err, files) => {

files.forEach((oneFile) => {
if (err) {
console.log(err);
return -1;
}
else if (path.extname(oneFile) == '.txt') {
readFileTxt(inputStr + '/' + oneFile)
.then(function (data) {
writeFile(oneFile, data);
})
.catch(function (err) {
console.log(err);
return -1;
});
}
else if (path.extname(oneFile) == '.md') {
readFileMd(inputStr + '/' + oneFile)
.then(function (data) {
writeFile(oneFile, data);
}).catch(function (err) {
console.log(err);
return -1;
});
}
else {
console.log("Invalid file type. Cannot parse.");
}
});

generateIndexHtmlFile(files, true);
});
}
// Otherwise, if the filepath was a single file then parse it.
else {
if (path.extname(fileName) == '.txt') {

readFileTxt(inputStr)
.then((data) => {
writeFile(fileName, data);
})
.catch(function (err) {
console.log(err);
return -1;
});
}
else if (path.extname(fileName) == '.md') {
readFileMd(inputStr)
.then(function (data){
writeFile(fileName, data);
})
.catch(function (err) {
console.log(err);
return -1;
});
}
else {
console.log("Invalid file type. Cannot parse.");
}
}
} // end else no errors
});

return 0;
}

function makeOutputFolder() {
function makeOutputFolder(filepath) {
// If the /dist directory exists, remove the folder and all of its contents
if (fs.existsSync(dist_path)) {
fs.rmSync(dist_path, { recursive: true, force: true });
if (fs.existsSync(filepath)) {
fs.rmSync(filepath, { recursive: true, force: true });
}

// Create a new /dist directory, if it does not already exist.
/* Note: This IF statement may be redundant, since the /dist folder should be deleted
by the above code block before execution reaches this line. */
if (!fs.existsSync(dist_path)) {
fs.mkdirSync(dist_path);
if (!fs.existsSync(filepath)) {
fs.mkdirSync(filepath);
}
}

Expand All @@ -134,7 +242,7 @@ function readFileTxt(filePath) {
// Accepts the contents of a file as a string literal. Creates an HTML file containing the content.
function writeFile(fileName, dataArr) {
return new Promise( (res, rej) => {
var htmlFilePath = dist_path + "/" + getFileNameNoExt(fileName) + '.html';
var htmlFilePath = output_path + "/" + getFileNameNoExt(fileName) + '.html';

var myBuffer = ""; // Buffer to hold lines read from the file.
var title = "";
Expand Down Expand Up @@ -203,7 +311,7 @@ function writeFile(fileName, dataArr) {

// Generates the index.html file.
function generateIndexHtmlFile(filenames) {
const indexFilePath = dist_path + "/index.html";
const indexFilePath = output_path + "/index.html";
const indexTitle = "AP21 SSG";

// Generate the head and the beginning of the body elements for the index.html file.
Expand All @@ -221,7 +329,7 @@ function generateIndexHtmlFile(filenames) {
for (let i=0; i < filenames.length; i++) {
htmlStr +=
`<li><ul>
<a href="${dist_path}/${filenames[i]}.html">${filenames[i]}</a>
<a href="${output_path}/${filenames[i]}.html">${filenames[i]}</a>
</ul></li>`;
}

Expand Down Expand Up @@ -293,7 +401,7 @@ function getFileNameNoExt(fileName) {
function parseMarkdown(mdStr) {
const mdBold = /\*\*(.*)\*\*/gim; // reg expression for bold syntax
const mdInlineCode = /\`(.*)\`/gim; // reg exp for in-line code syntax
const mdHr = /^(\s*)\-\-\-(\s*$)/; // reg exp for horizontal rule
const mdHr = /^(\s*)\-\-\-(\s*$)/gm; // reg exp for horizontal rule

var htmlText = mdStr.replace(mdBold, "<b>$1</b>")
.replace(mdInlineCode, "<code>$1</code>")
Expand Down
14 changes: 7 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// #! /usr/bin/env node

import { generateWebsite, setHtmlLang, setOutputFolder } from './helpers.js';
import { generateWebsite, setHtmlLang, generateWebsiteWithOutput } from './helpers.js';
import { Command } from 'commander';
import { createRequire } from "module";
const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -30,19 +30,19 @@ if (options.help)

if (options.input)
{
generateWebsite(`${options.input}`);
if (options.output) {
generateWebsiteWithOutput(`${options.input}`, `${options.output}`);
}
else {
generateWebsite(`${options.input}`);
}
}

if (options.lang)
{
setHtmlLang(`${options.lang}`);
}

if (options.output)
{
setOutputFolder(`${options.output}`);
}

function generateHelpMessage()
{
let helpMsg = `----------------------------------------------------------------------\n`;
Expand Down
21 changes: 21 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions node_modules/commander/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7517130

Please sign in to comment.