From 3314fe835200aac51de6e76c351454fa061c41ed Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Tue, 2 Jan 2024 21:13:13 -0600 Subject: [PATCH] enable multi templates --- markata/plugins/post_model.py | 2 +- markata/plugins/post_template.py | 20 +++++-- markata/plugins/publish_html.py | 16 +++++- markata/plugins/render_markdown.py | 2 +- markata/templates/base.html | 81 +++++++++++++++-------------- markata/templates/content_base.html | 6 +++ markata/templates/og.html | 16 ++++++ markata/templates/title.html | 18 +++---- 8 files changed, 105 insertions(+), 56 deletions(-) create mode 100644 markata/templates/content_base.html create mode 100644 markata/templates/og.html diff --git a/markata/plugins/post_model.py b/markata/plugins/post_model.py index c4cafb65..3e943aab 100644 --- a/markata/plugins/post_model.py +++ b/markata/plugins/post_model.py @@ -44,7 +44,7 @@ class Post(pydantic.BaseModel): arbitrary_types_allowed=True, extra="allow", ) - template: Optional[str] = "post.html" + template: Optional[str | Dict[str, str]] = "post.html" def __repr_args__(self: "Post") -> "ReprArgs": return [ diff --git a/markata/plugins/post_template.py b/markata/plugins/post_template.py index e9d835eb..b141b003 100644 --- a/markata/plugins/post_template.py +++ b/markata/plugins/post_template.py @@ -72,7 +72,7 @@ from functools import lru_cache import inspect from pathlib import Path -from typing import List, Optional, TYPE_CHECKING, Union +from typing import List, Optional, TYPE_CHECKING, Union, Dict import jinja2 from jinja2 import Template, Undefined @@ -170,7 +170,7 @@ def html(self): class Config(pydantic.BaseModel): head: HeadConfig = HeadConfig() style: Style = Style() - post_template: str = "post.html" + post_template: str | Dict[str, str] = "post.html" dynamic_templates_dir: Path = Path(".markata.cache/templates") templates_dir: List[Path] = pydantic.Field( [Path("templates"), Path(__file__).parents[1] / "templates"], @@ -211,7 +211,7 @@ class PostOverrides(pydantic.BaseModel): class Post(pydantic.BaseModel): config_overrides: PostOverrides = PostOverrides() - template: Optional[str] = None + template: Optional[str | Dict[str, str]] = None @pydantic.validator("template", pre=True, always=True) def default_template(cls, v, *, values): @@ -331,8 +331,20 @@ def get_template(markata, template): @background.task def render_article(markata, article): + if isinstance(article.template, str): + template = get_template(markata, article.template) + return render_template(markata, article, template) + if isinstance(article.template, dict): + htmls = { + slug: render_template(markata, article, get_template(markata, template)) + for slug, template in article.template.items() + } + return htmls + + +def render_template(markata, article, template): + template = get_template(markata, template) merged_config = markata.config - template = get_template(markata, article.template) # TODO do we need to handle merge?? # if head_template: # head = eval( diff --git a/markata/plugins/publish_html.py b/markata/plugins/publish_html.py index 7a5cdd49..6dfbc2ee 100644 --- a/markata/plugins/publish_html.py +++ b/markata/plugins/publish_html.py @@ -131,4 +131,18 @@ def save(markata: "Markata") -> None: """ for article in markata.articles: - article.output_html.write_text(article.html) + if article.html is None: + continue + if isinstance(article.html, str): + article.output_html.write_text(article.html) + if isinstance(article.html, Dict): + for slug, html in article.html.items(): + if slug == "index": + slug = "" + output_html = article.output_html + else: + slug = slugify(slug) + output_html = article.output_html.parent / slug / "index.html" + + output_html.parent.mkdir(parents=True, exist_ok=True) + output_html.write_text(html) diff --git a/markata/plugins/render_markdown.py b/markata/plugins/render_markdown.py index 979ebc88..e344b243 100644 --- a/markata/plugins/render_markdown.py +++ b/markata/plugins/render_markdown.py @@ -127,7 +127,7 @@ class Config(pydantic.BaseModel): class RenderMarkdownPost(pydantic.BaseModel): article_html: Optional[str] = None - html: Optional[str] = None + html: Optional[str | Dict[str, str]] = None @hook_impl() diff --git a/markata/templates/base.html b/markata/templates/base.html index 23643590..5011e536 100644 --- a/markata/templates/base.html +++ b/markata/templates/base.html @@ -1,50 +1,51 @@ - - {% block head %} - {% if post.title or config.title %} - {{ post.title or config.title }} - {% endif %} - - - {% if post.description or config.description %} - - {% endif %} {% if config.icon %} - - {% endif %} + + {% block head %} + {% if post.title or config.title %} + {{ post.title or config.title }} + {% endif %} + + + {% if post.description or config.description %} + + {% endif %} {% if config.icon %} + + {% endif %} - + - + - {% if 'markata.plugins.service_worker' in config.hooks %} - - {% endif %} + {% if 'markata.plugins.service_worker' in config.hooks %} + + {% endif %} - {% include "head.html" %} - {% endblock %} - + {% include "head.html" %} + {% endblock %} + - - {% include "nav.html" %} - {% block content %} {% endblock %} - {% block footer %} {% endblock %} - + + {% block body %} + {% block content %} {% endblock %} + {% block footer %} {% endblock %} + {% endblock %} + diff --git a/markata/templates/content_base.html b/markata/templates/content_base.html new file mode 100644 index 00000000..2f44908b --- /dev/null +++ b/markata/templates/content_base.html @@ -0,0 +1,6 @@ +{% extends "content_base.html" %} +{% block body %} +{% include "nav.html" %} +{% block content %} {% endblock %} +{% block footer %} {% endblock %} +{% endblock %} diff --git a/markata/templates/og.html b/markata/templates/og.html new file mode 100644 index 00000000..cc326f6a --- /dev/null +++ b/markata/templates/og.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} +{% block content %} +
+ + {% include "title.html" %} +
+{% endblock %} diff --git a/markata/templates/title.html b/markata/templates/title.html index cfe56855..342657b7 100644 --- a/markata/templates/title.html +++ b/markata/templates/title.html @@ -1,7 +1,7 @@

{{ title }} {% if config.get %} - + + c-23.562,0-42.736-19.174-42.736-42.735V150.285c0-23.562,19.174-42.735,42.736-42.735h267.741c6.743,0,12.21-5.467,12.21-12.21 + s-5.467-12.21-12.21-12.21H67.157C30.126,83.13,0,113.255,0,150.285v267.743c0,37.029,30.126,67.155,67.157,67.155h267.741 + c37.03,0,67.156-30.126,67.156-67.155V195.061C402.054,188.318,396.587,182.85,389.844,182.85z" /> + c-2.057,4.619-1.056,10.027,2.521,13.6c2.337,2.336,5.461,3.576,8.639,3.576c1.675,0,3.362-0.346,4.96-1.057l65.07-28.963 + c1.83-0.815,5.114-3.97,5.396-4.25L483.876,74.169c7.131-7.131,11.06-16.61,11.06-26.692 + C494.936,37.396,491.007,27.915,483.876,20.791z M466.61,56.897L257.457,266.05c-0.035,0.036-0.055,0.078-0.089,0.107 + l-33.989,15.131L238.51,247.3c0.03-0.036,0.071-0.055,0.107-0.09L447.765,38.058c5.038-5.039,13.819-5.033,18.846,0.005 + c2.518,2.51,3.905,5.855,3.905,9.414C470.516,51.036,469.127,54.38,466.61,56.897z" />