changeset 2422:5425cf18929b

template: fixed the use of eval_context_filter in item_filter
author Goffi <goffi@goffi.org>
date Sun, 05 Nov 2017 23:36:45 +0100
parents b7e24ce97a06
children e3edbccd26d7
files src/tools/common/template.py
diffstat 1 files changed, 23 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/common/template.py	Sun Nov 05 20:34:51 2017 +0100
+++ b/src/tools/common/template.py	Sun Nov 05 23:36:45 2017 +0100
@@ -427,7 +427,8 @@
                 continue
         return u' '.join(classes) or None
 
-    def _item_filter(self, item, filters):
+    @jinja2.contextfilter
+    def _item_filter(self, ctx, item, filters):
         """return item's value, filtered if suitable
 
         @param item(object): item to filter
@@ -446,14 +447,28 @@
         if filter_ is None:
             return value
         elif isinstance(filter_, dict):
-            filter_args = filter_.get(u'filter_args')
+            filters_args = filter_.get(u'filters_args')
             for idx, f_name in enumerate(filter_.get(u'filters', [])):
-                kwargs = filter_args[idx] if filter_args is not None else {}
-                value = self.env.filters[f_name](value, **kwargs)
-            template = filter_.get(u'template', u'{value}')
-            return template.format(value=value)
-        else:
-            return filter_(item.value)
+                kwargs = filters_args[idx] if filters_args is not None else {}
+                filter_func = self.env.filters[f_name]
+                try:
+                    eval_context_filter = filter_func.evalcontextfilter
+                except AttributeError:
+                    eval_context_filter = False
+
+                if eval_context_filter:
+                    value = filter_func(ctx.eval_ctx, value, **kwargs)
+                else:
+                    value = filter_func(value, **kwargs)
+            template = filter_.get(u'template')
+            if template:
+                # format will return a string, so we need to check first
+                # if the value is safe or not, and re-mark it after formatting
+                is_safe = isinstance(value, safe)
+                value = template.format(value=value)
+                if is_safe:
+                    value = safe(value)
+            return value
 
     def _adv_format(self, value, template, **kwargs):
         """Advancer formatter