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 %} -