Skip to content

Commit

Permalink
Merge pull request #18 from Reza9472/issue-17
Browse files Browse the repository at this point in the history
Adding support for config file - issue 17
  • Loading branch information
dhillonks authored Oct 12, 2021
2 parents 0e1d4c8 + 6c1db59 commit caa585d
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 17 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This processes all the <code>.txt</code> and <code>.md</code> files in <code>sam
-o, --output Output directory [string] [default: "./dist"]
-s, --stylesheet CSS Stylesheet for the website [string]
-l, --lang Lang attribute for html element [string] [default: "en-CA"]
-c, --config Pass JSON file for options [string]
```
## Features
##### :white_check_mark: Parse title from the input files
Expand All @@ -48,6 +49,8 @@ This processes all the <code>.txt</code> and <code>.md</code> files in <code>sam
##### :white_check_mark: Ability to specify the language using a `-l` flag

##### :white_check_mark: Produces horizontal rule using markdown syntax in `.md` files
##### :white_check_mark: Pass a JSON file for the options

## Examples

### Simple Example:
Expand Down
48 changes: 44 additions & 4 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const package = require("../package");
const generateHtml = require("../generateHtml");
const fs = require("fs");
const figlet = require("figlet");
const path = require("path");
const { exit } = require("process");

const outputDir = './dist';
const defaultLang = 'en-CA';
Expand All @@ -24,7 +26,6 @@ var argv = require('yargs/yargs')(process.argv.slice(2))
description: 'Input file/folder to be processed',
type: 'string',
requiresArg: true,
required: true
},
output: {
alias: 'o',
Expand All @@ -42,24 +43,56 @@ var argv = require('yargs/yargs')(process.argv.slice(2))
description: 'Lang attribute for html element',
type: 'string',
default: defaultLang
},
config: {
alias: 'c',
description: 'Specify all of the SSG options in a JSON formatted configuration file',
type: 'string',
requiresArg: true
}
})
.check((argv) => {

//Input
if(!fs.existsSync(argv.i)){
throw new Error("Input path must be a file or directory");
if(argv.i){

if(!fs.existsSync(argv.i)){
throw new Error("Input path must be a file or directory");
}
}

//Output
if(argv.o != outputDir){
else if(argv.o != outputDir){
if(fs.existsSync(argv.o)){
if(!fs.lstatSync(argv.o).isDirectory()){
throw new Error("Output path points to a file. Output directory must be valid")
}
}
else throw new Error("Output directory must be valid");
}

//Config
else if(argv.c){

if(isJSON(process.argv[3])){

if(!fs.existsSync(argv.c)){
throw new Error("JSON file path does not exist");
}
var data = JSON.parse(fs.readFileSync(argv.c));

if(data.input) argv.i = data.input
if(data.output) argv.o = data.output
if(data.stylesheet) argv.s = data.stylesheet
if(data.lang) argv.l = data.lang

}else{
throw new Error("The passed file should be of JSON format")
}
}
else {
throw new Error("A config file(.JSON) or an input file is required");
}

return true;
})
Expand All @@ -70,3 +103,10 @@ try {
} catch (err) {
console.error(err)
}

function isJSON(stats){
if(path.extname(stats) == ".json")
return true
else
return false
}
4 changes: 2 additions & 2 deletions dist/Silver Blaze.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!doctype html>
<html lang="en">
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>Silver Blaze</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" rel="stylesheet">
<link href="undefined" rel="stylesheet">
</head>
<body>
<h1>Silver Blaze</h1>
Expand Down
33 changes: 33 additions & 0 deletions dist/Test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!doctype html>
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>h1</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="undefined" rel="stylesheet">
</head>
<body>
<h1>h1</h1>
<p>text for h1</p>
<p>more text for h1 - <a href='www.google.com'>Link to google</a></p>
<h2>h2</h2>
<p>text for h2 - link <https://www.google.com></p>
2 spaces after h2


<h3>h3</h3>
<h4>h4</h4>
<p>text for h3 and h4 - <del>striked</del></p>
3 asterisks below this
<hr>
3 hyphens
<hr>
3 underscores
<hr>
<p>last line of text for h4</p>
<h5>h5</h5>
<p>text for h5 - <em>italic</em> or <em>italic</em></p>
<h6>h6</h6>
<p>text for h6 - <strong>bold</strong> or <em>bold</em></p>
</body>
</html>
4 changes: 2 additions & 2 deletions dist/The Adventure of the Six Napoleans.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!doctype html>
<html lang="en">
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>THE ADVENTURE OF THE SIX NAPOLEONS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" rel="stylesheet">
<link href="undefined" rel="stylesheet">
</head>
<body>
<h1>THE ADVENTURE OF THE SIX NAPOLEONS</h1>
Expand Down
4 changes: 2 additions & 2 deletions dist/The Adventure of the Speckled Band.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!doctype html>
<html lang="en">
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>THE ADVENTURE OF THE SPECKLED BAND</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" rel="stylesheet">
<link href="undefined" rel="stylesheet">
</head>
<body>
<h1>THE ADVENTURE OF THE SPECKLED BAND</h1>
Expand Down
4 changes: 2 additions & 2 deletions dist/The Naval Treaty.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!doctype html>
<html lang="en">
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>The Naval Treaty</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" rel="stylesheet">
<link href="undefined" rel="stylesheet">
</head>
<body>
<h1>The Naval Treaty</h1>
Expand Down
4 changes: 2 additions & 2 deletions dist/The Red Headed League.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!doctype html>
<html lang="en">
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>The Red Headed League</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" rel="stylesheet">
<link href="undefined" rel="stylesheet">
</head>
<body>
<h1>The Red Headed League</h1>
Expand Down
6 changes: 3 additions & 3 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!doctype html>
<html lang="en">
<html lang="en-CA">
<head>
<meta charset="utf-8">
<title>Index File</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" rel="stylesheet">
<link href="undefined" rel="stylesheet">
</head>
<body>
<h1>Index File</h1>
<a href="./Silver%20Blaze.html">Silver Blaze</a><br><a href="./The%20Adventure%20of%20the%20Six%20Napoleans.html">The Adventure of the Six Napoleans</a><br><a href="./The%20Adventure%20of%20the%20Speckled%20Band.html">The Adventure of the Speckled Band</a><br><a href="./The%20Naval%20Treaty.html">The Naval Treaty</a><br><a href="./The%20Red%20Headed%20League.html">The Red Headed League</a><br>
<a href="./Silver%20Blaze.html">Silver Blaze</a><br><a href="./The%20Adventure%20of%20the%20Six%20Napoleans.html">The Adventure of the Six Napoleans</a><br><a href="./The%20Adventure%20of%20the%20Speckled%20Band.html">The Adventure of the Speckled Band</a><br><a href="./The%20Naval%20Treaty.html">The Naval Treaty</a><br><a href="./The%20Red%20Headed%20League.html">The Red Headed League</a><br><a href="./Test.html">Test</a><br>
</body>
</html>
7 changes: 7 additions & 0 deletions sample_input/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"input": "./sample_input",
"output": "./files",
"stylesheet": "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css",
"lang": "fr"

}

1 comment on commit caa585d

@vercel
Copy link

@vercel vercel bot commented on caa585d Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.