[PATCH 2 of 3] templater: provide the standard template filters by default

Dirkjan Ochtman dirkjan at ochtman.nl
Mon Apr 6 10:44:31 CDT 2009


# HG changeset patch
# User Dirkjan Ochtman <dirkjan at ochtman.nl>
# Date 1239028110 -7200
# Node ID e5273a9182109eb5489598ad25425659ec0b66e5
# Parent  4439625da15a80188c96a2b0e9f4101d268cfdf4
templater: provide the standard template filters by default

diff -r 4439625da15a -r e5273a918210 mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Mon Apr 06 15:09:54 2009 +0200
+++ b/mercurial/cmdutil.py	Mon Apr 06 16:28:30 2009 +0200
@@ -699,10 +699,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}',
diff -r 4439625da15a -r e5273a918210 mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py	Mon Apr 06 15:09:54 2009 +0200
+++ b/mercurial/hgweb/hgweb_mod.py	Mon Apr 06 16:28:30 2009 +0200
@@ -7,8 +7,7 @@
 # of the GNU General Public License, 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
@@ -244,7 +243,7 @@
 
         # create the templater
 
-        tmpl = templater.templater(mapfile, templatefilters.filters,
+        tmpl = templater.templater(mapfile,
                                    defaults={"url": req.url,
                                              "staticurl": staticurl,
                                              "urlbase": urlbase,
diff -r 4439625da15a -r e5273a918210 mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py	Mon Apr 06 15:09:54 2009 +0200
+++ b/mercurial/hgweb/hgwebdir_mod.py	Mon Apr 06 16:28:30 2009 +0200
@@ -8,7 +8,7 @@
 
 import os
 from mercurial.i18n import _
-from mercurial import ui, hg, util, templater, templatefilters, error, encoding
+from mercurial import ui, hg, util, templater, error, encoding
 from common import ErrorResponse, get_mtime, staticfile, paritygen,\
                    get_contact, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
 from hgweb_mod import hgweb
@@ -318,7 +318,7 @@
         if self.stripecount is None:
             self.stripecount = int(config('web', 'stripes', 1))
         mapfile = templater.stylemap(style)
-        tmpl = templater.templater(mapfile, templatefilters.filters,
+        tmpl = templater.templater(mapfile,
                                    defaults={"header": header,
                                              "footer": footer,
                                              "motd": motd,
diff -r 4439625da15a -r e5273a918210 mercurial/templatefilters.py
--- a/mercurial/templatefilters.py	Mon Apr 06 15:09:54 2009 +0200
+++ b/mercurial/templatefilters.py	Mon Apr 06 16:28:30 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),
@@ -181,7 +187,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),
diff -r 4439625da15a -r e5273a918210 mercurial/templater.py
--- a/mercurial/templater.py	Mon Apr 06 15:09:54 2009 +0200
+++ b/mercurial/templater.py	Mon Apr 06 16:28:30 2009 +0200
@@ -7,8 +7,9 @@
 
 from i18n import _
 import re, sys, os
-from mercurial import util
+from mercurial import util, templatefilters
 
+stringify = templatefilters.stringify
 path = ['templates', '../templates']
 
 def parsestring(s, quoted=True):
@@ -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
 
@@ -213,9 +215,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)


More information about the Mercurial-devel mailing list