changeset 13590:1a752dcfe062

templatefilters: wrap all filters in dedicated functions This will highly simplify the docstring integration. I measured "hg log --style=changelog" duration on mercurial itself and could not detect any difference.
author Patrick Mezard <pmezard@gmail.com>
date Sat, 12 Mar 2011 12:46:31 +0100
parents b0a4b05c25e2
children 264f292a0c6f
files mercurial/templatefilters.py
diffstat 1 files changed, 80 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatefilters.py	Sat Mar 12 12:46:31 2011 +0100
+++ b/mercurial/templatefilters.py	Sat Mar 12 12:46:31 2011 +0100
@@ -44,6 +44,12 @@
         if n >= 2 or s == 1:
             return '%s ago' % fmt(t, n)
 
+def basename(path):
+    return os.path.basename(path)
+
+def datefilter(text):
+    return util.datestr(text)
+
 def domain(author):
     '''get domain of author, or empty string if none.'''
     f = author.find('@')
@@ -55,6 +61,12 @@
         author = author[:f]
     return author
 
+def email(text):
+    return util.email(text)
+
+def escape(text):
+    return cgi.escape(text, True)
+
 para_re = None
 space_re = None
 
@@ -83,6 +95,12 @@
     return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest
                     for para, rest in findparas()])
 
+def fill68(text):
+    return fill(text, 68)
+
+def fill76(text):
+    return fill(text, 76)
+
 def firstline(text):
     '''return the first line of text'''
     try:
@@ -90,6 +108,18 @@
     except IndexError:
         return ''
 
+def hexfilter(text):
+    return node.hex(text)
+
+def hgdate(text):
+    return "%d %d" % text
+
+def isodate(text):
+    return util.datestr(text, '%Y-%m-%d %H:%M %1%2')
+
+def isodatesec(text):
+    return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2')
+
 def indent(text, prefix):
     '''indent each non-empty line of text after first with prefix.'''
     lines = text.splitlines()
@@ -145,6 +175,9 @@
         s = s.replace(k, v)
     return ''.join(_uescape(c) for c in s)
 
+def localdate(text):
+    return (text[0], util.makedate()[1])
+
 def nonempty(str):
     return str or "(none)"
 
@@ -168,12 +201,30 @@
         return util.shortuser(author)
     return author[:f].rstrip()
 
+def rfc3339date(text):
+    return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2")
+
+def rfc822date(text):
+    return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2")
+
+def short(text):
+    return text[:12]
+
+def shortdate(text):
+    return util.shortdate(text)
+
+def stringescape(text):
+    return text.encode('string_escape')
+
 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)
 
+def strip(text):
+    return text.strip()
+
 def stripdir(text):
     '''Treat the text as path and strip a directory level, if possible.'''
     dir = os.path.dirname(text)
@@ -182,6 +233,15 @@
     else:
         return dir
 
+def tabindent(text):
+    return indent(text, '\t')
+
+def urlescape(text):
+    return urllib.quote(text)
+
+def userfilter(text):
+    return util.shortuser(text)
+
 def xmlescape(text):
     text = (text
             .replace('&', '&amp;')
@@ -194,35 +254,35 @@
 filters = {
     "addbreaks": addbreaks,
     "age": age,
-    "basename": os.path.basename,
-    "date": lambda x: util.datestr(x),
+    "basename": basename,
+    "date": datefilter,
     "domain": domain,
-    "email": util.email,
-    "escape": lambda x: cgi.escape(x, True),
-    "fill68": lambda x: fill(x, width=68),
-    "fill76": lambda x: fill(x, width=76),
+    "email": email,
+    "escape": escape,
+    "fill68": fill68,
+    "fill76": fill76,
     "firstline": firstline,
-    "hex": node.hex,
-    "hgdate": lambda x: "%d %d" % x,
-    "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'),
-    "isodatesec": lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2'),
+    "hex": hexfilter,
+    "hgdate": hgdate,
+    "isodate": isodate,
+    "isodatesec": isodatesec,
     "json": json,
     "jsonescape": jsonescape,
-    "localdate": lambda x: (x[0], util.makedate()[1]),
+    "localdate": localdate,
     "nonempty": nonempty,
     "obfuscate": obfuscate,
     "permissions": permissions,
     "person": person,
-    "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"),
-    "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"),
-    "short": lambda x: x[:12],
-    "shortdate": util.shortdate,
-    "stringescape": lambda x: x.encode('string_escape'),
+    "rfc3339date": rfc3339date,
+    "rfc822date": rfc822date,
+    "short": short,
+    "shortdate": shortdate,
+    "stringescape": stringescape,
     "stringify": stringify,
-    "strip": lambda x: x.strip(),
+    "strip": strip,
     "stripdir": stripdir,
-    "tabindent": lambda x: indent(x, '\t'),
-    "urlescape": lambda x: urllib.quote(x),
-    "user": lambda x: util.shortuser(x),
+    "tabindent": tabindent,
+    "urlescape": urlescape,
+    "user": userfilter,
     "xmlescape": xmlescape,
 }