Skip to content

Commit 581e340

Browse files
committedMay 22, 2017
Save dependencies for template hooks properly
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
1 parent e4c5336 commit 581e340

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed
 

‎CHANGES.txt

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Features
1616
Bugfixes
1717
--------
1818

19+
* Save dependencies for template hooks properly (using .__doc__ or
20+
.template_registry_identifier for callables)
1921
* Enable larger panorama thumbnails (Issue #2780)
2022
* Disable ``archive_rss`` link handler, which was useless because no
2123
such RSS was ever generated (Issue #2783)

‎docs/extending.txt

+12-4
Original file line numberDiff line numberDiff line change
@@ -537,21 +537,22 @@ HTML:
537537

538538
# In set_site
539539
def generate_html_bit(name, ftype='js'):
540+
"""Generate HTML for an asset."""
540541
return '<script src="/assets/{t}/{n}.{t}">'.format(n=name, t=ftype)
541542

542-
site.template_hooks['extra_head'].append(generate_html_bit, False, 'fancyplugin', type='js')
543+
site.template_hooks['extra_head'].append(generate_html_bit, False, 'fancyplugin', ftype='js')
543544

544545

545546
The second argument to ``append()`` is used to determine whether the function
546547
needs access to the current template context and the site. If it is set to
547548
``True``, the function will also receive ``site`` and ``context`` keyword
548549
arguments. Example use:
549550

550-
551551
.. code-block:: python
552552

553553
# In set_site
554554
def greeting(addr, endswith='', site=None, context=None):
555+
"""Greet someone."""
555556
if context['lang'] == 'en':
556557
greet = u'Hello'
557558
elif context['lang'] == 'es':
@@ -564,6 +565,13 @@ arguments. Example use:
564565

565566
site.template_hooks['page_header'].append(greeting, True, u'Nikola Tesla', endswith=u'!')
566567

568+
Dependencies for template hooks:
569+
570+
* if the input is a string, the string value, alongside arguments to ``append``, is used for calculating dependencies
571+
* if the input is a callable, it attempts ``input.template_registry_identifier``, then ``input.__doc__``, and if neither is available, it uses a static string.
572+
573+
Make sure to provide at least a docstring, or a identifier, to ensure rebuilds work properly.
574+
567575
Shortcodes
568576
==========
569577

@@ -579,7 +587,7 @@ So, if you are creating a plugin that generates markup, it may be a good idea
579587
to register it as a shortcode in addition of to restructured text directive or
580588
markdown extension, thus making it available to all markup formats.
581589

582-
To implement your own shortcodes from a plugin, you can create a plugin inheriting ``ShortcodePlugin`` and
590+
To implement your own shortcodes from a plugin, you can create a plugin inheriting ``ShortcodePlugin`` and
583591
from its ``set_site`` method, call
584592

585593
``Nikola.register_shortcode(name, func)`` with the following arguments:
@@ -610,7 +618,7 @@ variable keyword arguments):
610618
between them, otherwise ``None``.
611619

612620
``lang``:
613-
The current language.
621+
The current language.
614622

615623
If the shortcode tag has arguments of the form ``foo=bar`` they will be
616624
passed as named arguments. Everything else will be passed as positional

‎nikola/nikola.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2243,7 +2243,7 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
22432243
deps_dict.update(post_deps_dict)
22442244

22452245
for k, v in self.GLOBAL_CONTEXT['template_hooks'].items():
2246-
deps_dict['||template_hooks|{0}||'.format(k)] = v._items
2246+
deps_dict['||template_hooks|{0}||'.format(k)] = v.calculate_deps()
22472247

22482248
for k in self._GLOBAL_CONTEXT_TRANSLATABLE:
22492249
deps_dict[k] = deps_dict['global'][k](lang)

‎nikola/plugins/task/galleries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def gen_tasks(self):
173173
self.image_ext_list.extend(self.site.config.get('EXTRA_IMAGE_EXTENSIONS', []))
174174

175175
for k, v in self.site.GLOBAL_CONTEXT['template_hooks'].items():
176-
self.kw['||template_hooks|{0}||'.format(k)] = v._items
176+
self.kw['||template_hooks|{0}||'.format(k)] = v.calculate_deps()
177177

178178
self.site.scan_posts()
179179
yield self.group_task()

‎nikola/plugins/task/listings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def render_listing(in_name, out_name, input_folder, output_folder, folders=[], f
187187
uptodate = {'c': self.site.GLOBAL_CONTEXT}
188188

189189
for k, v in self.site.GLOBAL_CONTEXT['template_hooks'].items():
190-
uptodate['||template_hooks|{0}||'.format(k)] = v._items
190+
uptodate['||template_hooks|{0}||'.format(k)] = v.calculate_deps()
191191

192192
for k in self.site._GLOBAL_CONTEXT_TRANSLATABLE:
193193
uptodate[k] = self.site.GLOBAL_CONTEXT[k](self.kw['default_lang'])

‎nikola/utils.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,23 @@ def append(self, inp, wants_site_and_context=False, *args, **kwargs):
512512
c = callable(inp)
513513
self._items.append((c, inp, wants_site_and_context, args, kwargs))
514514

515+
def calculate_deps(self):
516+
"""Calculate dependencies for a registry."""
517+
deps = []
518+
for is_callable, inp, wants_site_and_context, args, kwargs in self._items:
519+
if not is_callable:
520+
name = inp
521+
elif hasattr(inp, 'template_registry_identifier'):
522+
name = inp.template_registry_identifier
523+
elif hasattr(inp, '__doc__'):
524+
name = inp.__doc__
525+
else:
526+
name = '_undefined_callable_'
527+
deps.append((is_callable, name, wants_site_and_context, args, kwargs))
528+
515529
def __hash__(self):
516530
"""Return hash of a registry."""
517-
return hash(config_changed({self.name: self._items})._calc_digest())
531+
return hash(config_changed({self.name: self.calculate_deps()})._calc_digest())
518532

519533
def __str__(self):
520534
"""Stringify a registry."""

0 commit comments

Comments
 (0)
Please sign in to comment.