Skip to content

Commit

Permalink
Fix #2455
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Aug 27, 2016
1 parent ac49ce3 commit 1820b7b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ New in master
Features
--------

* Detect dependencies in template strings (Issue #2455)
* RSS feeds for sections (Issue #2068)
* New ``data`` metadata that loads data from external files (Issue #2450)
* Shortcode to escape to the template language (Issue #1227)
Expand Down
4 changes: 1 addition & 3 deletions nikola/nikola.py
Expand Up @@ -1486,9 +1486,7 @@ def _template_shortcode_handler(self, *args, **kw):
for k in self._GLOBAL_CONTEXT_TRANSLATABLE:
context[k] = context[k](context['lang'])
output = self.template_system.render_template_to_string(t_data, context)
# XXX FIXME: we have no standard way to get dependency information from
# a template that's not a file
dependencies = []
dependencies = self.template_system.get_string_deps(t_data)
return output, dependencies

def register_shortcode(self, name, f):
Expand Down
8 changes: 8 additions & 0 deletions nikola/plugin_categories.py
Expand Up @@ -204,6 +204,14 @@ def template_deps(self, template_name):
"""Return filenames which are dependencies for a template."""
raise NotImplementedError()

def get_deps(self, filename):
"""Return paths to dependencies for the template loaded from filename."""
raise NotImplementedError()

def get_string_deps(self, text):
"""Find dependencies for a template string."""
raise NotImplementedError()

def render_template(self, template_name, output_name, context):
"""Render template to a file using context.
Expand Down
26 changes: 15 additions & 11 deletions nikola/plugins/template/jinja.py
Expand Up @@ -103,20 +103,24 @@ def render_template_to_string(self, template, context):
"""Render template to a string using context."""
return self.lookup.from_string(template).render(**context)

def get_string_deps(self, text):
"""Find dependencies for a template string."""
deps = set([])
ast = self.lookup.parse(text)
dep_names = meta.find_referenced_templates(ast)
for dep_name in dep_names:
filename = self.lookup.loader.get_source(self.lookup, dep_name)[1]
deps.add(filename)
sub_deps = self.get_deps(filename)
self.dependency_cache[dep_name] = sub_deps
deps |= set(sub_deps)
return list(deps)

def get_deps(self, filename):
"""Return paths to dependencies for the template loaded from filename."""
deps = set([])
with open(filename) as fd:
source = fd.read()
ast = self.lookup.parse(source)
dep_names = meta.find_referenced_templates(ast)
for dep_name in dep_names:
filename = self.lookup.loader.get_source(self.lookup, dep_name)[1]
deps.add(filename)
sub_deps = self.get_deps(filename)
self.dependency_cache[dep_name] = sub_deps
deps |= set(sub_deps)
return list(deps)
text = fd.read()
return self.get_string_deps(text)

def template_deps(self, template_name):
"""Generate list of dependencies for a template."""
Expand Down
10 changes: 7 additions & 3 deletions nikola/plugins/template/mako.py
Expand Up @@ -55,9 +55,8 @@ class MakoTemplates(TemplateSystem):
directories = []
cache_dir = None

def get_deps(self, filename):
"""Get paths to dependencies for a template."""
text = util.read_file(filename)
def get_string_deps(self, text, filename=None):
"""Find dependencies for a template string."""
lex = lexer.Lexer(text=text, filename=filename)
lex.parse()

Expand All @@ -72,6 +71,11 @@ def get_deps(self, filename):
deps[i] = self.get_template_path(d)
return deps

def get_deps(self, filename):
"""Get paths to dependencies for a template."""
text = util.read_file(filename)
return self.get_string_deps(text, filename)

def set_directories(self, directories, cache_folder):
"""Create a new template lookup with set directories."""
cache_dir = os.path.join(cache_folder, '.mako.tmp')
Expand Down

0 comments on commit 1820b7b

Please sign in to comment.