diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2021-05-05 09:37:43 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2021-05-05 09:37:43 -0600 |
commit | 6f143604c28c3165db35c2cad99a0dc76d7ccdaa (patch) | |
tree | addf3517732d753bde1c258574318bac51b6ca24 /src | |
parent | 43ed04c6740a3ac11f7e2fc6d75429951536286e (diff) |
Add working file discovery and destination file structure creation
Diffstat (limited to 'src')
-rw-r--r-- | src/pyssg/discovery.py | 40 | ||||
-rw-r--r-- | src/pyssg/file_discovery.py | 5 | ||||
-rw-r--r-- | src/pyssg/parser.py | 24 | ||||
-rw-r--r-- | src/pyssg/pyssg.py | 37 | ||||
-rw-r--r-- | src/pyssg/templates.py (renamed from src/pyssg/file_structure.py) | 15 |
5 files changed, 99 insertions, 22 deletions
diff --git a/src/pyssg/discovery.py b/src/pyssg/discovery.py new file mode 100644 index 0000000..39c8bb1 --- /dev/null +++ b/src/pyssg/discovery.py @@ -0,0 +1,40 @@ +import os + + +def get_file_list(extensions: list[str], exclude: list[str]=None) -> list[str]: + cwd = os.getcwd() + + out = [] + for root, dirs, files in os.walk(cwd): + if exclude is not None: + dirs[:] = [d for d in dirs if d not in exclude] + + for f in files: + if f.endswith(tuple(extensions)): + out.append(os.path.join(root, f)) + + return out + + +def get_dir_structure(exclude: list[str]=None) -> list[str]: + cwd = os.getcwd() + + out = [] + for root, dirs, files in os.walk(cwd): + if exclude is not None: + dirs[:] = [d for d in dirs if d not in exclude] + + for d in dirs: + if root in out: + out.remove(root) + out.append(os.path.join(root, d).replace(cwd, '')) + + return out + + +def get_all_files(): + md_files = get_file_list(['.md', '.markdown'], ['templates']) + html_files = get_file_list(['.html'], ['templates']) + dirs = get_dir_structure(['templates']) + + return (dirs, md_files, html_files) diff --git a/src/pyssg/file_discovery.py b/src/pyssg/file_discovery.py deleted file mode 100644 index 9b3a57d..0000000 --- a/src/pyssg/file_discovery.py +++ /dev/null @@ -1,5 +0,0 @@ -import os - - -def get_md_files(directory: str) -> list: - return os.listdir(directory) diff --git a/src/pyssg/parser.py b/src/pyssg/parser.py new file mode 100644 index 0000000..8b8d108 --- /dev/null +++ b/src/pyssg/parser.py @@ -0,0 +1,24 @@ +import os +from .discovery import get_all_files + + +def create_dir_structure(dirs: list[str]): + cwd = os.getcwd() + + for d in dirs: + try: + os.makedirs(os.path.join(cwd, d[1:])) + except FileExistsError: + pass + + +def generate_static_site(src: str, dst: str): + iwd = os.getcwd() + + os.chdir(src) + dirs, md_files, html_files = get_all_files() + os.chdir(iwd) + + os.chdir(dst) + create_dir_structure(dirs) + os.chdir(iwd) diff --git a/src/pyssg/pyssg.py b/src/pyssg/pyssg.py index 6d0df3a..7378705 100644 --- a/src/pyssg/pyssg.py +++ b/src/pyssg/pyssg.py @@ -1,34 +1,45 @@ import os from argparse import ArgumentParser, Namespace -from .file_discovery import get_md_files -from .file_structure import create_structure +from .templates import create_templates +from .parser import generate_static_site def get_options() -> Namespace: parser = ArgumentParser(prog='pyssg', description='''Static Site Generator that reads Markdown files and creates HTML files.''') - parser.add_argument('-d', '--directory', - default='.', + parser.add_argument('-s', '--src', + default='src', type=str, - help='''root directory for all site files, - defaults to "." (cwd), uses relative or absolute - resolution''') + help='''src directory; handmade files, templates and + metadata directory; defaults to 'src' ''') + parser.add_argument('-d', '--dst', + default='dst', + type=str, + help='''dst directory; generated (and transfered html) + files; defaults to 'dst' ''') parser.add_argument('-i', '--init', action='store_true', - help='''initialize the directory structure where -d - specifies''') + help='''initializes the dir structure, templates, + as well as the 'src' and 'dst' directories''') + parser.add_argument('-b', '--build', + action='store_true', + help='''generates all html files and passes over + existing (handmade) ones''') return parser.parse_args() def main(): opts = vars(get_options()) - directory = opts['directory'] + src = opts['src'] + dst = opts['dst'] if opts['init']: - create_structure(directory) + create_templates(src, dst) + return - os.chdir(directory) - root_dir = os.getcwd() + if opts['build']: + generate_static_site(src, dst) + return diff --git a/src/pyssg/file_structure.py b/src/pyssg/templates.py index f349fed..9ae2e96 100644 --- a/src/pyssg/file_structure.py +++ b/src/pyssg/templates.py @@ -1,13 +1,19 @@ import os -def create_structure(directory: str): +def create_templates(src: str, dst: str): # get initial working directory iwd = os.getcwd() + print('creating dir structure...') - # create main dir - os.mkdir(directory) - os.chdir(directory) + # create main dirs + try: + os.mkdir(src) + os.mkdir(dst) + except FileExistsError: + pass + + os.chdir(src) # create templates dir os.mkdir('templates') @@ -74,3 +80,4 @@ def create_structure(directory: str): # return to initial working directory os.chdir(iwd) + print('done creating dir structure...') |