aboutsummaryrefslogtreecommitdiffstats
path: root/nikola/plugins/template
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2021-02-03 19:17:50 -0500
committerLibravatarUnit 193 <unit193@unit193.net>2021-02-03 19:17:50 -0500
commit475d074fd74425efbe783fad08f97f2df0c4909f (patch)
tree2acdae53999b3c74b716efa4edb5b40311fa356a /nikola/plugins/template
parentcd502d52787f666fff3254d7d7e7578930c813c2 (diff)
parent3a0d66f07b112b6d2bdc2b57bbf717a89a351ce6 (diff)
Update upstream source from tag 'upstream/8.1.2'
Update to upstream version '8.1.2' with Debian dir e5e966a9e6010ef70618dc9a61558fa4db35aceb
Diffstat (limited to 'nikola/plugins/template')
-rw-r--r--nikola/plugins/template/__init__.py2
-rw-r--r--nikola/plugins/template/jinja.plugin2
-rw-r--r--nikola/plugins/template/jinja.py21
-rw-r--r--nikola/plugins/template/mako.plugin2
-rw-r--r--nikola/plugins/template/mako.py33
5 files changed, 29 insertions, 31 deletions
diff --git a/nikola/plugins/template/__init__.py b/nikola/plugins/template/__init__.py
index d5efd61..a530db4 100644
--- a/nikola/plugins/template/__init__.py
+++ b/nikola/plugins/template/__init__.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-# Copyright © 2012-2016 Roberto Alsina and others.
+# Copyright © 2012-2020 Roberto Alsina and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
diff --git a/nikola/plugins/template/jinja.plugin b/nikola/plugins/template/jinja.plugin
index 78fd41b..629b20e 100644
--- a/nikola/plugins/template/jinja.plugin
+++ b/nikola/plugins/template/jinja.plugin
@@ -9,5 +9,5 @@ website = https://getnikola.com/
description = Support for Jinja2 templates.
[Nikola]
-plugincategory = Template
+PluginCategory = Template
diff --git a/nikola/plugins/template/jinja.py b/nikola/plugins/template/jinja.py
index 5a2135f..7795739 100644
--- a/nikola/plugins/template/jinja.py
+++ b/nikola/plugins/template/jinja.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-# Copyright © 2012-2016 Roberto Alsina and others.
+# Copyright © 2012-2020 Roberto Alsina and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
@@ -24,21 +24,20 @@
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
"""Jinja template handler."""
-from __future__ import unicode_literals
-import os
import io
import json
+import os
+
+from nikola.plugin_categories import TemplateSystem
+from nikola.utils import makedirs, req_missing, sort_posts, _smartjoin_filter
+
try:
import jinja2
from jinja2 import meta
except ImportError:
- jinja2 = None # NOQA
-
-from nikola.plugin_categories import TemplateSystem
-from nikola.utils import makedirs, req_missing
+ jinja2 = None
class JinjaTemplates(TemplateSystem):
@@ -65,6 +64,8 @@ class JinjaTemplates(TemplateSystem):
self.lookup.trim_blocks = True
self.lookup.lstrip_blocks = True
self.lookup.filters['tojson'] = json.dumps
+ self.lookup.filters['sort_posts'] = sort_posts
+ self.lookup.filters['smartjoin'] = _smartjoin_filter
self.lookup.globals['enumerate'] = enumerate
self.lookup.globals['isinstance'] = isinstance
self.lookup.globals['tuple'] = tuple
@@ -107,7 +108,7 @@ class JinjaTemplates(TemplateSystem):
"""Find dependencies for a template string."""
deps = set([])
ast = self.lookup.parse(text)
- dep_names = meta.find_referenced_templates(ast)
+ dep_names = [d for d in meta.find_referenced_templates(ast) if d]
for dep_name in dep_names:
filename = self.lookup.loader.get_source(self.lookup, dep_name)[1]
sub_deps = [filename] + self.get_deps(filename)
@@ -117,7 +118,7 @@ class JinjaTemplates(TemplateSystem):
def get_deps(self, filename):
"""Return paths to dependencies for the template loaded from filename."""
- with io.open(filename, 'r', encoding='utf-8') as fd:
+ with io.open(filename, 'r', encoding='utf-8-sig') as fd:
text = fd.read()
return self.get_string_deps(text)
diff --git a/nikola/plugins/template/mako.plugin b/nikola/plugins/template/mako.plugin
index 308d291..2d353bf 100644
--- a/nikola/plugins/template/mako.plugin
+++ b/nikola/plugins/template/mako.plugin
@@ -9,5 +9,5 @@ website = https://getnikola.com/
description = Support for Mako templates.
[Nikola]
-plugincategory = Template
+PluginCategory = Template
diff --git a/nikola/plugins/template/mako.py b/nikola/plugins/template/mako.py
index 0c9bb64..30e2041 100644
--- a/nikola/plugins/template/mako.py
+++ b/nikola/plugins/template/mako.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-# Copyright © 2012-2016 Roberto Alsina and others.
+# Copyright © 2012-2020 Roberto Alsina and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
@@ -26,12 +26,9 @@
"""Mako template handler."""
-from __future__ import unicode_literals, print_function, absolute_import
import io
import os
import shutil
-import sys
-import tempfile
from mako import exceptions, util, lexer, parsetree
from mako.lookup import TemplateLookup
@@ -39,9 +36,9 @@ from mako.template import Template
from markupsafe import Markup # It's ok, Mako requires it
from nikola.plugin_categories import TemplateSystem
-from nikola.utils import makedirs, get_logger, STDERR_HANDLER
+from nikola.utils import makedirs, get_logger
-LOGGER = get_logger('mako', STDERR_HANDLER)
+LOGGER = get_logger('mako')
class MakoTemplates(TemplateSystem):
@@ -57,7 +54,7 @@ class MakoTemplates(TemplateSystem):
def get_string_deps(self, text, filename=None):
"""Find dependencies for a template string."""
- lex = lexer.Lexer(text=text, filename=filename)
+ lex = lexer.Lexer(text=text, filename=filename, input_encoding='utf-8')
lex.parse()
deps = []
@@ -68,7 +65,12 @@ class MakoTemplates(TemplateSystem):
# Some templates will include "foo.tmpl" and we need paths, so normalize them
# using the template lookup
for i, d in enumerate(deps):
- deps[i] = self.get_template_path(d)
+ dep = self.get_template_path(d)
+ if dep:
+ deps[i] = dep
+ else:
+ LOGGER.error("Cannot find template {0} referenced in {1}",
+ d, filename)
return deps
def get_deps(self, filename):
@@ -79,13 +81,6 @@ class MakoTemplates(TemplateSystem):
def set_directories(self, directories, cache_folder):
"""Create a new template lookup with set directories."""
cache_dir = os.path.join(cache_folder, '.mako.tmp')
- # Workaround for a Mako bug, Issue #825
- if sys.version_info[0] == 2:
- try:
- os.path.abspath(cache_dir).decode('ascii')
- except UnicodeEncodeError:
- cache_dir = tempfile.mkdtemp()
- LOGGER.warning('Because of a Mako bug, setting cache_dir to {0}'.format(cache_dir))
if os.path.exists(cache_dir):
shutil.rmtree(cache_dir)
self.directories = directories
@@ -103,6 +98,7 @@ class MakoTemplates(TemplateSystem):
self.lookup = TemplateLookup(
directories=self.directories,
module_directory=self.cache_dir,
+ input_encoding='utf-8',
output_encoding='utf-8')
def set_site(self, site):
@@ -135,9 +131,10 @@ class MakoTemplates(TemplateSystem):
dep_filenames = self.get_deps(template.filename)
deps = [template.filename]
for fname in dep_filenames:
- deps += [fname] + self.get_deps(fname)
- self.cache[template_name] = deps
- return list(self.cache[template_name])
+ # yes, it uses forward slashes on Windows
+ deps += self.template_deps(fname.split('/')[-1])
+ self.cache[template_name] = list(set(deps))
+ return self.cache[template_name]
def get_template_path(self, template_name):
"""Get the path to a template or return None."""