changeset 8158:1bef3656d9fe

templatefilters: add new stripdir filter Adds a new template filter for removing directory levels from a string. Examples: {foo|stripdir} -> 'foo' {foo/bar|stripdir} -> 'foo' {foo/bar/more|stripdir} -> 'foo/bar' {foo/bar/more|stripdir|stripdir} -> 'foo'
author Aleix Conchillo Flaque <aleix@member.fsf.org>
date Fri, 24 Apr 2009 18:37:44 +0200
parents 77c5877a668c
children 19f22977e635
files mercurial/templatefilters.py
diffstat 1 files changed, 9 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatefilters.py	Fri Apr 24 18:47:15 2009 +0200
+++ b/mercurial/templatefilters.py	Fri Apr 24 18:37:44 2009 +0200
@@ -157,9 +157,18 @@
     else:
         raise TypeError('cannot encode type %s' % obj.__class__.__name__)
 
+def stripdir(text):
+    '''Treat the text as path and strip a directory level, if possible.'''
+    dir = os.path.dirname(text)
+    if dir == "":
+        return os.path.basename(text)
+    else:
+        return dir
+
 filters = {
     "addbreaks": nl2br,
     "basename": os.path.basename,
+    "stripdir": stripdir,
     "age": age,
     "date": lambda x: util.datestr(x),
     "domain": domain,