diff --git a/src/ablog/__init__.py b/src/ablog/__init__.py index 4b2c4283..08ba354f 100755 --- a/src/ablog/__init__.py +++ b/src/ablog/__init__.py @@ -67,6 +67,51 @@ def builder_support(builder): return builder.format == "html" and builder.name not in not_supported + +def update_blogpage_context(app, context): + """Inject ABlog page content into `context["body"]` when needed. + + Why pre-render `body`? + ----------------------- + Sphinx themes differ in where they place page content: some render `{{ body }}` + in the `{% block content %}`, others render it in the `{% block body %}`. + ABlog's catalog/collection/redirect pages need to work across themes without + relying on a specific block structure, so we render the final HTML fragment + ourselves and put it into `context["body"]`. + """ + if "redirect" in context: + context["metatags"] = context.get("metatags", "") + context["metatags"] += app.builder.templates.render_string( + """ + """, + context, + ) + context["body"] = app.builder.templates.render( + "ablog/content/redirect.html", + context, + ) + return + + if "collection" in context or "catalog" in context: + def postlink(post): + if post.external_link: + return post.external_link + else: + return context["pathto"](post.docname) + context["anchor"](post) + + context["postlink"] = postlink + + template = ( + "ablog/content/collection.html" + if "collection" in context + else "ablog/content/catalog.html" + ) + context["body"] = app.builder.templates.render(template, context) + + def html_page_context(app, pagename, templatename, context, doctree): if builder_support(app): context["ablog"] = blog = Blog(app) @@ -78,6 +123,17 @@ def html_page_context(app, pagename, templatename, context, doctree): context["feed_path"] = blog.blog_path context["feed_title"] = blog.blog_title + if templatename == "ablog/blog.html": + update_blogpage_context(app, context) + return + + # Replace the default page template (typically `page.html`) with ABlog's + # wrapper template for post pages and blog-related pages. + # See: https://www.sphinx-doc.org/en/master/extdev/event_callbacks.html#event-html-page-context + if pagename in blog or pagename.startswith(blog.config["blog_path"]): + if not templatename.startswith("ablog/"): + return "ablog/blog.html" + def config_inited(app, config): # Automatically identify any blog posts if a pattern is specified in the config diff --git a/src/ablog/post.py b/src/ablog/post.py index c68bcea4..20918bef 100644 --- a/src/ablog/post.py +++ b/src/ablog/post.py @@ -568,7 +568,8 @@ def generate_archive_pages(app): register_posts(app) for post in blog.posts: for redirect in post.redirect: - yield (redirect, {"redirect": post.docname, "post": post}, "ablog/redirect.html") + yield (redirect, {"redirect": post.docname, "post": post}, "ablog/blog.html") + found_docs = app.env.found_docs atom_feed = bool(blog.blog_baseurl) feed_archives = blog.blog_feed_archives @@ -585,7 +586,7 @@ def generate_archive_pages(app): continue context = {"parents": [], "title": title, "header": header, "catalog": catalog, "summary": True} if catalog.docname not in found_docs: - yield (catalog.docname, context, "ablog/catalog.html") + yield (catalog.docname, context, "ablog/blog.html") for collection in catalog: if not collection: continue @@ -600,7 +601,7 @@ def generate_archive_pages(app): } context["feed_title"] = context["title"] if collection.docname not in found_docs: - yield (collection.docname, context, "ablog/collection.html") + yield (collection.docname, context, "ablog/blog.html") if 1: context = { "parents": [], @@ -612,9 +613,9 @@ def generate_archive_pages(app): "feed_path": blog.blog_path, } docname = blog.posts.docname - yield (docname, context, "ablog/collection.html") + yield (docname, context, "ablog/blog.html") context = {"parents": [], "title": _("Drafts"), "collection": blog.drafts, "summary": True} - yield (blog.drafts.docname, context, "ablog/collection.html") + yield (blog.drafts.docname, context, "ablog/blog.html") def generate_atom_feeds(app): diff --git a/src/ablog/templates/ablog/blog.html b/src/ablog/templates/ablog/blog.html new file mode 100644 index 00000000..15482c0f --- /dev/null +++ b/src/ablog/templates/ablog/blog.html @@ -0,0 +1,21 @@ +{%- extends "page.html" %} +{%- block extrahead %} + {{ super() }} + {%- if feed_path %} + + {%- endif %} + {%- if ablog.fontawesome_link_cdn %} + + {%- elif ablog.fontawesome_css_file %} + + {%- endif %} +{%- endblock %} diff --git a/src/ablog/templates/ablog/catalog.html b/src/ablog/templates/ablog/content/catalog.html similarity index 62% rename from src/ablog/templates/ablog/catalog.html rename to src/ablog/templates/ablog/content/catalog.html index 5f746a01..78f92ae1 100644 --- a/src/ablog/templates/ablog/catalog.html +++ b/src/ablog/templates/ablog/content/catalog.html @@ -1,7 +1,4 @@ -{%- extends "page.html" %} {% macro postlink(post) -%} {% if post.external_link --%} {{- post.external_link -}} {% else %} {{- pathto(post.docname) }}{{ -anchor(post) -}} {%- endif %} {%- endmacro %} {% block body %} {% for collection -in catalog %} {% if collection %} +{% for collection in catalog %} {% if collection %}
- {% if post.published %} - {{ post.date.strftime(ablog.post_date_format) }} - {% else %} - Draft - {% endif %} - - {{ post.title }} -
-
Comments