Skip to content
Permalink
Browse files
Fix #2455
  • Loading branch information
ralsina committed Aug 27, 2016
1 parent ac49ce3 commit 1820b7be213d97617c0a0e99155cde4aeda6e50b
Showing with 32 additions and 17 deletions.
  1. +1 −0 CHANGES.txt
  2. +1 −3 nikola/nikola.py
  3. +8 −0 nikola/plugin_categories.py
  4. +15 −11 nikola/plugins/template/jinja.py
  5. +7 −3 nikola/plugins/template/mako.py
@@ -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)
@@ -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):
@@ -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.
@@ -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."""
@@ -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()

@@ -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')

0 comments on commit 1820b7b

Please sign in to comment.