Skip to content

Commit

Permalink
Merge pull request #10 from cychu42/issue-8
Browse files Browse the repository at this point in the history
Add initial support for markdown files with italicization parser
  • Loading branch information
alexsam29 authored Sep 23, 2022
2 parents a29e8a4 + 733e86f commit ce60fd9
Show file tree
Hide file tree
Showing 5 changed files with 2,359 additions and 11 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A command-line interface Static Site Generator (SSG) used for generating a complete HTML web site from raw data and files.

## Overview
This tool allows a user to input a text file path or a directory path and an HTML file will be created for the specified file or for every text file in the specified directory.
This tool allows a user to input a text or markdown file path or a directory path and an HTML file will be created for the specified file or for every text/markdown file in the specified directory.
## Requirements
A recent version of [Node.js](https://nodejs.org/en/) must be downloaded and installed.

Expand All @@ -14,7 +14,7 @@
```
ssg -i file_path
```
5. If a file was specified, an HTML file will be created and added into the ```dist``` directory. If a directory was specified, HTML files will be created for every text file in the directory and they will be added into the ```dist``` directory.
5. If a file was specified, an HTML file will be created and added into the ```dist``` directory. If a directory was specified, HTML files will be created for every text/markdown file in the directory and they will be added into the ```dist``` directory.
6. [OPTIONAL] Specify a directory to output the HTML files to by adding the path as a second argument. If the directory does not exist, a new one will be created. If it does exist, the current contents will be deleted and only the HTML files will be added.
```
ssg -i file_path -o directoryPath
Expand All @@ -29,15 +29,18 @@
|-o, --output| Specify a different output directory (any existing contents in the directory will be <b>DELETED</b>)| [string]|

## Examples
### Generate HTML file based off a text file
### Generate HTML file based off a text or markdown file
```
ssg -i file_path
```
Add ```" "``` for files with spaces.
```
ssg -i ".\testFiles\Silver Blaze.txt"
```
### Generate HTML files based off of text files within a directory
```
ssg -i ".\testFiles\input MD.md"
```
### Generate HTML files based off of text or markdown files within a directory
```
ssg -i .\testFiles
```
Expand All @@ -57,6 +60,7 @@
ssg -i .\testFiles -o .\anotherFolder
```
## Optional Features Implemented
- Title parsed from text files. It will populate the ```<title>``` tag and add a ```<h1>``` tag to the top of the body.
- Title parsed from text and markdown files. It will populate the ```<title>``` tag and add a ```<h1>``` tag to the top of the body.
- Allow the user to specify a different output directory using --output or -o. If not specified, dist will be used, but if the user specifies a different output path, it will use that. If the directory does not exist, a new directory will be created.
- If the user specifies a folder for the input, it will automatically generate an index.html file, which has relative links to each of the generated HTML files.
- Italicized text parsed from markdown files. It will add a ```<i>``` tag to any italicized text.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</head>
<body>
<ul>
<li><a href=".\dist\Silver Blaze.html">Silver Blaze</a></li><li><a href=".\dist\THE ADVENTURE OF THE SIX NAPOLEONS.html">THE ADVENTURE OF THE SIX NAPOLEONS</a></li><li><a href=".\dist\THE ADVENTURE OF THE SPECKLED BAND.html">THE ADVENTURE OF THE SPECKLED BAND</a></li><li><a href=".\dist\The Naval Treaty.html">The Naval Treaty</a></li><li><a href=".\dist\The Red Headed League.html">The Red Headed League</a></li></ul>
<li><a href=".\dist\input MD.html">input MD</a></li><li><a href=".\dist\inputTXT.html">inputTXT</a></li><li><a href=".\dist\jjj.html">jjj</a></li><li><a href=".\dist\Silver Blaze.html">Silver Blaze</a></li><li><a href=".\dist\THE ADVENTURE OF THE SIX NAPOLEONS.html">THE ADVENTURE OF THE SIX NAPOLEONS</a></li><li><a href=".\dist\THE ADVENTURE OF THE SPECKLED BAND.html">THE ADVENTURE OF THE SPECKLED BAND</a></li><li><a href=".\dist\The Naval Treaty.html">The Naval Treaty</a></li><li><a href=".\dist\The Red Headed League.html">The Red Headed League</a></li></ul>

</body>
</html>
36 changes: 31 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports.main = function main() {
var inputPath = `Path of file or folder: ${options.input}`;
var isDirectory = false;
var isFile = false;
var isMd = false;

// Determine if input is a valid file or directory
try {
Expand Down Expand Up @@ -77,18 +78,28 @@ module.exports.main = function main() {
var filePath = options.input + "\\" + filename;
if (fs.lstatSync(filePath).isFile()) {
var content = fs.readFileSync(filePath, "utf-8");
if (path.extname(filename) == ".txt") {
if (path.extname(filename) == ".txt" || path.extname(filename) == ".md" ) { // Check if file extension is txt or md
if (path.extname(filename) == ".md") { // Check and flag for .md files
isMd=true;
}else{
isMd=false;
}
HTMLcreate(filename.split(".")[0], content);
}
}
});
indexCreate(dir);
} else if (isFile) {
// If input file is not a txt file, output error msg
if (path.extname(options.input) != ".txt") {
console.log(chalk.red.bold("Please select a text file."));
if (path.extname(options.input) != ".txt" && path.extname(options.input) != ".md") { //Check if file extension is .txt or .md
console.log(chalk.red.bold("Please select a text or markdown file."));
return;
}

if (path.extname(options.input) == ".md"){ // Check and flag for .md files
isMd=true;
}

// Create HTML for single txt file
var data = fs.readFileSync(options.input, "utf-8");
HTMLcreate(options.input.split("\\").slice(-1)[0].split(".")[0], data);
Expand All @@ -100,9 +111,24 @@ module.exports.main = function main() {
var title = filename;
var body = content.split("\n\r");
var newBody = "<h1>" + title + "</h1>";


// Append rest of the body after the title
body.forEach(function (line, index) {
body.forEach(function (line, index) {

// If it's .md file, check each line; while any *text* and _text_ exist, repalce them with <i></i>
if (isMd==true) {
while (line.match(/_[—!@#$%^&()+;/<>.\s\w\d,?"“”-]+_/)) {
line=line.replace("_","<i>");
line=line.replace("_","</i>");
}

while (line.match(/\*[—!@#$%^&()+;/<>.\s\w\d,?"“”-]+\*/)) {
line=line.replace("*","<i>");
line=line.replace("*","</i>");
}
}

if (index != 0) {
newBody += "<p>" + line + "</p>";
}
Expand All @@ -112,7 +138,7 @@ module.exports.main = function main() {
title: title,
body: newBody,
});

// Write to HTML file
fs.writeFileSync(`${dir + "\\" + title}.html`, html);
console.log(
Expand Down
Loading

0 comments on commit ce60fd9

Please sign in to comment.