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 | |
parent | 43ed04c6740a3ac11f7e2fc6d75429951536286e (diff) |
Add working file discovery and destination file structure creation
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | example_src/a/test3.md (renamed from example/templates/article/article_footer.html) | 0 | ||||
-rw-r--r-- | example_src/a/test4.md (renamed from example/templates/article/article_separator.html) | 0 | ||||
-rw-r--r-- | example_src/a/test6.html (renamed from example/templates/article/index_footer.html) | 0 | ||||
-rw-r--r-- | example_src/b/c/test8.html (renamed from example/templates/article/index_header.html) | 0 | ||||
-rw-r--r-- | example_src/b/c/test9.md (renamed from example/templates/article/tag_index_footer.html) | 0 | ||||
-rw-r--r-- | example_src/b/test7.html (renamed from example/templates/article/tag_index_header.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/article_entry.html (renamed from example/templates/article/article_entry.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/article_footer.html | 0 | ||||
-rw-r--r-- | example_src/templates/article/article_header.html (renamed from example/templates/article/article_header.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/article_list_footer.html (renamed from example/templates/article/article_list_footer.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/article_list_header.html (renamed from example/templates/article/article_list_header.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/article_separator.html | 0 | ||||
-rw-r--r-- | example_src/templates/article/footer.html (renamed from example/templates/article/footer.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/header.html (renamed from example/templates/article/header.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/index_footer.html | 0 | ||||
-rw-r--r-- | example_src/templates/article/index_header.html | 0 | ||||
-rw-r--r-- | example_src/templates/article/tag_entry.html (renamed from example/templates/article/tag_entry.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/tag_index_footer.html | 0 | ||||
-rw-r--r-- | example_src/templates/article/tag_index_header.html | 0 | ||||
-rw-r--r-- | example_src/templates/article/tag_list_footer.html (renamed from example/templates/article/tag_list_footer.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/tag_list_header.html (renamed from example/templates/article/tag_list_header.html) | 0 | ||||
-rw-r--r-- | example_src/templates/article/tag_separator.html (renamed from example/templates/article/tag_separator.html) | 0 | ||||
-rw-r--r-- | example_src/test.md | 0 | ||||
-rw-r--r-- | example_src/test2.md | 0 | ||||
-rw-r--r-- | example_src/test5.html | 0 | ||||
-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 |
31 files changed, 100 insertions, 22 deletions
@@ -1,6 +1,7 @@ CHANGES ======= +* initial template creation, barebones arg parser * Prepare barebones package info * Update readme * Initial commit diff --git a/example/templates/article/article_footer.html b/example_src/a/test3.md index e69de29..e69de29 100644 --- a/example/templates/article/article_footer.html +++ b/example_src/a/test3.md diff --git a/example/templates/article/article_separator.html b/example_src/a/test4.md index e69de29..e69de29 100644 --- a/example/templates/article/article_separator.html +++ b/example_src/a/test4.md diff --git a/example/templates/article/index_footer.html b/example_src/a/test6.html index e69de29..e69de29 100644 --- a/example/templates/article/index_footer.html +++ b/example_src/a/test6.html diff --git a/example/templates/article/index_header.html b/example_src/b/c/test8.html index e69de29..e69de29 100644 --- a/example/templates/article/index_header.html +++ b/example_src/b/c/test8.html diff --git a/example/templates/article/tag_index_footer.html b/example_src/b/c/test9.md index e69de29..e69de29 100644 --- a/example/templates/article/tag_index_footer.html +++ b/example_src/b/c/test9.md diff --git a/example/templates/article/tag_index_header.html b/example_src/b/test7.html index e69de29..e69de29 100644 --- a/example/templates/article/tag_index_header.html +++ b/example_src/b/test7.html diff --git a/example/templates/article/article_entry.html b/example_src/templates/article/article_entry.html index 6e6befd..6e6befd 100644 --- a/example/templates/article/article_entry.html +++ b/example_src/templates/article/article_entry.html diff --git a/example_src/templates/article/article_footer.html b/example_src/templates/article/article_footer.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/templates/article/article_footer.html diff --git a/example/templates/article/article_header.html b/example_src/templates/article/article_header.html index d2954d9..d2954d9 100644 --- a/example/templates/article/article_header.html +++ b/example_src/templates/article/article_header.html diff --git a/example/templates/article/article_list_footer.html b/example_src/templates/article/article_list_footer.html index 3d3a44c..3d3a44c 100644 --- a/example/templates/article/article_list_footer.html +++ b/example_src/templates/article/article_list_footer.html diff --git a/example/templates/article/article_list_header.html b/example_src/templates/article/article_list_header.html index 7b504ea..7b504ea 100644 --- a/example/templates/article/article_list_header.html +++ b/example_src/templates/article/article_list_header.html diff --git a/example_src/templates/article/article_separator.html b/example_src/templates/article/article_separator.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/templates/article/article_separator.html diff --git a/example/templates/article/footer.html b/example_src/templates/article/footer.html index 308b1d0..308b1d0 100644 --- a/example/templates/article/footer.html +++ b/example_src/templates/article/footer.html diff --git a/example/templates/article/header.html b/example_src/templates/article/header.html index 48adfef..48adfef 100644 --- a/example/templates/article/header.html +++ b/example_src/templates/article/header.html diff --git a/example_src/templates/article/index_footer.html b/example_src/templates/article/index_footer.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/templates/article/index_footer.html diff --git a/example_src/templates/article/index_header.html b/example_src/templates/article/index_header.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/templates/article/index_header.html diff --git a/example/templates/article/tag_entry.html b/example_src/templates/article/tag_entry.html index 3a7aaad..3a7aaad 100644 --- a/example/templates/article/tag_entry.html +++ b/example_src/templates/article/tag_entry.html diff --git a/example_src/templates/article/tag_index_footer.html b/example_src/templates/article/tag_index_footer.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/templates/article/tag_index_footer.html diff --git a/example_src/templates/article/tag_index_header.html b/example_src/templates/article/tag_index_header.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/templates/article/tag_index_header.html diff --git a/example/templates/article/tag_list_footer.html b/example_src/templates/article/tag_list_footer.html index 8ce77e5..8ce77e5 100644 --- a/example/templates/article/tag_list_footer.html +++ b/example_src/templates/article/tag_list_footer.html diff --git a/example/templates/article/tag_list_header.html b/example_src/templates/article/tag_list_header.html index 7a57395..7a57395 100644 --- a/example/templates/article/tag_list_header.html +++ b/example_src/templates/article/tag_list_header.html diff --git a/example/templates/article/tag_separator.html b/example_src/templates/article/tag_separator.html index c3a6e48..c3a6e48 100644 --- a/example/templates/article/tag_separator.html +++ b/example_src/templates/article/tag_separator.html diff --git a/example_src/test.md b/example_src/test.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/test.md diff --git a/example_src/test2.md b/example_src/test2.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/test2.md diff --git a/example_src/test5.html b/example_src/test5.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/example_src/test5.html 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...') |