Skip to content

Commit

Permalink
Add ALL_PAGE_DEPENDENCIES feature to clean up global context
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Apr 15, 2018
1 parent 54cd963 commit 89d5ceb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 0 additions & 1 deletion docs/template-variables.rst
Expand Up @@ -88,7 +88,6 @@ Name Type Descript
``posts_section_title`` TranslatableSetting<str> ``POSTS_SECTION_TITLE`` setting
``rel_link`` function ``Nikola.rel_link`` function
``rss_link`` str ``RSS_LINK`` setting
``rss_path`` TranslatableSetting<str> ``RSS_PATH`` setting
``search_form`` TranslatableSetting<str> ``SEARCH_FORM`` setting
``set_locale`` function ``LocaleBorg.set_locale`` function (or None if not available)
``show_blog_title`` bool ``SHOW_BLOG_TITLE`` setting
Expand Down
16 changes: 14 additions & 2 deletions nikola/nikola.py
Expand Up @@ -599,6 +599,9 @@ def __init__(self, **config):
# set global_context for template rendering
self._GLOBAL_CONTEXT = {}

# dependencies for all pages, not included in global context
self.ALL_PAGE_DEPENDENCIES = {}

self.config.update(config)

# __builtins__ contains useless cruft
Expand Down Expand Up @@ -678,7 +681,6 @@ def __init__(self, **config):
'posts_section_name',
'posts_section_title',
'front_index_header',
'rss_path',
)
# WARNING: navigation_links SHOULD NOT be added to the list above.
# Themes ask for [lang] there and we should provide it.
Expand Down Expand Up @@ -842,6 +844,7 @@ def __init__(self, **config):
self.register_filter(filter_name_format.format(filter_name), filter_definition)

self._set_global_context_from_config()
self._set_all_page_dependencies_from_config()
# Read data files only if a site exists (Issue #2708)
if self.configured:
self._set_global_context_from_data()
Expand Down Expand Up @@ -1116,7 +1119,6 @@ def _set_global_context_from_config(self):
'CONTENT_FOOTER')
self._GLOBAL_CONTEXT['generate_atom'] = self.config.get('GENERATE_ATOM')
self._GLOBAL_CONTEXT['generate_rss'] = self.config.get('GENERATE_RSS')
self._GLOBAL_CONTEXT['rss_path'] = self.config.get('RSS_PATH')
self._GLOBAL_CONTEXT['rss_link'] = self.config.get('RSS_LINK')

self._GLOBAL_CONTEXT['navigation_links'] = self.config.get('NAVIGATION_LINKS')
Expand Down Expand Up @@ -1162,6 +1164,14 @@ def _set_global_context_from_data(self):
# Offer global_data as an alias for data (Issue #2488)
self._GLOBAL_CONTEXT['global_data'] = self._GLOBAL_CONTEXT['data']

def _set_all_page_dependencies_from_config(self):
"""Create dependencies for all pages from configuration.
Changes of values in this dict will force a rebuild of all pages.
Unlike global context, contents are NOT available to templates.
"""
self.ALL_PAGE_DEPENDENCIES['rss_path'] = self.config.get('RSS_PATH')

def _activate_plugins_of_category(self, category):
"""Activate all the plugins of a given category and return them."""
# this code duplicated in tests/base.py
Expand Down Expand Up @@ -2134,6 +2144,7 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
deps_dict['OUTPUT_FOLDER'] = self.config['OUTPUT_FOLDER']
deps_dict['TRANSLATIONS'] = self.config['TRANSLATIONS']
deps_dict['global'] = self.GLOBAL_CONTEXT
deps_dict["all_page_dependencies"] = self.ALL_PAGE_DEPENDENCIES
if post_deps_dict:
deps_dict.update(post_deps_dict)

Expand Down Expand Up @@ -2267,6 +2278,7 @@ def atom_link(link_rel, link_type, link_href):
deps_context["posts"] = [(p.meta[lang]['title'], p.permalink(lang)) for p in
posts]
deps_context["global"] = self.GLOBAL_CONTEXT
deps_context["all_page_dependencies"] = self.ALL_PAGE_DEPENDENCIES

for k in self._GLOBAL_CONTEXT_TRANSLATABLE:
deps_context[k] = deps_context['global'][k](lang)
Expand Down

0 comments on commit 89d5ceb

Please sign in to comment.