summaryrefslogtreecommitdiff
path: root/src/pyssg/converter.py
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2021-05-09 12:37:01 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2021-05-09 12:37:01 -0600
commit0a9c6693255800c6c197a3dcf3614046c296e293 (patch)
treed4e9999c2a0931db95379f7cb3a9c5fe0d7a7eaa /src/pyssg/converter.py
parent6f143604c28c3165db35c2cad99a0dc76d7ccdaa (diff)
semiworking program, still very alpha
Diffstat (limited to 'src/pyssg/converter.py')
-rw-r--r--src/pyssg/converter.py62
1 files changed, 62 insertions, 0 deletions
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)