Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for JSON formatted Config files #14

Merged
merged 4 commits into from Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Expand Up @@ -38,8 +38,9 @@ nan1-ssg [-option]
| -h, --help | Will display a help message, showing options and usage. |
| -i <filename>, --input <filename> | Gives the tool a filename to generate HTML files with. The filename can be a file or a directory. |
| -l <language>, --lang <language> | Specifies a language to generate the HTML from. |
| -c <configFile>, --config <configFile> | Add a JSON config file to specify options. Omit other options when using this option. |

The hello.txt file, markdownTest.md file, and Sherlock-Holmes-Selected-Stories directory are provided for testing purposes.
The hello.txt file, markdownTest.md file, ssg-config.json file and Sherlock-Holmes-Selected-Stories directory are provided for testing purposes.

## Examples

Expand Down Expand Up @@ -73,10 +74,23 @@ nan1-ssg -i "./Sherlock-Holmes-Selected-Stories/Silver Blaze.txt"
nan1-ssg -i "file with spaces.txt"
```

**Using a configuration JSON file:**
```
nan1-ssg -c ./ssg-config.json
```

## Features

- Generating valid HTML5 files from .txt and .md files and placed in the dist directory
- An index.html file is created which contain relative links to the generated HTML files
- Each HTML file uses a default stylesheet to improve beauty and readability
- Can specify language to HTML file to use
- Horizontal rules are translated from Markdown files
- A configuration JSON file can be used to specify all options
- Example file format:
```json
{
"input": "./ssg-config.json",
"lang": "fr"
}
```
32 changes: 31 additions & 1 deletion main.js
@@ -1,11 +1,13 @@
#! /usr/bin/env node
import { generateHTML } from './generateHTML.js';
import { program } from 'commander';
import fs, { read } from 'fs';

program
.option('-v, --version', 'displays the tool name and version')
.option('-i, --input <item>', 'gets input from a file or folder')
.option('-l, --lang <item>', 'specifies language to use');
.option('-l, --lang <item>', 'specifies language to use')
.option('-c, --config <item>', 'Add a JSON config file to specify options');

program.parse(process.argv);

Expand All @@ -15,6 +17,17 @@ if (program.opts().version)
console.log("version: 0.2");
}

if(program.opts().config)
{
readConfigFile(program.opts().config).then(function(configFile)
{
generateHTML(configFile.input, configFile.lang);
})
.catch(function (rej)
{
console.log(rej);
})
}
if (program.opts().input)
{
if (program.opts().lang)
Expand All @@ -28,3 +41,20 @@ if (program.opts().input)
}
}

//this function will read a JSON config file
function readConfigFile(config)
{
return new Promise(function(res, rej)
{
if(fs.existsSync(config))
{
const theFile = fs.readFileSync(config, "utf-8");
const JSONfile = JSON.parse(theFile);
res(JSONfile);
}
else
{
rej("Error: Config file does not exist.");
}
})
}
5 changes: 5 additions & 0 deletions ssg-config.json
@@ -0,0 +1,5 @@
{
"input": "Sherlock-Holmes-Selected-Stories",
"output": "./build",
"stylesheet": "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"
}