From 6f143604c28c3165db35c2cad99a0dc76d7ccdaa Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Wed, 5 May 2021 09:37:43 -0600 Subject: Add working file discovery and destination file structure creation --- src/pyssg/discovery.py | 40 ++++++++++++++++++++++ src/pyssg/file_discovery.py | 5 --- src/pyssg/file_structure.py | 76 ----------------------------------------- src/pyssg/parser.py | 24 +++++++++++++ src/pyssg/pyssg.py | 37 +++++++++++++------- src/pyssg/templates.py | 83 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 171 insertions(+), 94 deletions(-) create mode 100644 src/pyssg/discovery.py delete mode 100644 src/pyssg/file_discovery.py delete mode 100644 src/pyssg/file_structure.py create mode 100644 src/pyssg/parser.py create mode 100644 src/pyssg/templates.py (limited to 'src') 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/file_structure.py b/src/pyssg/file_structure.py deleted file mode 100644 index f349fed..0000000 --- a/src/pyssg/file_structure.py +++ /dev/null @@ -1,76 +0,0 @@ -import os - - -def create_structure(directory: str): - # get initial working directory - iwd = os.getcwd() - - # create main dir - os.mkdir(directory) - os.chdir(directory) - - # create templates dir - os.mkdir('templates') - os.chdir('templates') - - # create article (blog) barebones template - os.mkdir('article') - with open('article/header.html', 'w+') as f: - f.write('\n') - f.write('\n') - f.write('\n') - f.write('\n') - f.write('$$TITLE\n') - f.write('$$EXTRAHEAD\n') - f.write('\n') - f.write('\n') - - with open('article/footer.html', 'w+') as f: - f.write('\n') - f.write('\n') - - with open('article/index_header.html', 'w+') as f: - f.write('') - - with open('article/tag_list_header.html', 'w+') as f: - f.write('

Tags:') - - with open('article/tag_entry.html', 'w+') as f: - f.write('$$NAME') - - with open('article/tag_separator.html', 'w+') as f: - f.write(', ') - - with open('article/tag_list_footer.html', 'w+') as f: - f.write('

\n') - - with open('article/article_list_header.html', 'w+') as f: - f.write('

Articles

\n') - f.write('\n') - - with open('article/index_footer.html', 'w+') as f: - f.write('') - - with open('article/tag_index_header.html', 'w+') as f: - f.write('') - - with open('article/tag_index_footer.html', 'w+') as f: - f.write('') - - with open('article/article_header.html', 'w+') as f: - f.write('

$$TITLE

') - - with open('article/article_footer.html', 'w+') as f: - f.write('') - - # return to initial working directory - os.chdir(iwd) 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/templates.py b/src/pyssg/templates.py new file mode 100644 index 0000000..9ae2e96 --- /dev/null +++ b/src/pyssg/templates.py @@ -0,0 +1,83 @@ +import os + + +def create_templates(src: str, dst: str): + # get initial working directory + iwd = os.getcwd() + print('creating dir structure...') + + # create main dirs + try: + os.mkdir(src) + os.mkdir(dst) + except FileExistsError: + pass + + os.chdir(src) + + # create templates dir + os.mkdir('templates') + os.chdir('templates') + + # create article (blog) barebones template + os.mkdir('article') + with open('article/header.html', 'w+') as f: + f.write('\n') + f.write('\n') + f.write('\n') + f.write('\n') + f.write('$$TITLE\n') + f.write('$$EXTRAHEAD\n') + f.write('\n') + f.write('\n') + + with open('article/footer.html', 'w+') as f: + f.write('\n') + f.write('\n') + + with open('article/index_header.html', 'w+') as f: + f.write('') + + with open('article/tag_list_header.html', 'w+') as f: + f.write('

Tags:') + + with open('article/tag_entry.html', 'w+') as f: + f.write('$$NAME') + + with open('article/tag_separator.html', 'w+') as f: + f.write(', ') + + with open('article/tag_list_footer.html', 'w+') as f: + f.write('

\n') + + with open('article/article_list_header.html', 'w+') as f: + f.write('

Articles

\n') + f.write('\n') + + with open('article/index_footer.html', 'w+') as f: + f.write('') + + with open('article/tag_index_header.html', 'w+') as f: + f.write('') + + with open('article/tag_index_footer.html', 'w+') as f: + f.write('') + + with open('article/article_header.html', 'w+') as f: + f.write('

$$TITLE

') + + with open('article/article_footer.html', 'w+') as f: + f.write('') + + # return to initial working directory + os.chdir(iwd) + print('done creating dir structure...') -- cgit v1.2.3-70-g09d2