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

New feature: config-file #14

Merged
merged 1 commit into from
Oct 9, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pip install beautifulsoup4
| ------------ | -------- |
|Generate basic HTML file(s)|`python3 ssg.py --input "relative path or absolute path to the file or folder"`|
|Generate basic HTML file(s) with CSS stylesheet | `python3 ssg.py --input "relative path or absolute path to the file or folder" --stylesheet "URL to CSS stylesheet"`|
|New Feature: Support arguments passed with Config File | `python3 ssg.py -c ./ssg-config.json` |
|Help | `python3 ssg.py --help` |
|Get current version | `python3 ssg.py --version`|

Expand All @@ -38,6 +39,7 @@ pip install beautifulsoup4
| `--help` | `--h` |
| `--version` | `--v` |
| `--stylesheet` | `--s`|
| `--config` | `--c`|


## Markdown
Expand Down
4 changes: 4 additions & 0 deletions ssg-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"input": "requirements.txt",
"stylesheet": "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"
}
38 changes: 33 additions & 5 deletions ssg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from bs4 import BeautifulSoup
from io import open
import platform
import json

OUTPUT_DIR = "dist"

Expand Down Expand Up @@ -279,17 +280,44 @@ def cla_parser():
# Creating argparser object
# https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser
parser = argparse.ArgumentParser(description="Static Site Generator - is a tool to generate HTML files from raw data like txt files.")
parser.add_argument('-i','--input', type=str,required=True,metavar='',help='path to the file or folder that needs to be processed')
parser.add_argument('-i','--input', type=str, metavar='',help='path to the file or folder that needs to be processed')
# --version -v argument
parser.add_argument("-v", "--version", action="version", version="Static Site Generator 0.1", help="show program's version number and exit")
# --stylesheet -s argument
parser.add_argument("-s", "--stylesheet", metavar='',help="URL stylesheet to be used in generated HTML files")
# --config -c argument
parser.add_argument("-c", "--config", metavar='', help="Users want to be able to specify all of their SSG options in a JSON formatted configuration file instead of having to pass them all as command line arguments every time")
# Parse the command line arguments
args = parser.parse_args()
parsed_args = {
'input': args.input,
'stylesheet': args.stylesheet if args.stylesheet else None
}
# if argument passed is config
if args.config:
with open(args.config) as f:
try:
stored_data = json.load(f)
# print(stored_data)
if len(stored_data) == 0:
print("JSON file not found:(\n Please update!\n")
exit(1)
except:
print("\nError in reading Config File")
exit(1)
for value in stored_data:
if value == "input" or value == "i":input = stored_data[value]
elif value == "stylesheet" or value == "s":stylesheet = stored_data[value]
if input == None:
print("No input file specified")
exit(1)
# parsing the arguments from config JSON file
parsed_args = {
'input': input if input else None,
'stylesheet': stylesheet if stylesheet else None,
'config': args.config
}
elif args.input:
parsed_args = {
'input': args.input,
'stylesheet': args.stylesheet if args.stylesheet else None,
}
return parsed_args


Expand Down