Skip to content

Commit

Permalink
enable multi templates
Browse files Browse the repository at this point in the history
  • Loading branch information
WaylonWalker committed Jan 3, 2024
1 parent 8cb0b40 commit 3314fe8
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 56 deletions.
2 changes: 1 addition & 1 deletion markata/plugins/post_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
20 changes: 16 additions & 4 deletions markata/plugins/post_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 15 additions & 1 deletion markata/plugins/publish_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion markata/plugins/render_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
81 changes: 41 additions & 40 deletions markata/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
<!DOCTYPE html>
<html lang="{{ markata.config.lang }}">

<head>
{% block head %}
{% if post.title or config.title %}
<title>{{ post.title or config.title }}</title>
{% endif %}
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{% if post.description or config.description %}
<meta name="description" content="{{ post.description or config.description }}" />
{% endif %} {% if config.icon %}
<link href="/{{ config.icon }}" rel="icon" type="image/png" />
{% endif %}
<head>
{% block head %}
{% if post.title or config.title %}
<title>{{ post.title or config.title }}</title>
{% endif %}
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{% if post.description or config.description %}
<meta name="description" content="{{ post.description or config.description }}" />
{% endif %} {% if config.icon %}
<link href="/{{ config.icon }}" rel="icon" type="image/png" />
{% endif %}

<script>
{% include "theme.js" %}
</script>
<script>
{% include "theme.js" %}
</script>

<style>
<!-- prettier-ignore
-->
<!-- prettier-ignore-attribute -->
{% include "post.css" %}
</style>
<style>
<!-- prettier-ignore
-->
<!-- prettier-ignore-attribute -->
{% include "post.css" %}
</style>

{% if 'markata.plugins.service_worker' in config.hooks %}
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/service-worker.js");
navigator.serviceWorker.addEventListener("controllerchange", () => {
console.log("new worker");
window.location.reload();
});
}
</script>
{% endif %}
{% if 'markata.plugins.service_worker' in config.hooks %}
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/service-worker.js");
navigator.serviceWorker.addEventListener("controllerchange", () => {
console.log("new worker");
window.location.reload();
});
}
</script>
{% endif %}

{% include "head.html" %}
{% endblock %}
</head>
{% include "head.html" %}
{% endblock %}
</head>

<body>
{% include "nav.html" %}
{% block content %} {% endblock %}
{% block footer %} {% endblock %}
</body>
<body>
{% block body %}
{% block content %} {% endblock %}
{% block footer %} {% endblock %}
{% endblock %}
</body>

</html>
6 changes: 6 additions & 0 deletions markata/templates/content_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "content_base.html" %}
{% block body %}
{% include "nav.html" %}
{% block content %} {% endblock %}
{% block footer %} {% endblock %}
{% endblock %}
16 changes: 16 additions & 0 deletions markata/templates/og.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block content %}
<article style="text-align: center;">
<style>
section {
font-size: 200%;
}


.edit {
display: none;
}
</style>
{% include "title.html" %}
</article>
{% endblock %}
18 changes: 9 additions & 9 deletions markata/templates/title.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<section class="title">
<h1 id="title">
{{ title }} {% if config.get %}
<a href="{{ edit_link }}" alt="edit post url" title="edit this post">
<a href="{{ edit_link }}" alt="edit post url" title="edit this post" class="edit">
<span role="img" aria-label="">
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="30" height="30"
Expand All @@ -11,16 +11,16 @@ <h1 id="title">
<g>
<path
d="M389.844,182.85c-6.743,0-12.21,5.467-12.21,12.21v222.968c0,23.562-19.174,42.735-42.736,42.735H67.157
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-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" />
<path
d="M483.876,20.791c-14.72-14.72-38.669-14.714-53.377,0L221.352,229.944c-0.28,0.28-3.434,3.559-4.251,5.396l-28.963,65.069
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" />
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" />
</g>
</g>
</svg>
Expand Down

0 comments on commit 3314fe8

Please sign in to comment.