diff --git a/config.py b/config.py new file mode 100644 index 0000000..38e0a85 --- /dev/null +++ b/config.py @@ -0,0 +1,6 @@ +import os + +class Config: + MAIN_PAGE_TITLE = "Selected poems" + CONTENT_DIR = os.environ.get('CONTENT_DIR') or 'content' + HEADER_IMAGE = 'static/header.jpg' diff --git a/hydrogen.py b/hydrogen.py new file mode 100644 index 0000000..ac3f229 --- /dev/null +++ b/hydrogen.py @@ -0,0 +1,88 @@ +from pathlib import Path +import frontmatter +import markdown +from jinja2 import Environment, FileSystemLoader +import shutil +from config import Config + +content_dir = Path('content') +template_dir = Path('templates') +output_dir = Path('public') +img_dir = Path('public/images') +assets_dir = Path('public/assets') +assets_css_dir = Path('public/assets/css') +assets_js_dir = Path('public/assets/js') +static_dir = Path('static') +header_image = Config.HEADER_IMAGE or '/static/header.jpg' + +class ContentItemPrototype: + def render_content(self): + if self.image_src_file.exists(): + shutil.copyfile(self.image_src_file, output_dir / self.image) + if self.custom_css_src_file.exists(): + shutil.copyfile(self.custom_css_src_file, output_dir / self.custom_css) + if self.custom_js_src_file.exists(): + shutil.copyfile(self.custom_js_src_file, output_dir / self.custom_js) + with self.content_output_path.open("w", encoding="utf-8") as f: + #f.write(content_item_template.render(page_title = self.title, header_image = header_image, image = self.image, custom_css = self.custom_css, custom_js = self.custom_js, content=self.html)) + f.write(content_item_template.render(content_item = self, page_title = self.title)) + + def __init__(self, md_file): + self.slug = md_file.stem + self.data = frontmatter.load(md_file) + self.html = markdown.markdown(self.data.content) + self.preview = self.html[:300] + " ...read more" + self.title = self.data.get("title", self.slug) + self.omit_second_title = self.data.get("omit_second_title", False) + self.date = self.data.get("data", "2000-01-01T00:00:00+03:00") + self.content_output_path = output_dir / f"{self.slug}.html" + self.image_src_file = Path(f"{content_dir}/{md_file.stem}.jpg") + self.image = f"images/{md_file.stem}.jpg" if self.image_src_file.exists() else None + if self.slug == 'about': + self.image = 'static/about.jpg' + self.custom_css_src_file = Path(f"{content_dir}/{md_file.stem}.css") + self.custom_css = f"assets/css/{md_file.stem}.css" if self.custom_css_src_file.exists() else None + self.custom_js_src_file = Path(f"{content_dir}/{md_file.stem}.js") + self.custom_js = f"assets/js/{md_file.stem}.js" if self.custom_js_src_file.exists() else None + +# Recreate the output dir if needed +if output_dir.exists(): + shutil.rmtree(output_dir) +output_dir.mkdir() + +# Create public subdirs +img_dir.mkdir() +assets_dir.mkdir() +assets_css_dir.mkdir() +assets_js_dir.mkdir() + +# Copy static files if exist +if static_dir.exists(): + shutil.copytree(static_dir, output_dir / "static") + +# Prepare template rendering engine +env = Environment(loader=FileSystemLoader(str(template_dir))) +env.globals['header_image'] = header_image +index_template = env.get_template("index.html") +content_item_template = env.get_template("content_item.html") + +# Parse the content files +content_items = [] +for md_file in content_dir.glob("*.md"): + citem = ContentItemPrototype(md_file) + citem.render_content() + content_items.append({ + "slug": citem.slug, + "title": citem.title, + "date": citem.date, + "preview": markdown.markdown(citem.preview), + "image": citem.image, + }) + +# Render the index file +with (output_dir / "index.html").open("w", encoding="utf-8") as f: + f.write(index_template.render(page_title = Config.MAIN_PAGE_TITLE, content_items=content_items)) + +# Render the about file +about_content = ContentItemPrototype(Path('static/about.md')) +about_content.render_content() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d822853 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-frontmatter +jinja2 +markdown diff --git a/static/about.jpg b/static/about.jpg new file mode 100644 index 0000000..ac7bb59 Binary files /dev/null and b/static/about.jpg differ diff --git a/static/about.md b/static/about.md new file mode 100644 index 0000000..f51267b --- /dev/null +++ b/static/about.md @@ -0,0 +1,6 @@ +--- +title: "About" +omit_second_title: True +--- + + diff --git a/static/header.jpg b/static/header.jpg new file mode 100644 index 0000000..bcc5d93 Binary files /dev/null and b/static/header.jpg differ diff --git a/templates/content_item.html b/templates/content_item.html new file mode 100644 index 0000000..ae9720a --- /dev/null +++ b/templates/content_item.html @@ -0,0 +1,24 @@ +{% extends "default.html" %} + +{% block head_includes %} + {% if content_item.custom_css %} + + {% endif %} +{% endblock %} + +{% block content %} +
+ {% if content_item.image %}{% endif %} + {% if not content_item.omit_second_title %} +

{{ content_item.title }}

+ {% endif %} +
{{ content_item.html | safe }}
+ ← Back +
+{% endblock %} + +{% block footer_includes %} + {% if content_item.custom_js %} + + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/templates/default.html b/templates/default.html new file mode 100644 index 0000000..9c93bb8 --- /dev/null +++ b/templates/default.html @@ -0,0 +1,90 @@ + + + + + {% set base = "" %} + + + + {{ page_title }} + + + +
+
+

{{ page_title }}

+
+
+
+ + Home + About + +
+ +
{% block content %}{% endblock %}
+ + + + + {% block footer_includes %} + {% endblock %} + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..2c8fe7f --- /dev/null +++ b/templates/index.html @@ -0,0 +1,19 @@ +{% extends "default.html" %} +{% block content %} + +
+ {% for content_item in content_items %} +
+
+ {{ content_item.title }} +
+
{{ content_item.title }}
+

{{ content_item.preview | safe}}

+ +
+
+
+ {% endfor %} +
+ +{% endblock %} \ No newline at end of file