diff options
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | e_dst/a/test2.html (renamed from example_src/a/test3.md) | 0 | ||||
-rw-r--r-- | e_dst/test1.html (renamed from example_src/a/test4.md) | 0 | ||||
-rw-r--r-- | e_src/a/test2.html (renamed from example_src/a/test6.html) | 0 | ||||
-rw-r--r-- | e_src/a/test2.md | 13 | ||||
-rw-r--r-- | e_src/templates/article/footer.html (renamed from example_src/b/c/test8.html) | 0 | ||||
-rw-r--r-- | e_src/templates/article/header.html (renamed from example_src/templates/article/article_header.html) | 0 | ||||
-rw-r--r-- | e_src/templates/articles/footer.html (renamed from example_src/b/c/test9.md) | 0 | ||||
-rw-r--r-- | e_src/templates/articles/header.html (renamed from example_src/b/test7.html) | 0 | ||||
-rw-r--r-- | e_src/templates/articles/list_entry.html | 1 | ||||
-rw-r--r-- | e_src/templates/articles/list_footer.html (renamed from example_src/templates/article/article_list_footer.html) | 0 | ||||
-rw-r--r-- | e_src/templates/articles/list_header.html (renamed from example_src/templates/article/article_list_header.html) | 0 | ||||
-rw-r--r-- | e_src/templates/articles/list_separator.html (renamed from example_src/templates/article/article_footer.html) | 0 | ||||
-rw-r--r-- | e_src/templates/common/footer.html (renamed from example_src/templates/article/footer.html) | 0 | ||||
-rw-r--r-- | e_src/templates/common/header.html (renamed from example_src/templates/article/header.html) | 2 | ||||
-rw-r--r-- | e_src/templates/tag/footer.html (renamed from example_src/templates/article/article_separator.html) | 0 | ||||
-rw-r--r-- | e_src/templates/tag/header.html (renamed from example_src/templates/article/index_footer.html) | 0 | ||||
-rw-r--r-- | e_src/templates/tag/list_entry.html (renamed from example_src/templates/article/tag_entry.html) | 0 | ||||
-rw-r--r-- | e_src/templates/tag/list_footer.html (renamed from example_src/templates/article/tag_list_footer.html) | 0 | ||||
-rw-r--r-- | e_src/templates/tag/list_header.html | 1 | ||||
-rw-r--r-- | e_src/templates/tag/list_separator.html (renamed from example_src/templates/article/tag_separator.html) | 0 | ||||
-rw-r--r-- | e_src/test1.html (renamed from example_src/templates/article/index_header.html) | 0 | ||||
-rw-r--r-- | e_src/test1.md | 12 | ||||
-rw-r--r-- | example_src/templates/article/article_entry.html | 1 | ||||
-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_header.html | 1 | ||||
-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/converter.py | 62 | ||||
-rw-r--r-- | src/pyssg/discovery.py | 13 | ||||
-rw-r--r-- | src/pyssg/generator.py | 40 | ||||
-rw-r--r-- | src/pyssg/page.py | 49 | ||||
-rw-r--r-- | src/pyssg/parser.py | 24 | ||||
-rw-r--r-- | src/pyssg/pyssg.py | 15 | ||||
-rw-r--r-- | src/pyssg/template.py | 164 | ||||
-rw-r--r-- | src/pyssg/templates.py | 83 |
38 files changed, 364 insertions, 118 deletions
@@ -1,6 +1,7 @@ CHANGES ======= +* Add working file discovery and destination file structure creation * initial template creation, barebones arg parser * Prepare barebones package info * Update readme diff --git a/example_src/a/test3.md b/e_dst/a/test2.html index e69de29..e69de29 100644 --- a/example_src/a/test3.md +++ b/e_dst/a/test2.html diff --git a/example_src/a/test4.md b/e_dst/test1.html index e69de29..e69de29 100644 --- a/example_src/a/test4.md +++ b/e_dst/test1.html diff --git a/example_src/a/test6.html b/e_src/a/test2.html index e69de29..e69de29 100644 --- a/example_src/a/test6.html +++ b/e_src/a/test2.html diff --git a/e_src/a/test2.md b/e_src/a/test2.md new file mode 100644 index 0000000..0eb26ec --- /dev/null +++ b/e_src/a/test2.md @@ -0,0 +1,13 @@ +title: Test 2 +author: David Luévano +summary: Small summary +lang: en +tags: test + small + archive + +This is an article test, again + +- asdfg +- hjikl + - ?? diff --git a/example_src/b/c/test8.html b/e_src/templates/article/footer.html index e69de29..e69de29 100644 --- a/example_src/b/c/test8.html +++ b/e_src/templates/article/footer.html diff --git a/example_src/templates/article/article_header.html b/e_src/templates/article/header.html index d2954d9..d2954d9 100644 --- a/example_src/templates/article/article_header.html +++ b/e_src/templates/article/header.html diff --git a/example_src/b/c/test9.md b/e_src/templates/articles/footer.html index e69de29..e69de29 100644 --- a/example_src/b/c/test9.md +++ b/e_src/templates/articles/footer.html diff --git a/example_src/b/test7.html b/e_src/templates/articles/header.html index e69de29..e69de29 100644 --- a/example_src/b/test7.html +++ b/e_src/templates/articles/header.html diff --git a/e_src/templates/articles/list_entry.html b/e_src/templates/articles/list_entry.html new file mode 100644 index 0000000..91d2d75 --- /dev/null +++ b/e_src/templates/articles/list_entry.html @@ -0,0 +1 @@ +<li><a href="$$URL">$$DATE - $$TITLE</a></li> diff --git a/example_src/templates/article/article_list_footer.html b/e_src/templates/articles/list_footer.html index 3d3a44c..3d3a44c 100644 --- a/example_src/templates/article/article_list_footer.html +++ b/e_src/templates/articles/list_footer.html diff --git a/example_src/templates/article/article_list_header.html b/e_src/templates/articles/list_header.html index 7b504ea..7b504ea 100644 --- a/example_src/templates/article/article_list_header.html +++ b/e_src/templates/articles/list_header.html diff --git a/example_src/templates/article/article_footer.html b/e_src/templates/articles/list_separator.html index e69de29..e69de29 100644 --- a/example_src/templates/article/article_footer.html +++ b/e_src/templates/articles/list_separator.html diff --git a/example_src/templates/article/footer.html b/e_src/templates/common/footer.html index 308b1d0..308b1d0 100644 --- a/example_src/templates/article/footer.html +++ b/e_src/templates/common/footer.html diff --git a/example_src/templates/article/header.html b/e_src/templates/common/header.html index 48adfef..9c423df 100644 --- a/example_src/templates/article/header.html +++ b/e_src/templates/common/header.html @@ -1,5 +1,5 @@ <!DOCTYPE html> -<html lang=$$LANG> +<html lang="$$LANG"> <head> <meta charset="utf-8"> <title>$$TITLE</title> diff --git a/example_src/templates/article/article_separator.html b/e_src/templates/tag/footer.html index e69de29..e69de29 100644 --- a/example_src/templates/article/article_separator.html +++ b/e_src/templates/tag/footer.html diff --git a/example_src/templates/article/index_footer.html b/e_src/templates/tag/header.html index e69de29..e69de29 100644 --- a/example_src/templates/article/index_footer.html +++ b/e_src/templates/tag/header.html diff --git a/example_src/templates/article/tag_entry.html b/e_src/templates/tag/list_entry.html index 3a7aaad..3a7aaad 100644 --- a/example_src/templates/article/tag_entry.html +++ b/e_src/templates/tag/list_entry.html diff --git a/example_src/templates/article/tag_list_footer.html b/e_src/templates/tag/list_footer.html index 8ce77e5..8ce77e5 100644 --- a/example_src/templates/article/tag_list_footer.html +++ b/e_src/templates/tag/list_footer.html diff --git a/e_src/templates/tag/list_header.html b/e_src/templates/tag/list_header.html new file mode 100644 index 0000000..5bb080d --- /dev/null +++ b/e_src/templates/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/e_src/templates/tag/list_separator.html index c3a6e48..c3a6e48 100644 --- a/example_src/templates/article/tag_separator.html +++ b/e_src/templates/tag/list_separator.html diff --git a/example_src/templates/article/index_header.html b/e_src/test1.html index e69de29..e69de29 100644 --- a/example_src/templates/article/index_header.html +++ b/e_src/test1.html diff --git a/e_src/test1.md b/e_src/test1.md new file mode 100644 index 0000000..85e97b3 --- /dev/null +++ b/e_src/test1.md @@ -0,0 +1,12 @@ +title: Test 1 +author: David Luévano +summary: Small summary +lang: en +tags: test + small + archive + +This is an article test + +- asdfg +- hjikl diff --git a/example_src/templates/article/article_entry.html b/example_src/templates/article/article_entry.html deleted file mode 100644 index 6e6befd..0000000 --- a/example_src/templates/article/article_entry.html +++ /dev/null @@ -1 +0,0 @@ -<li><a href="$$URL">$$DATE $$TITLE</a></li> diff --git a/example_src/templates/article/tag_index_footer.html b/example_src/templates/article/tag_index_footer.html deleted file mode 100644 index e69de29..0000000 --- a/example_src/templates/article/tag_index_footer.html +++ /dev/null diff --git a/example_src/templates/article/tag_index_header.html b/example_src/templates/article/tag_index_header.html deleted file mode 100644 index e69de29..0000000 --- a/example_src/templates/article/tag_index_header.html +++ /dev/null diff --git a/example_src/templates/article/tag_list_header.html b/example_src/templates/article/tag_list_header.html deleted file mode 100644 index 7a57395..0000000 --- a/example_src/templates/article/tag_list_header.html +++ /dev/null @@ -1 +0,0 @@ -<p>Tags:
\ No newline at end of file diff --git a/example_src/test.md b/example_src/test.md deleted file mode 100644 index e69de29..0000000 --- a/example_src/test.md +++ /dev/null diff --git a/example_src/test2.md b/example_src/test2.md deleted file mode 100644 index e69de29..0000000 --- a/example_src/test2.md +++ /dev/null diff --git a/example_src/test5.html b/example_src/test5.html deleted file mode 100644 index e69de29..0000000 --- a/example_src/test5.html +++ /dev/null diff --git a/src/pyssg/converter.py b/src/pyssg/converter.py new file mode 100644 index 0000000..5af2bd2 --- /dev/null +++ b/src/pyssg/converter.py @@ -0,0 +1,62 @@ +import os +from markdown import Markdown +from copy import deepcopy +from .page import Page +from .template import Template + + +def get_pages(src: str, files: list[str]) -> list[Page]: + md: Markdown = Markdown(extensions=['extra', 'meta', 'sane_lists', + 'smarty', 'toc', 'wikilinks'], + output_format='html5') + + pages: list[Page] = [] + + for f in files: + f_name = os.path.join(src, f) + + content = md.reset().convert(open(f_name).read()) + f_time = os.stat(f_name).st_mtime + + pages.append(Page(f_name, f_time, content, md.Meta)) + + return pages + + +def create_html_files(src: str, dst: str, files: list[str]) -> None: + # get the list of page objects + pages: list[Page] = get_pages(src, files) + + # read all templates into a template obj + template: Template = Template() + template.read_templates(src) + for p in pages: + # t=template, p=page + t: Template = deepcopy(template) + p.parse_meta() + + # common + t.header = t.header.replace("$$LANG", p.lang) + t.header = t.header.replace('$$TITLE', p.title) + t.header = t.header.replace('$$EXTRAHEAD', f''' + <!-- example extra head --> + <meta name="robots" content="index, follow"> + <meta property="og:title" content="{p.title}"> + <meta property="og:description" content="{p.summary}">''') + + # article entry + t.article.header = t.article.header.replace('$$TITLE', p.title) + + print(t.header) + print(t.article.header) + print(p.c_html) + print(t.tags.list_header, sep='') + for tag in p.tags: + tag_entry = t.tags.list_entry + tag_entry = tag_entry.replace('$$NAME', tag) + tag_entry = tag_entry.replace('$$URL', p.f_name) + print(tag_entry, sep='') + print(t.tags.list_separator, sep='') + print(t.tags.list_footer) + print(t.article.footer) + print(t.footer) diff --git a/src/pyssg/discovery.py b/src/pyssg/discovery.py index 39c8bb1..606c1d0 100644 --- a/src/pyssg/discovery.py +++ b/src/pyssg/discovery.py @@ -11,7 +11,7 @@ def get_file_list(extensions: list[str], exclude: list[str]=None) -> list[str]: for f in files: if f.endswith(tuple(extensions)): - out.append(os.path.join(root, f)) + out.append(os.path.join(root, f).replace(cwd, '')[1:]) return out @@ -27,14 +27,19 @@ def get_dir_structure(exclude: list[str]=None) -> list[str]: for d in dirs: if root in out: out.remove(root) - out.append(os.path.join(root, d).replace(cwd, '')) + out.append(os.path.join(root, d)) + + return [o.replace(cwd, '')[1:] for o in out] - return out +def get_all_files(src: str) -> tuple[list[str], list[str], list[str]]: + iwd = os.getcwd() + os.chdir(src) -def get_all_files(): md_files = get_file_list(['.md', '.markdown'], ['templates']) html_files = get_file_list(['.html'], ['templates']) dirs = get_dir_structure(['templates']) + os.chdir(iwd) + return (dirs, md_files, html_files) diff --git a/src/pyssg/generator.py b/src/pyssg/generator.py new file mode 100644 index 0000000..143eae1 --- /dev/null +++ b/src/pyssg/generator.py @@ -0,0 +1,40 @@ +import os +import shutil +from .discovery import get_all_files +from .converter import create_html_files + + +def create_dir_structure(dst: str, dirs: list[str]) -> None: + iwd = os.getcwd() + + os.chdir(dst) + cwd = os.getcwd() + + for d in dirs: + # for the dir structure, + # doesn't matter if the dir already exists + try: + os.makedirs(os.path.join(cwd, d)) + except FileExistsError: + pass + + os.chdir(iwd) + + +def copy_html_files(src: str, dst: str, files: list[str]) -> None: + src_file = None + dst_file = None + + for f in files: + src_file = os.path.join(src, f) + dst_file = os.path.join(dst, f) + + shutil.copy2(src_file, dst_file) + + +def generate_static_site(src: str, dst: str) -> None: + dirs, md_files, html_files = get_all_files(src) + create_dir_structure(dst, dirs) + + copy_html_files(src, dst, html_files) + create_html_files(src, dst, md_files) diff --git a/src/pyssg/page.py b/src/pyssg/page.py new file mode 100644 index 0000000..09486ea --- /dev/null +++ b/src/pyssg/page.py @@ -0,0 +1,49 @@ +from datetime import datetime, timezone + + +class Page: + def __init__(self, + f_name: str, + f_time: float, + c_html: str, + c_meta: dict): + self.f_name: str = f_name + self.f_time: float = f_time + self.c_html: str = c_html + self.c_meta: dict = c_meta + + self.title: str = None + self.author: str = None + self.timestamp: str = None + self.summary: str = None + self.lang: str = None + self.tags: list = None + + + def parse_meta(self): + try: + self.title = self.c_meta['title'][0] + except KeyError: + pass + + try: + self.author = self.c_meta['author'][0] + except KeyError: + pass + + self.timestamp = datetime.fromtimestamp(self.f_time, tz=timezone.utc) + + try: + self.summary = self.c_meta['summary'][0] + except KeyError: + pass + + try: + self.lang = self.c_meta['lang'][0] + except KeyError: + pass + + try: + self.tags = self.c_meta['tags'] + except KeyError: + pass diff --git a/src/pyssg/parser.py b/src/pyssg/parser.py deleted file mode 100644 index 8b8d108..0000000 --- a/src/pyssg/parser.py +++ /dev/null @@ -1,24 +0,0 @@ -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 7378705..5b736ca 100644 --- a/src/pyssg/pyssg.py +++ b/src/pyssg/pyssg.py @@ -1,8 +1,8 @@ import os from argparse import ArgumentParser, Namespace -from .templates import create_templates -from .parser import generate_static_site +from .template import Template +from .generator import generate_static_site def get_options() -> Namespace: @@ -31,13 +31,20 @@ def get_options() -> Namespace: return parser.parse_args() -def main(): +def main() -> None: opts = vars(get_options()) src = opts['src'] dst = opts['dst'] if opts['init']: - create_templates(src, dst) + try: + os.mkdir(src) + os.makedirs(os.path.join(dst, 'tag')) + except FileExistsError: + pass + + template = Template() + template.write_templates(src) return if opts['build']: diff --git a/src/pyssg/template.py b/src/pyssg/template.py new file mode 100644 index 0000000..c85df72 --- /dev/null +++ b/src/pyssg/template.py @@ -0,0 +1,164 @@ +import os + + +class HF: + def __init__(self): + self.header: str = None + self.footer: str = None + + +class Common(HF): + def __init__(self): + self.list_header: str = None + self.list_footer: str = None + self.list_entry: str = None + self.list_separator: str = None + + +class Template(HF): + def __init__(self): + self.article: HF = HF() + self.articles: Common = Common() + self.tags: Common = Common() + + + def write_templates(self, src: str) -> None: + # get initial working directory + iwd = os.getcwd() + os.chdir(src) + + # create templates dir + os.mkdir('templates') + os.chdir('templates') + + # common + os.mkdir('common') + os.chdir('common') + self.__write_template('header.html', + ['<!DOCTYPE html>\n', + '<html lang="$$LANG">\n', + '<head>\n', + '<meta charset="utf-8">\n', + '<title>$$TITLE</title>\n', + '$$EXTRAHEAD\n', + '</head>\n', + '<body>\n']) + self.__write_template('footer.html', + ['</body>\n', + '</html>\n']) + + # go back to templates + os.chdir('..') + + # article entry + os.mkdir('article') + os.chdir('article') + self.__write_template('header.html', + ['<h1>$$TITLE</h1>']) + self.__write_template('footer.html', + ['']) + + # go back to templates + os.chdir('..') + + # article index (articles) + os.mkdir('articles') + os.chdir('articles') + self.__write_template('header.html', + ['']) + self.__write_template('list_header.html', + ['<h2>Articles</h2>\n', + '<ul>\n']) + self.__write_template('list_entry.html', + ['<li><a href="$$URL">$$DATE - $$TITLE</a></li>\n']) + self.__write_template('list_separator.html', + ['']) + self.__write_template('list_footer.html', + ['</ul>\n']) + self.__write_template('footer.html', + ['']) + + # go back to templates + os.chdir('..') + + # tag + os.mkdir('tag') + os.chdir('tag') + self.__write_template('header.html', + ['']) + self.__write_template('list_header.html', + ['<p>Tags: ']) + self.__write_template('list_entry.html', + ['<a href="$$URL">$$NAME</a>']) + self.__write_template('list_separator.html', + [', ']) + self.__write_template('list_footer.html', + ['</p>\n']) + self.__write_template('footer.html', + ['']) + + # return to initial working directory + os.chdir(iwd) + + + def read_templates(self, src: str) -> None: + # get initial working directory + iwd = os.getcwd() + os.chdir(os.path.join(src, 'templates')) + + # common + os.chdir('common') + self.header = self.__read_template('header.html') + self.footer = self.__read_template('footer.html') + + # go back to templates + os.chdir('..') + + # article entry + os.chdir('article') + self.article.header = self.__read_template('header.html') + self.article.footer = self.__read_template('footer.html') + + # go back to templates + os.chdir('..') + + # article index + os.chdir('articles') + self.articles.header = self.__read_template('header.html') + self.articles.list_header = \ + self.__read_template('list_header.html') + self.articles.list_entry = \ + self.__read_template('list_entry.html') + self.articles.list_separator = \ + self.__read_template('list_separator.html') + self.articles.list_footer = \ + self.__read_template('list_footer.html') + self.articles.footer = self.__read_template('footer.html') + + # go back to templates + os.chdir('..') + + # tag + os.chdir('tag') + self.tags.header = self.__read_template('header.html') + self.tags.list_header = self.__read_template('list_header.html') + self.tags.list_entry = self.__read_template('list_entry.html') + self.tags.list_separator = self.__read_template('list_separator.html') + self.tags.list_footer = self.__read_template('list_footer.html') + self.tags.footer = self.__read_template('footer.html') + + # return to initial working directory + os.chdir(iwd) + + + def __write_template(self, file_name: str, content: list[str]) -> None: + with open(file_name, 'w+') as f: + for c in content: + f.write(c) + + def __read_template(self, file_name: str) -> str: + out: str = None + with open(file_name, 'r') as f: + out = f.read() + + return out diff --git a/src/pyssg/templates.py b/src/pyssg/templates.py deleted file mode 100644 index 9ae2e96..0000000 --- a/src/pyssg/templates.py +++ /dev/null @@ -1,83 +0,0 @@ -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...') |