changeset 8360:acc202b71619

templater: provide the standard template filters by default
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Tue, 12 May 2009 12:04:05 +0200
parents 07ddec2ea203
children d8c5a7f25a40
files hgext/highlight/highlight.py mercurial/cmdutil.py mercurial/hgweb/hgweb_mod.py mercurial/hgweb/hgwebdir_mod.py mercurial/templatefilters.py mercurial/templater.py
diffstat 6 files changed, 18 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/highlight/highlight.py	Tue May 12 10:57:55 2009 +0200
+++ b/hgext/highlight/highlight.py	Tue May 12 12:04:05 2009 +0200
@@ -10,9 +10,7 @@
 
 from mercurial import demandimport
 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',])
-
 from mercurial import util, encoding
-from mercurial.templatefilters import filters
 
 from pygments import highlight
 from pygments.util import ClassNotFound
@@ -55,7 +53,7 @@
     colorized = colorized[colorized.find('<pre>')+5:]
     coloriter = iter(colorized.splitlines())
 
-    filters['colorize'] = lambda x: coloriter.next()
+    tmpl.filters['colorize'] = lambda x: coloriter.next()
 
     oldl = tmpl.cache[field]
     newl = oldl.replace('line|escape', 'line|colorize')
--- a/mercurial/cmdutil.py	Tue May 12 10:57:55 2009 +0200
+++ b/mercurial/cmdutil.py	Tue May 12 12:04:05 2009 +0200
@@ -712,10 +712,8 @@
 
     def __init__(self, ui, repo, patch, diffopts, mapfile, buffered):
         changeset_printer.__init__(self, ui, repo, patch, diffopts, buffered)
-        filters = templatefilters.filters.copy()
-        filters['formatnode'] = (ui.debugflag and (lambda x: x)
-                                 or (lambda x: x[:12]))
-        self.t = templater.templater(mapfile, filters,
+        formatnode = ui.debugflag and (lambda x: x) or (lambda x: x[:12])
+        self.t = templater.templater(mapfile, {'formatnode': formatnode},
                                      cache={
                                          'parent': '{rev}:{node|formatnode} ',
                                          'manifest': '{rev}:{node|formatnode}',
--- a/mercurial/hgweb/hgweb_mod.py	Tue May 12 10:57:55 2009 +0200
+++ b/mercurial/hgweb/hgweb_mod.py	Tue May 12 12:04:05 2009 +0200
@@ -7,8 +7,7 @@
 # GNU General Public License version 2, incorporated herein by reference.
 
 import os
-from mercurial import ui, hg, util, hook, error, encoding
-from mercurial import templater, templatefilters
+from mercurial import ui, hg, util, hook, error, encoding, templater
 from common import get_mtime, ErrorResponse
 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
 from common import HTTP_UNAUTHORIZED, HTTP_METHOD_NOT_ALLOWED
@@ -246,7 +245,7 @@
 
         # create the templater
 
-        tmpl = templater.templater(mapfile, templatefilters.filters,
+        tmpl = templater.templater(mapfile,
                                    defaults={"url": req.url,
                                              "staticurl": staticurl,
                                              "urlbase": urlbase,
--- a/mercurial/hgweb/hgwebdir_mod.py	Tue May 12 10:57:55 2009 +0200
+++ b/mercurial/hgweb/hgwebdir_mod.py	Tue May 12 12:04:05 2009 +0200
@@ -8,7 +8,7 @@
 
 import os
 from mercurial.i18n import _
-from mercurial import ui, hg, util, templater, templatefilters
+from mercurial import ui, hg, util, templater
 from mercurial import error, encoding
 from common import ErrorResponse, get_mtime, staticfile, paritygen,\
                    get_contact, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
@@ -304,7 +304,7 @@
 
         style = 'style' in req.form and req.form['style'][0] or self.style
         mapfile = templater.stylemap(style)
-        tmpl = templater.templater(mapfile, templatefilters.filters,
+        tmpl = templater.templater(mapfile,
                                    defaults={"header": header,
                                              "footer": footer,
                                              "motd": motd,
--- a/mercurial/templatefilters.py	Tue May 12 10:57:55 2009 +0200
+++ b/mercurial/templatefilters.py	Tue May 12 12:04:05 2009 +0200
@@ -8,6 +8,12 @@
 import cgi, re, os, time, urllib, textwrap
 import util, templater, encoding
 
+def stringify(thing):
+    '''turn nested template iterator into string.'''
+    if hasattr(thing, '__iter__') and not isinstance(thing, str):
+        return "".join([stringify(t) for t in thing if t is not None])
+    return str(thing)
+
 agescales = [("second", 1),
              ("minute", 60),
              ("hour", 3600),
@@ -194,7 +200,7 @@
     "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"),
     "short": lambda x: x[:12],
     "shortdate": util.shortdate,
-    "stringify": templater.stringify,
+    "stringify": stringify,
     "strip": lambda x: x.strip(),
     "urlescape": lambda x: urllib.quote(x),
     "user": lambda x: util.shortuser(x),
--- a/mercurial/templater.py	Tue May 12 10:57:55 2009 +0200
+++ b/mercurial/templater.py	Tue May 12 12:04:05 2009 +0200
@@ -7,9 +7,10 @@
 
 from i18n import _
 import re, sys, os
-import util, config
+import util, config, templatefilters
 
 path = ['templates', '../templates']
+stringify = templatefilters.stringify
 
 def parsestring(s, quoted=True):
     '''parse a string using simple c-like syntax.
@@ -116,7 +117,8 @@
         self.cache = cache.copy()
         self.map = {}
         self.base = (mapfile and os.path.dirname(mapfile)) or ''
-        self.filters = filters
+        self.filters = templatefilters.filters.copy()
+        self.filters.update(filters)
         self.defaults = defaults
         self.minchunk, self.maxchunk = minchunk, maxchunk
 
@@ -207,9 +209,3 @@
                 return mapfile
 
     raise RuntimeError("No hgweb templates found in %r" % paths)
-
-def stringify(thing):
-    '''turn nested template iterator into string.'''
-    if hasattr(thing, '__iter__') and not isinstance(thing, str):
-        return "".join([stringify(t) for t in thing if t is not None])
-    return str(thing)