diff options
Diffstat (limited to 'src/pyssg/converter.py')
-rw-r--r-- | src/pyssg/converter.py | 231 |
1 files changed, 189 insertions, 42 deletions
diff --git a/src/pyssg/converter.py b/src/pyssg/converter.py index 5af2bd2..d054855 100644 --- a/src/pyssg/converter.py +++ b/src/pyssg/converter.py @@ -1,62 +1,209 @@ import os +from datetime import datetime from markdown import Markdown from copy import deepcopy -from .page import Page + +from .database import Database from .template import Template +from .page import Page -def get_pages(src: str, files: list[str]) -> list[Page]: +def get_pages(src: str, + files: list[str], + db: Database) -> (list[Page], list[Page]): md: Markdown = Markdown(extensions=['extra', 'meta', 'sane_lists', 'smarty', 'toc', 'wikilinks'], output_format='html5') - pages: list[Page] = [] - + all_pages: list[Page] = [] + updated_pages: list[Page] = [] for f in files: - f_name = os.path.join(src, f) + src_file: str = os.path.join(src, f) + # get flag if update is successful + updated: bool = db.update(src_file, remove=f'{src}/') - content = md.reset().convert(open(f_name).read()) - f_time = os.stat(f_name).st_mtime + page: Page = None + content: str = md.reset().convert(open(src_file).read()) + page = Page(f, db.e[f][0], db.e[f][1], content, md.Meta) - pages.append(Page(f_name, f_time, content, md.Meta)) + if updated: + updated_pages.append(page) + all_pages.append(page) - return pages + # add its tag to corresponding entry if existent + if page.tags is not None: + db.update_tags(f, page.tags) -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) + return (all_pages, updated_pages) - # read all templates into a template obj - template: Template = Template() - template.read_templates(src) + +def create_articles(dst: str, + pages: list[Page], + template: Template) -> None: + # TODO: clean this mess + # TODO: proper creation of html files for p in pages: - # t=template, p=page - t: Template = deepcopy(template) - p.parse_meta() + create_article(dst, p, template) + + +def create_article(dst: str, + page: Page, + template: Template) -> None: + # TODO: clean this mess + # make temporary template + t: Template = deepcopy(template) + # TODO: make this configurable + base_url: str = 'https://blog.luevano.xyz/' + f_name: str = page.name + f_name = f_name.replace('.md', '.html') + f_name = f_name.replace('.markdown', '.html') + + with open(os.path.join(dst, f_name), 'w') as f: # 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) + t.header = t.header.replace("$$LANG", + page.lang if page.lang is not None else 'en') + t.header = t.header.replace('$$TITLE', page.title) + t.header = t.header.replace('$$EXTRAHEAD', '') + + # article header + t.article.header = t.article.header.replace('$$TITLE', page.title) + + # Actually write to the html file + f.write(t.header) + f.write(t.article.header) + f.write(page.html) + + if page.tags is not None: + tag_amount: int = len(page.tags) + + f.write(t.tags.list_header) + for i, tag in enumerate(page.tags): + t_entry: str = t.tags.list_entry + t_entry = t_entry.replace('$$URL', f'{base_url}tag/@{tag}.html') + t_entry = t_entry.replace('$$NAME', tag) + + f.write(t_entry) + # don't write last separator, not needed + if i != tag_amount - 1: + f.write(t.tags.list_separator) + f.write(t.tags.list_footer) + + f.write(t.article.footer) + f.write(t.footer) + + +def get_all_tags(pages: list[Page]) -> list[str]: + tags: list[str] = [] + for p in pages: + if p.tags is not None: + for t in p.tags: + if t not in tags: + tags.append(t) + tags.sort() + + return tags + + +def create_tags(dst: str, + tags: list[str], + pages: list[Page], + template: Template) -> None: + for t in tags: + # get a list of all pages that have current tag + # and sort them (by time) + tag_pages: list[Page] = [] + for p in pages: + if p.tags is not None and t in p.tags: + tag_pages.append(p) + tag_pages.sort(reverse=True) + + # build tag page + create_tag(dst, t, tag_pages, template) + + # clean list of pages with current tag + tag_pages = [] + + +def create_tag(dst: str, + tag: str, + pages: list[Page], + template: Template) -> None: + # TODO: clean this mess + # make temporary template + t: Template = deepcopy(template) + # TODO: make this configurable + base_url: str = 'https://blog.luevano.xyz/' + + with open(os.path.join(dst, f'tag/@{tag}.html'), 'w') as f: + # common + t.header = t.header.replace("$$LANG", 'en') + t.header = t.header.replace('$$TITLE', f'Posts filtered by tag "{tag}"') + t.header = t.header.replace('$$EXTRAHEAD', '') + + # tag header + t.tags.header = t.tags.header.replace('$$NAME', tag) + t.tags.header = t.tags.header.replace('$$URL', + f'{base_url}tag/@{tag}.html') + + # Actually write to the html file + f.write(t.header) + f.write(t.tags.header) + f.write(t.articles.list_header) + + month_year: str = '-' + for p in pages: + c_month_year: str = p.c_datetime.strftime('%B %Y') + if c_month_year != month_year: + month_year = c_month_year + + month_sep: str = t.articles.list_separator + month_sep = month_sep.replace('$$SEP', month_year) + + f.write(month_sep) + + f_name: str = p.name + f_name = f_name.replace('.md', '.html') + f_name = f_name.replace('.markdown', '.html') + + page_entry: str = t.articles.list_entry + page_entry = page_entry.replace('$$URL', f'{base_url}{f_name}') + page_entry = page_entry.replace('$$DATE', + p.c_datetime.strftime('%b %d')) + page_entry = page_entry.replace('$$TITLE', p.title) + + f.write(page_entry) + + f.write(t.articles.list_footer) + f.write(t.tags.footer) + f.write(t.footer) + + +def create_article_index(dst: str, + tags: list[str], + pages: list[Page]) -> None: + # TODO: actually make this function + pass + + +def create_html_files(src: str, + dst: str, + files: list[str], + db: Database) -> None: + # get the list of page objects + all_pages, updated_pages = get_pages(src, files, db) + + # get all tags + all_tags = get_all_tags(all_pages) + + # read all templates into a template obj + template: Template = Template(src) + template.read() + + # create each category of html pages + create_articles(dst, updated_pages, template) + create_tags(dst, all_tags, all_pages, template) + + # create the article index + create_article_index(dst, all_tags, all_pages) |