Skip to content
Closed
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
25 changes: 25 additions & 0 deletions src/ablog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from glob import glob
from pathlib import PurePath

from jinja2 import TemplateNotFound
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.errors import ThemeError
from sphinx.jinja2glue import BuiltinTemplateLoader, SphinxFileSystemLoader
Expand Down Expand Up @@ -67,6 +68,26 @@ def builder_support(builder):
return builder.format == "html" and builder.name not in not_supported


def search_theme_page_template(app):
environment = app.builder.templates.environment

# List of theme names that are active via inheritance
# (used for template resolution, not all installed themes)
active_themes = [p.name for p in app.builder.theme.get_theme_dirs()]

for theme in active_themes:
try:
template = f"{theme}/page.html"
app.builder.templates.get_source(environment, template)
return template
except TemplateNotFound:
pass

# Normally not reached.
# Returned as a defensive fallback, just in case.
return "layout.html"


def html_page_context(app, pagename, templatename, context, doctree):
if builder_support(app):
context["ablog"] = blog = Blog(app)
Expand Down Expand Up @@ -116,6 +137,10 @@ def builder_inited(app):
"support it out of the box. Please remove `get_html_templates_path` "
"from `templates_path` in your `conf.py` to resolve this."
)

if not app.config.theme_page_template:
app.config.theme_page_template = search_theme_page_template(app)

theme = app.builder.theme
loaders = app.builder.templates.loaders
templatepathlen = app.builder.templates.templatepathlen
Expand Down
2 changes: 2 additions & 0 deletions src/ablog/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def verify_fn(key, value, config):
("post_redirect_refresh", 5, True),
("post_show_prev_next", True, True),
("skip_injecting_base_ablog_templates", False, True),
("theme_page_template", None, True, require_config_type(str)),
("theme_content_block", "body", True, require_config_type(str)),
]
TOMORROW = datetime.today() + dtmod.timedelta(1)
TOMORROW = TOMORROW.replace(hour=0, minute=0, second=0, microsecond=0)
Expand Down
9 changes: 6 additions & 3 deletions src/ablog/templates/ablog/catalog.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{%- extends "page.html" %} {% macro postlink(post) -%} {% if post.external_link
{%- extends "page.html" %}
{% block page_content %}
{% macro postlink(post) -%} {% if post.external_link
-%} {{- post.external_link -}} {% else %} {{- pathto(post.docname) }}{{
anchor(post) -}} {%- endif %} {%- endmacro %} {% block body %} {% for collection
anchor(post) -}} {%- endif %} {%- endmacro %}{% for collection
in catalog %} {% if collection %}
<div class="section ablog__catalog_header">
<h2>
Expand All @@ -22,4 +24,5 @@ <h2>
</div>
{% endfor %}
</div>
{% endif %} {% endfor %} {% endblock body %}
{% endif %} {% endfor %}
{% endblock %}
6 changes: 4 additions & 2 deletions src/ablog/templates/ablog/collection.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{%- extends "page.html" %} {% block body %} {% macro postlink(post) -%} {% if
{%- extends "page.html" %}
{% block page_content %}
{% macro postlink(post) -%} {% if
post.external_link -%} {{- post.external_link -}} {% else %} {{-
pathto(post.docname) }}{{ anchor(post) -}} {%- endif %} {%- endmacro %}
<div class="section ablog__collection">
Expand Down Expand Up @@ -54,4 +56,4 @@ <h2 class="ablog-post-title">
</div>
{% endfor %} {% endif %}
</div>
{% endblock body %}
{% endblock %}
38 changes: 38 additions & 0 deletions src/ablog/templates/ablog/page_content_post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<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
ablog[pagename].nocomments) and ((pagename in ablog and
(ablog[pagename].published or ablog.disqus_drafts)) or (not pagename in ablog
and ablog.disqus_pages)) %}
<div class="section ablog__comments">
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = "{{ ablog.disqus_shortname }}";
var disqus_identifier = "{{ablog.page_id(pagename)}}";
var disqus_title = "{{title|e}}";
var disqus_url = "{{ablog.page_url(pagename)}}";

(function () {
var dsq = document.createElement("script");
dsq.type = "text/javascript";
dsq.async = true;
dsq.src = "//" + disqus_shortname + ".disqus.com/embed.js";
(
document.getElementsByTagName("head")[0] ||
document.getElementsByTagName("body")[0]
).appendChild(dsq);
})();
</script>
<noscript>
Please enable JavaScript to view the
<a href="https://disqus.com/?ref_noscript">
comments powered by Disqus.</a
></noscript
>
<a href="https://disqus.com" class="dsq-brlink">
comments powered by <span class="logo-disqus">Disqus</span>
</a>
</div>
{% endif %}
</div>
58 changes: 18 additions & 40 deletions src/ablog/templates/page.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{%- extends "layout.html" %} {% set fa = ablog.fontawesome %} {%- block
{%- extends ablog.theme_page_template %}{% set fa = ablog.fontawesome %} {%- block
extrahead %} {{ super() }} {% if feed_path %}
<link
rel="alternate"
Expand All @@ -14,43 +14,21 @@
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
ablog[pagename].nocomments) and ((pagename in ablog and
(ablog[pagename].published or ablog.disqus_drafts)) or (not pagename in ablog
and ablog.disqus_pages)) %}
<div class="section ablog__comments">
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = "{{ ablog.disqus_shortname }}";
var disqus_identifier = "{{ablog.page_id(pagename)}}";
var disqus_title = "{{title|e}}";
var disqus_url = "{{ablog.page_url(pagename)}}";

(function () {
var dsq = document.createElement("script");
dsq.type = "text/javascript";
dsq.async = true;
dsq.src = "//" + disqus_shortname + ".disqus.com/embed.js";
(
document.getElementsByTagName("head")[0] ||
document.getElementsByTagName("body")[0]
).appendChild(dsq);
})();
</script>
<noscript>
Please enable JavaScript to view the
<a href="https://disqus.com/?ref_noscript">
comments powered by Disqus.</a
></noscript
>
<a href="https://disqus.com" class="dsq-brlink">
comments powered by <span class="logo-disqus">Disqus</span>
</a>
</div>
{% endif %} {% endblock extrahead %}
{% block content %}
{% if ablog.theme_content_block == "content" %}
{% block page_content %}
{{ body }}
{% include "ablog/page_content_post.html" %}
{% endblock %}
{% else %}
{{ super() }}
{% endif %}
</div>
{% endblock body %}
{% endblock %}
{% block body %}
{% if ablog.theme_content_block == "body" %}
{{ self.page_content() }}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{% else %}
{{ super() }}
{% endif %}
{% endblock %}
Loading