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

{% endfor %} -{% endif %} {% endfor %} {% endblock body %} +{% endif %} {% endfor %} diff --git a/src/ablog/templates/ablog/collection.html b/src/ablog/templates/ablog/content/collection.html similarity index 87% rename from src/ablog/templates/ablog/collection.html rename to src/ablog/templates/ablog/content/collection.html index 6e1bf1a0..0c9b983c 100644 --- a/src/ablog/templates/ablog/collection.html +++ b/src/ablog/templates/ablog/content/collection.html @@ -1,6 +1,3 @@ -{%- extends "page.html" %} {% block body %} {% macro postlink(post) -%} {% if -post.external_link -%} {{- post.external_link -}} {% else %} {{- -pathto(post.docname) }}{{ anchor(post) -}} {%- endif %} {%- endmacro %}

{% if archive_feed and fa %} @@ -54,4 +51,3 @@

{% endfor %} {% endif %} -{% endblock body %} diff --git a/src/ablog/templates/ablog/content/redirect.html b/src/ablog/templates/ablog/content/redirect.html new file mode 100644 index 00000000..4e8f685d --- /dev/null +++ b/src/ablog/templates/ablog/content/redirect.html @@ -0,0 +1 @@ +You are being redirected to {{ post.title }} in {{ ablog.post_redirect_refresh }} seconds; diff --git a/src/ablog/templates/page.html b/src/ablog/templates/ablog/disqus.html similarity index 63% rename from src/ablog/templates/page.html rename to src/ablog/templates/ablog/disqus.html index 5ec0ed3d..ab46d449 100644 --- a/src/ablog/templates/page.html +++ b/src/ablog/templates/ablog/disqus.html @@ -1,23 +1,5 @@ -{%- extends "layout.html" %} {% set fa = ablog.fontawesome %} {%- block -extrahead %} {{ super() }} {% if feed_path %} - -{% endif %} {% if ablog.fontawesome_link_cdn %} - -{% elif ablog.fontawesome_css_file %} - -{% endif %} {% endblock extrahead %} {% block body %} {{ body }}
- {% if pagename in ablog %} {% include "ablog/postnavy.html" %} {% endif %} {% - if ablog.disqus_shortname and ablog.blog_baseurl and (not + {% if ablog.disqus_shortname and ablog.blog_baseurl and (not ablog[pagename].nocomments) and ((pagename in ablog and (ablog[pagename].published or ablog.disqus_drafts)) or (not pagename in ablog and ablog.disqus_pages)) %} @@ -53,4 +35,3 @@

Comments

{% endif %} -{% endblock body %} diff --git a/src/ablog/templates/ablog/postnavy.html b/src/ablog/templates/ablog/postnavy.html index 6cb11c18..04e5f04a 100644 --- a/src/ablog/templates/ablog/postnavy.html +++ b/src/ablog/templates/ablog/postnavy.html @@ -1,4 +1,5 @@ -{# prev/next are not set for drafts #} {% set post = ablog[pagename] %} {% if +{# prev/next are not set for drafts #} +{% if pagename in ablog %}{% set post = ablog[pagename] %} {% if post.published and ablog.post_show_prev_next %}
@@ -26,3 +27,4 @@
{% endif %} +{% endif %} diff --git a/src/ablog/templates/ablog/redirect.html b/src/ablog/templates/ablog/redirect.html deleted file mode 100644 index e2ce7929..00000000 --- a/src/ablog/templates/ablog/redirect.html +++ /dev/null @@ -1,8 +0,0 @@ -{%- extends "!layout.html" %} {%- block extrahead %} {{ super() }} - -{% endblock extrahead %} {% block body %} You are being redirected to -{{ post.title }} in {{ -ablog.post_redirect_refresh }} seconds; {% endblock body %} diff --git a/src/ablog/templates/catalog.html b/src/ablog/templates/catalog.html deleted file mode 100644 index 196a8510..00000000 --- a/src/ablog/templates/catalog.html +++ /dev/null @@ -1,26 +0,0 @@ -{{ warning("catalog.html is an old template path, that is no longer used by -ablog. Please use ablog/catalog.html instead.") }} {%- 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 %} -
-

- {{ header }} - {{ collection }} -

- {% for post in collection %} -
-

- {% if post.published %} - {{ post.date.strftime(ablog.post_date_format) }} - {% else %} - Draft - {% endif %} - - {{ post.title }} -

-
- {% endfor %} -
-{% endif %} {% endfor %} {% endblock body %} diff --git a/src/ablog/templates/collection.html b/src/ablog/templates/collection.html deleted file mode 100644 index 93b668c7..00000000 --- a/src/ablog/templates/collection.html +++ /dev/null @@ -1,59 +0,0 @@ -{{ warning("collection.html is an old template path, that is no longer used by -ablog. Please use ablog/collection.html instead.") }} {%- extends "page.html" %} -{% block body %} {% macro postlink(post) -%} {% if post.external_link -%} {{- -post.external_link -}} {% else %} {{- pathto(post.docname) }}{{ anchor(post) -}} -{%- endif %} {%- endmacro %} {% endmacro %} -
-

- {% if archive_feed and fa %} - - - {% endif %} - {{ header }} {% if collection.href %} - {{ collection }} - {% else %} {{ collection }} {% endif %} - -

- {% if ablog.blog_archive_titles %} {% for post in collection %} -
-

- {% if post.published %} - {{ post.date.strftime(ablog.post_date_format) }} - {% else %} - Draft - {% endif %} - - {{ post.title }} -

-
- {% endfor %} {% else %} {% for post in collection %} -
-

- {{ post.title }} -

- - {{ post.to_html(collection.docname) }} -

- {{ _("Read more ...") }} -

-
-
- {% endfor %} {% endif %} -
-{% endblock body %}