Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/ablog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
<meta
http-equiv="refresh"
content="{{ ablog.post_redirect_refresh }}; url={{ pathto(redirect) }}"
/>""",
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)
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/ablog/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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": [],
Expand All @@ -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):
Expand Down
21 changes: 21 additions & 0 deletions src/ablog/templates/ablog/blog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{%- extends "page.html" %}
{%- block extrahead %}
{{ super() }}
{%- if feed_path %}
<link
rel="alternate"
type="application/atom+xml"
href="{{ pathto(feed_path, 1) }}/atom.xml"
title="{{ feed_title }}"
/>
{%- endif %}
{%- if ablog.fontawesome_link_cdn %}
<link rel="stylesheet" href="{{ ablog.fontawesome_link_cdn }}"/>
{%- elif ablog.fontawesome_css_file %}
<link
rel="stylesheet"
href="{{ pathto('_static/' + ablog.fontawesome_css_file, 1) }}"
type="text/css"
/>
{%- endif %}
{%- endblock %}
Original file line number Diff line number Diff line change
@@ -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 %}
<div class="section ablog__catalog_header">
<h2>
<span
Expand All @@ -22,4 +19,4 @@ <h2>
</div>
{% endfor %}
</div>
{% endif %} {% endfor %} {% endblock body %}
{% endif %} {% endfor %}
Original file line number Diff line number Diff line change
@@ -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 %}
<div class="section ablog__collection">
<h1>
{% if archive_feed and fa %}
Expand Down Expand Up @@ -54,4 +51,3 @@ <h2 class="ablog-post-title">
</div>
{% endfor %} {% endif %}
</div>
{% endblock body %}
1 change: 1 addition & 0 deletions src/ablog/templates/ablog/content/redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You are being redirected to <a href="{{ pathto(redirect) }}">{{ post.title }}</a> in {{ ablog.post_redirect_refresh }} seconds;
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
{%- extends "layout.html" %} {% set fa = ablog.fontawesome %} {%- block
extrahead %} {{ super() }} {% if feed_path %}
<link
rel="alternate"
type="application/atom+xml"
href="{{ pathto(feed_path, 1) }}/atom.xml"
title="{{ feed_title }}"
/>
{% endif %} {% if ablog.fontawesome_link_cdn %}
<link href="{{ ablog.fontawesome_link_cdn }}" rel="stylesheet" />
{% elif ablog.fontawesome_css_file %}
<link
rel="stylesheet"
href="{{ pathto('_static/' + ablog.fontawesome_css_file, 1) }}"
type="text/css"
/>
{% endif %} {% endblock extrahead %} {% block body %} {{ body }}
<div class="section ablog__blog_comments">
{% 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)) %}
Expand Down Expand Up @@ -53,4 +35,3 @@ <h2>Comments</h2>
</div>
{% endif %}
</div>
{% endblock body %}
4 changes: 3 additions & 1 deletion src/ablog/templates/ablog/postnavy.html
Original file line number Diff line number Diff line change
@@ -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 %}
<div class="section ablog__prev-next">
<span class="ablog__prev">
Expand Down Expand Up @@ -26,3 +27,4 @@
</span>
</div>
{% endif %}
{% endif %}
8 changes: 0 additions & 8 deletions src/ablog/templates/ablog/redirect.html

This file was deleted.

26 changes: 0 additions & 26 deletions src/ablog/templates/catalog.html

This file was deleted.

59 changes: 0 additions & 59 deletions src/ablog/templates/collection.html

This file was deleted.

Loading