Skip to content

Commit 04161d4

Browse files
committed
WIP - REBASE THIS OUT LATER
1 parent a0d0e1c commit 04161d4

12 files changed

Lines changed: 98 additions & 58 deletions

File tree

ablog/__init__.py

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from glob import glob
77
from pathlib import PurePath
88

9+
from sphinx.jinja2glue import BuiltinTemplateLoader, SphinxFileSystemLoader
10+
911
from .blog import CONFIG, Blog
1012
from .post import (
1113
CheckFrontMatter,
@@ -28,6 +30,14 @@
2830
__all__ = ["setup"]
2931

3032

33+
def get_html_templates_path():
34+
"""
35+
Return path to ABlog templates folder.
36+
"""
37+
pkgdir = os.path.abspath(os.path.dirname(__file__))
38+
return os.path.join(pkgdir, "templates")
39+
40+
3141
def anchor(post):
3242
"""
3343
Return anchor string for posts that are page sections.
@@ -62,6 +72,52 @@ def html_page_context(app, pagename, templatename, context, doctree):
6272
context["feed_title"] = blog.blog_title
6373

6474

75+
def builder_inited(app):
76+
if not app.config.inject_templates_after_theme:
77+
if not isinstance(app.builder.templates, BuiltinTemplateLoader):
78+
raise Exception(
79+
"Ablog does not know how to inject templates into with custom "
80+
"template bridges. You can use `ablog.get_html_templates_path()` to "
81+
"get the path to add in your custom template bridge and set "
82+
"`inject_templates_after_theme = False` in your "
83+
"`conf.py` file."
84+
)
85+
if get_html_templates_path() in app.config.templates_path:
86+
raise Exception(
87+
"Found the path from `ablog.get_html_templates_path()` in the "
88+
"`templates_path` variable from `conf.py`. Doing so interferes "
89+
"with Ablog's ability to stay compatible with Sphinx themes that "
90+
"support it out of the box. Please remove `get_html_templates_path` "
91+
"from `templates_path` in your `conf.py` to resolve this."
92+
)
93+
theme = app.builder.theme
94+
loaders = app.builder.templates.loaders
95+
templatepathlen = app.builder.templates.templatepathlen
96+
if theme.get_config("ablog", "inject_templates_after_theme", False):
97+
# Inject *after* the user templates and the theme templates,
98+
# allowing themes to override the templates provided by this
99+
# extension while those templates still serve as a fallback.
100+
loaders.append(SphinxFileSystemLoader(get_html_templates_path()))
101+
else:
102+
# Inject *after* the user templates and *before* the theme
103+
# templates. This enables ablog to provide support for themes
104+
# that don't support it out-of-the-box, like alabaster.
105+
loaders.insert(templatepathlen, SphinxFileSystemLoader(get_html_templates_path()))
106+
107+
# Automatically identify any blog posts if a pattern is specified in the config
108+
if isinstance(app.config.blog_post_pattern, str):
109+
app.config.blog_post_pattern = [app.config.blog_post_pattern]
110+
matched_patterns = []
111+
for pattern in app.config.blog_post_pattern:
112+
pattern = os.path.join(app.srcdir, pattern)
113+
# make sure that blog post paths have forward slashes even on windows
114+
matched_patterns.extend(
115+
PurePath(ii).relative_to(app.srcdir).with_suffix("").as_posix()
116+
for ii in glob(pattern, recursive=True)
117+
)
118+
app.config.matched_blog_posts = matched_patterns
119+
120+
65121
def setup(app):
66122
"""
67123
Setup ABlog extension.
@@ -70,7 +126,7 @@ def setup(app):
70126
app.add_config_value(*args[:3])
71127
app.add_directive("post", PostDirective)
72128
app.add_directive("postlist", PostListDirective)
73-
app.connect("config-inited", config_inited)
129+
app.connect("builder-inited", builder_inited)
74130
app.connect("doctree-read", process_posts)
75131
app.connect("env-purge-doc", purge_posts)
76132
app.connect("doctree-resolved", process_postlist)
@@ -89,27 +145,3 @@ def setup(app):
89145
locale_dir = os.path.join(pkgdir, "locales")
90146
app.config.locale_dirs.append(locale_dir)
91147
return {"version": __version__} # identifies the version of our extension
92-
93-
94-
def config_inited(app, config):
95-
app.config.templates_path.append(get_html_templates_path())
96-
# Automatically identify any blog posts if a pattern is specified in the config
97-
if isinstance(config.blog_post_pattern, str):
98-
config.blog_post_pattern = [config.blog_post_pattern]
99-
matched_patterns = []
100-
for pattern in config.blog_post_pattern:
101-
pattern = os.path.join(app.srcdir, pattern)
102-
# make sure that blog post paths have forward slashes even on windows
103-
matched_patterns.extend(
104-
PurePath(ii).relative_to(app.srcdir).with_suffix("").as_posix()
105-
for ii in glob(pattern, recursive=True)
106-
)
107-
app.config.matched_blog_posts = matched_patterns
108-
109-
110-
def get_html_templates_path():
111-
"""
112-
Return path to ABlog templates folder.
113-
"""
114-
pkgdir = os.path.abspath(os.path.dirname(__file__))
115-
return os.path.join(pkgdir, "templates")

ablog/blog.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,37 +91,38 @@ def verify_fn(key, value, config):
9191
CONFIG = [
9292
# name, default, rebuild, verify_fn
9393
# where verify_fn is (key, value, app.config) --> value, throwing a KeyError if the value isn't right
94-
("blog_path", "blog", True, require_config_type(str)),
95-
("blog_title", "Blog", True, require_config_type(str)),
96-
("blog_baseurl", "", True, require_config_type(str)),
9794
("blog_archive_titles", None, False, require_config_type(bool)),
95+
("blog_authors", {}, True, require_config_full_name_link_dict()),
96+
("blog_baseurl", "", True, require_config_type(str)),
97+
("blog_default_author", None, True, require_config_str_or_list_lookup("blog_authors")),
98+
("blog_default_language", None, True, require_config_str_or_list_lookup("blog_languages")),
99+
("blog_default_location", None, True, require_config_str_or_list_lookup("blog_locations")),
98100
("blog_feed_archives", False, True),
99101
("blog_feed_fulltext", False, True),
102+
("blog_feed_length", None, None),
100103
("blog_feed_subtitle", None, True),
101-
("blog_feed_titles", None, False),
102104
("blog_feed_templates", {"atom": {}}, True),
103-
("blog_feed_length", None, None),
104-
("blog_authors", {}, True, require_config_full_name_link_dict()),
105-
("blog_default_author", None, True, require_config_str_or_list_lookup("blog_authors")),
106-
("blog_locations", {}, True, require_config_full_name_link_dict()),
107-
("blog_default_location", None, True, require_config_str_or_list_lookup("blog_locations")),
105+
("blog_feed_titles", None, False),
108106
("blog_languages", {}, True, require_config_full_name_link_dict()),
109-
("blog_default_language", None, True, require_config_str_or_list_lookup("blog_languages")),
110-
("fontawesome_link_cdn", None, True),
111-
("fontawesome_included", False, True, require_config_type(bool)),
107+
("blog_locations", {}, True, require_config_full_name_link_dict()),
108+
("blog_path", "blog", True, require_config_type(str)),
109+
("blog_post_pattern", [], True, require_config_type((str, list))),
110+
("blog_title", "Blog", True, require_config_type(str)),
111+
("disqus_drafts", False, True),
112+
("disqus_pages", False, True),
113+
("disqus_shortname", None, True),
112114
("fontawesome_css_file", "", True, require_config_type(str)),
113-
("post_date_format", "%d %B %Y", True, require_config_type(str)),
114-
("post_date_format_short", "%d %B", True, require_config_type(str)),
115-
("post_auto_orphan", True, True, require_config_type(bool)),
116-
("post_auto_image", 0, True),
115+
("fontawesome_included", False, True, require_config_type(bool)),
116+
("fontawesome_link_cdn", None, True),
117+
("post_always_section", False, True),
117118
("post_auto_excerpt", 1, True),
119+
("post_auto_image", 0, True),
120+
("post_auto_orphan", True, True, require_config_type(bool)),
121+
("post_date_format_short", "%d %B", True, require_config_type(str)),
122+
("post_date_format", "%d %B %Y", True, require_config_type(str)),
118123
("post_redirect_refresh", 5, True),
119-
("post_always_section", False, True),
120124
("post_show_prev_next", True, True),
121-
("disqus_shortname", None, True),
122-
("disqus_drafts", False, True),
123-
("disqus_pages", False, True),
124-
("blog_post_pattern", [], True, require_config_type((str, list))),
125+
("inject_templates_after_theme", False, True),
125126
]
126127
TOMORROW = datetime.today() + dtmod.timedelta(1)
127128
TOMORROW = TOMORROW.replace(hour=0, minute=0, second=0, microsecond=0)

ablog/post.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def generate_archive_pages(app):
554554
blog = Blog(app)
555555
for post in blog.posts:
556556
for redirect in post.redirect:
557-
yield (redirect, {"redirect": post.docname, "post": post}, "redirect.html")
557+
yield (redirect, {"redirect": post.docname, "post": post}, "ablog/redirect.html")
558558
found_docs = app.env.found_docs
559559
atom_feed = bool(blog.blog_baseurl)
560560
feed_archives = blog.blog_feed_archives
@@ -571,7 +571,7 @@ def generate_archive_pages(app):
571571
continue
572572
context = {"parents": [], "title": title, "header": header, "catalog": catalog, "summary": True}
573573
if catalog.docname not in found_docs:
574-
yield (catalog.docname, context, "catalog.html")
574+
yield (catalog.docname, context, "ablog/catalog.html")
575575
for collection in catalog:
576576
if not collection:
577577
continue
@@ -586,7 +586,7 @@ def generate_archive_pages(app):
586586
}
587587
context["feed_title"] = context["title"]
588588
if collection.docname not in found_docs:
589-
yield (collection.docname, context, "collection.html")
589+
yield (collection.docname, context, "ablog/collection.html")
590590
if 1:
591591
context = {
592592
"parents": [],
@@ -598,9 +598,9 @@ def generate_archive_pages(app):
598598
"feed_path": blog.blog_path,
599599
}
600600
docname = blog.posts.docname
601-
yield (docname, context, "collection.html")
601+
yield (docname, context, "ablog/collection.html")
602602
context = {"parents": [], "title": _("Drafts"), "collection": blog.drafts, "summary": True}
603-
yield (blog.drafts.docname, context, "collection.html")
603+
yield (blog.drafts.docname, context, "ablog/collection.html")
604604

605605

606606
def generate_atom_feeds(app):

ablog/start.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ def w(t, ls=80):
7777
# 'Earth': ('The Blue Planet', 'https://en.wikipedia.org/wiki/Earth),
7878
# }}
7979
80+
# See https://github.com/sunpy/ablog/pull/144 for the full context
81+
# This will prevent ablog from overriding any local templates that you might
82+
# need to use for your project.
83+
# Default is ``False``.
84+
# inject_templates_after_theme = False
85+
8086
# -- Blog Post Related --------------------------------------------------------
8187
8288
# Format date for a post.
@@ -215,9 +221,6 @@ def w(t, ls=80):
215221
'ablog',
216222
]
217223
218-
# Add any paths that contain templates here, relative to this directory.
219-
templates_path = [ablog.get_html_templates_path(), "{dot}templates"]
220-
221224
# The suffix(es) of source filenames.
222225
source_suffix = "{suffix}"
223226

ablog/templates/ablog/collection.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h2 class="ablog-post-title">
5050
{% endif %}
5151
{% endif %}
5252
</li>
53-
{% include "postcard2.html" %}
53+
{% include "ablog/postcard2.html" %}
5454
</ul>
5555
{{ post.to_html(collection.docname) }}
5656
<p class="ablog-post-expand"><a href="{{ pathto(post.docname) }}"><em>{{ _("Read more ...") }}</em></a></p>

ablog/templates/ablog/postcard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h2>
2020
{% endif %}
2121
</h2>
2222
<ul>
23-
{% include "postcard2.html" %}
23+
{% include "ablog/postcard2.html" %}
2424
</ul>
2525
</div>
2626
{% endif %}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
{{ body }}
2525
<div class="section ablog__blog_comments">
2626
{% if pagename in ablog %}
27-
{% include "postnavy.html" %}
27+
{% include "ablog/postnavy.html" %}
2828
{% endif %}
2929
{% 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)) %}
3030
<div class="section ablog__comments">

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"SF": ("San Francisco, CA", "https://en.wikipedia.org/wiki/San_Francisco"),
5858
"Denizli": ("Denizli, Turkey", "https://en.wikipedia.org/wiki/Denizli"),
5959
}
60+
inject_templates_after_theme = False
6061
blog_languages = {
6162
"en": ("English", None),
6263
"nl": ("Nederlands", None),

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ include_package_data = True
1717
setup_requires =
1818
setuptools_scm
1919
install_requires =
20-
docutils>=0.19
20+
docutils
2121
feedgen>=0.9.0
2222
invoke>=1.6.0
2323
python-dateutil>=2.8.0

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
message_extractors={
3030
"ablog": [
3131
("**.py", "python", None),
32+
("templates/**.html", "jinja2", None),
3233
("templates/ablog/**.html", "jinja2", None),
3334
],
3435
},

0 commit comments

Comments
 (0)