From 6f143604c28c3165db35c2cad99a0dc76d7ccdaa Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado <david@luevano.xyz> Date: Wed, 5 May 2021 09:37:43 -0600 Subject: Add working file discovery and destination file structure creation --- ChangeLog | 1 + example/templates/article/article_entry.html | 1 - example/templates/article/article_footer.html | 0 example/templates/article/article_header.html | 1 - example/templates/article/article_list_footer.html | 1 - example/templates/article/article_list_header.html | 2 - example/templates/article/article_separator.html | 0 example/templates/article/footer.html | 2 - example/templates/article/header.html | 8 --- example/templates/article/index_footer.html | 0 example/templates/article/index_header.html | 0 example/templates/article/tag_entry.html | 1 - example/templates/article/tag_index_footer.html | 0 example/templates/article/tag_index_header.html | 0 example/templates/article/tag_list_footer.html | 1 - example/templates/article/tag_list_header.html | 1 - example/templates/article/tag_separator.html | 1 - example_src/a/test3.md | 0 example_src/a/test4.md | 0 example_src/a/test6.html | 0 example_src/b/c/test8.html | 0 example_src/b/c/test9.md | 0 example_src/b/test7.html | 0 example_src/templates/article/article_entry.html | 1 + example_src/templates/article/article_footer.html | 0 example_src/templates/article/article_header.html | 1 + .../templates/article/article_list_footer.html | 1 + .../templates/article/article_list_header.html | 2 + .../templates/article/article_separator.html | 0 example_src/templates/article/footer.html | 2 + example_src/templates/article/header.html | 8 +++ example_src/templates/article/index_footer.html | 0 example_src/templates/article/index_header.html | 0 example_src/templates/article/tag_entry.html | 1 + .../templates/article/tag_index_footer.html | 0 .../templates/article/tag_index_header.html | 0 example_src/templates/article/tag_list_footer.html | 1 + example_src/templates/article/tag_list_header.html | 1 + example_src/templates/article/tag_separator.html | 1 + example_src/test.md | 0 example_src/test2.md | 0 example_src/test5.html | 0 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 ++++++++++++++++++++++ 48 files changed, 191 insertions(+), 113 deletions(-) delete mode 100644 example/templates/article/article_entry.html delete mode 100644 example/templates/article/article_footer.html delete mode 100644 example/templates/article/article_header.html delete mode 100644 example/templates/article/article_list_footer.html delete mode 100644 example/templates/article/article_list_header.html delete mode 100644 example/templates/article/article_separator.html delete mode 100644 example/templates/article/footer.html delete mode 100644 example/templates/article/header.html delete mode 100644 example/templates/article/index_footer.html delete mode 100644 example/templates/article/index_header.html delete mode 100644 example/templates/article/tag_entry.html delete mode 100644 example/templates/article/tag_index_footer.html delete mode 100644 example/templates/article/tag_index_header.html delete mode 100644 example/templates/article/tag_list_footer.html delete mode 100644 example/templates/article/tag_list_header.html delete mode 100644 example/templates/article/tag_separator.html create mode 100644 example_src/a/test3.md create mode 100644 example_src/a/test4.md create mode 100644 example_src/a/test6.html create mode 100644 example_src/b/c/test8.html create mode 100644 example_src/b/c/test9.md create mode 100644 example_src/b/test7.html create mode 100644 example_src/templates/article/article_entry.html create mode 100644 example_src/templates/article/article_footer.html create mode 100644 example_src/templates/article/article_header.html create mode 100644 example_src/templates/article/article_list_footer.html create mode 100644 example_src/templates/article/article_list_header.html create mode 100644 example_src/templates/article/article_separator.html create mode 100644 example_src/templates/article/footer.html create mode 100644 example_src/templates/article/header.html create mode 100644 example_src/templates/article/index_footer.html create mode 100644 example_src/templates/article/index_header.html create mode 100644 example_src/templates/article/tag_entry.html create mode 100644 example_src/templates/article/tag_index_footer.html create mode 100644 example_src/templates/article/tag_index_header.html create mode 100644 example_src/templates/article/tag_list_footer.html create mode 100644 example_src/templates/article/tag_list_header.html create mode 100644 example_src/templates/article/tag_separator.html create mode 100644 example_src/test.md create mode 100644 example_src/test2.md create mode 100644 example_src/test5.html 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 diff --git a/ChangeLog b/ChangeLog index e404d77..9ae16c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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_entry.html b/example/templates/article/article_entry.html deleted file mode 100644 index 6e6befd..0000000 --- a/example/templates/article/article_entry.html +++ /dev/null @@ -1 +0,0 @@ -<li><a href="$$URL">$$DATE $$TITLE</a></li> diff --git a/example/templates/article/article_footer.html b/example/templates/article/article_footer.html deleted file mode 100644 index e69de29..0000000 diff --git a/example/templates/article/article_header.html b/example/templates/article/article_header.html deleted file mode 100644 index d2954d9..0000000 --- a/example/templates/article/article_header.html +++ /dev/null @@ -1 +0,0 @@ -<h1>$$TITLE</h1> \ No newline at end of file diff --git a/example/templates/article/article_list_footer.html b/example/templates/article/article_list_footer.html deleted file mode 100644 index 3d3a44c..0000000 --- a/example/templates/article/article_list_footer.html +++ /dev/null @@ -1 +0,0 @@ -</ul> diff --git a/example/templates/article/article_list_header.html b/example/templates/article/article_list_header.html deleted file mode 100644 index 7b504ea..0000000 --- a/example/templates/article/article_list_header.html +++ /dev/null @@ -1,2 +0,0 @@ -<h2>Articles</h2> -<ul> diff --git a/example/templates/article/article_separator.html b/example/templates/article/article_separator.html deleted file mode 100644 index e69de29..0000000 diff --git a/example/templates/article/footer.html b/example/templates/article/footer.html deleted file mode 100644 index 308b1d0..0000000 --- a/example/templates/article/footer.html +++ /dev/null @@ -1,2 +0,0 @@ -</body> -</html> diff --git a/example/templates/article/header.html b/example/templates/article/header.html deleted file mode 100644 index 48adfef..0000000 --- a/example/templates/article/header.html +++ /dev/null @@ -1,8 +0,0 @@ -<!DOCTYPE html> -<html lang=$$LANG> -<head> -<meta charset="utf-8"> -<title>$$TITLE</title> -$$EXTRAHEAD -</head> -<body> diff --git a/example/templates/article/index_footer.html b/example/templates/article/index_footer.html deleted file mode 100644 index e69de29..0000000 diff --git a/example/templates/article/index_header.html b/example/templates/article/index_header.html deleted file mode 100644 index e69de29..0000000 diff --git a/example/templates/article/tag_entry.html b/example/templates/article/tag_entry.html deleted file mode 100644 index 3a7aaad..0000000 --- a/example/templates/article/tag_entry.html +++ /dev/null @@ -1 +0,0 @@ -<a href="$$URL">$$NAME</a> \ No newline at end of file diff --git a/example/templates/article/tag_index_footer.html b/example/templates/article/tag_index_footer.html deleted file mode 100644 index e69de29..0000000 diff --git a/example/templates/article/tag_index_header.html b/example/templates/article/tag_index_header.html deleted file mode 100644 index e69de29..0000000 diff --git a/example/templates/article/tag_list_footer.html b/example/templates/article/tag_list_footer.html deleted file mode 100644 index 8ce77e5..0000000 --- a/example/templates/article/tag_list_footer.html +++ /dev/null @@ -1 +0,0 @@ -</p> diff --git a/example/templates/article/tag_list_header.html b/example/templates/article/tag_list_header.html deleted file mode 100644 index 7a57395..0000000 --- a/example/templates/article/tag_list_header.html +++ /dev/null @@ -1 +0,0 @@ -<p>Tags: \ No newline at end of file diff --git a/example/templates/article/tag_separator.html b/example/templates/article/tag_separator.html deleted file mode 100644 index c3a6e48..0000000 --- a/example/templates/article/tag_separator.html +++ /dev/null @@ -1 +0,0 @@ -, \ No newline at end of file diff --git a/example_src/a/test3.md b/example_src/a/test3.md new file mode 100644 index 0000000..e69de29 diff --git a/example_src/a/test4.md b/example_src/a/test4.md new file mode 100644 index 0000000..e69de29 diff --git a/example_src/a/test6.html b/example_src/a/test6.html new file mode 100644 index 0000000..e69de29 diff --git a/example_src/b/c/test8.html b/example_src/b/c/test8.html new file mode 100644 index 0000000..e69de29 diff --git a/example_src/b/c/test9.md b/example_src/b/c/test9.md new file mode 100644 index 0000000..e69de29 diff --git a/example_src/b/test7.html b/example_src/b/test7.html new file mode 100644 index 0000000..e69de29 diff --git a/example_src/templates/article/article_entry.html b/example_src/templates/article/article_entry.html new file mode 100644 index 0000000..6e6befd --- /dev/null +++ b/example_src/templates/article/article_entry.html @@ -0,0 +1 @@ +<li><a href="$$URL">$$DATE $$TITLE</a></li> 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 diff --git a/example_src/templates/article/article_header.html b/example_src/templates/article/article_header.html new file mode 100644 index 0000000..d2954d9 --- /dev/null +++ b/example_src/templates/article/article_header.html @@ -0,0 +1 @@ +<h1>$$TITLE</h1> \ No newline at end of file diff --git a/example_src/templates/article/article_list_footer.html b/example_src/templates/article/article_list_footer.html new file mode 100644 index 0000000..3d3a44c --- /dev/null +++ b/example_src/templates/article/article_list_footer.html @@ -0,0 +1 @@ +</ul> diff --git a/example_src/templates/article/article_list_header.html b/example_src/templates/article/article_list_header.html new file mode 100644 index 0000000..7b504ea --- /dev/null +++ b/example_src/templates/article/article_list_header.html @@ -0,0 +1,2 @@ +<h2>Articles</h2> +<ul> 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 diff --git a/example_src/templates/article/footer.html b/example_src/templates/article/footer.html new file mode 100644 index 0000000..308b1d0 --- /dev/null +++ b/example_src/templates/article/footer.html @@ -0,0 +1,2 @@ +</body> +</html> diff --git a/example_src/templates/article/header.html b/example_src/templates/article/header.html new file mode 100644 index 0000000..48adfef --- /dev/null +++ b/example_src/templates/article/header.html @@ -0,0 +1,8 @@ +<!DOCTYPE html> +<html lang=$$LANG> +<head> +<meta charset="utf-8"> +<title>$$TITLE</title> +$$EXTRAHEAD +</head> +<body> 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 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 diff --git a/example_src/templates/article/tag_entry.html b/example_src/templates/article/tag_entry.html new file mode 100644 index 0000000..3a7aaad --- /dev/null +++ b/example_src/templates/article/tag_entry.html @@ -0,0 +1 @@ +<a href="$$URL">$$NAME</a> \ No newline at end of file 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 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 diff --git a/example_src/templates/article/tag_list_footer.html b/example_src/templates/article/tag_list_footer.html new file mode 100644 index 0000000..8ce77e5 --- /dev/null +++ b/example_src/templates/article/tag_list_footer.html @@ -0,0 +1 @@ +</p> diff --git a/example_src/templates/article/tag_list_header.html b/example_src/templates/article/tag_list_header.html new file mode 100644 index 0000000..7a57395 --- /dev/null +++ b/example_src/templates/article/tag_list_header.html @@ -0,0 +1 @@ +<p>Tags: \ No newline at end of file diff --git a/example_src/templates/article/tag_separator.html b/example_src/templates/article/tag_separator.html new file mode 100644 index 0000000..c3a6e48 --- /dev/null +++ b/example_src/templates/article/tag_separator.html @@ -0,0 +1 @@ +, \ No newline at end of file diff --git a/example_src/test.md b/example_src/test.md new file mode 100644 index 0000000..e69de29 diff --git a/example_src/test2.md b/example_src/test2.md new file mode 100644 index 0000000..e69de29 diff --git a/example_src/test5.html b/example_src/test5.html new file mode 100644 index 0000000..e69de29 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('<!DOCTYPE html>\n') - f.write('<html lang=$$LANG>\n') - f.write('<head>\n') - f.write('<meta charset="utf-8">\n') - f.write('<title>$$TITLE</title>\n') - f.write('$$EXTRAHEAD\n') - f.write('</head>\n') - f.write('<body>\n') - - with open('article/footer.html', 'w+') as f: - f.write('</body>\n') - f.write('</html>\n') - - with open('article/index_header.html', 'w+') as f: - f.write('') - - with open('article/tag_list_header.html', 'w+') as f: - f.write('<p>Tags:') - - with open('article/tag_entry.html', 'w+') as f: - f.write('<a href="$$URL">$$NAME</a>') - - with open('article/tag_separator.html', 'w+') as f: - f.write(', ') - - with open('article/tag_list_footer.html', 'w+') as f: - f.write('</p>\n') - - with open('article/article_list_header.html', 'w+') as f: - f.write('<h2>Articles</h2>\n') - f.write('<ul>\n') - - with open('article/article_entry.html', 'w+') as f: - f.write('<li><a href="$$URL">$$DATE $$TITLE</a></li>\n') - - with open('article/article_separator.html', 'w+') as f: - f.write('') - - with open('article/article_list_footer.html', 'w+') as f: - f.write('</ul>\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('<h1>$$TITLE</h1>') - - 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('<!DOCTYPE html>\n') + f.write('<html lang=$$LANG>\n') + f.write('<head>\n') + f.write('<meta charset="utf-8">\n') + f.write('<title>$$TITLE</title>\n') + f.write('$$EXTRAHEAD\n') + f.write('</head>\n') + f.write('<body>\n') + + with open('article/footer.html', 'w+') as f: + f.write('</body>\n') + f.write('</html>\n') + + with open('article/index_header.html', 'w+') as f: + f.write('') + + with open('article/tag_list_header.html', 'w+') as f: + f.write('<p>Tags:') + + with open('article/tag_entry.html', 'w+') as f: + f.write('<a href="$$URL">$$NAME</a>') + + with open('article/tag_separator.html', 'w+') as f: + f.write(', ') + + with open('article/tag_list_footer.html', 'w+') as f: + f.write('</p>\n') + + with open('article/article_list_header.html', 'w+') as f: + f.write('<h2>Articles</h2>\n') + f.write('<ul>\n') + + with open('article/article_entry.html', 'w+') as f: + f.write('<li><a href="$$URL">$$DATE $$TITLE</a></li>\n') + + with open('article/article_separator.html', 'w+') as f: + f.write('') + + with open('article/article_list_footer.html', 'w+') as f: + f.write('</ul>\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('<h1>$$TITLE</h1>') + + 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