[PATCH] templatefilters: add new stripdir filter

Aleix Conchillo Flaque aleix at member.fsf.org
Fri Apr 24 11:38:36 CDT 2009


# HG changeset patch
# User Aleix Conchillo Flaque <aleix at member.fsf.org>
# Date 1240591064 -7200
# Node ID 8ab31977e1e8f987c1bfece16e66dd8ea7b6ce3e
# Parent  ddbee2d0d634b67d7f6ebea405398c171bbf3a8a
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'

diff -r ddbee2d0d634 -r 8ab31977e1e8 mercurial/templatefilters.py
--- a/mercurial/templatefilters.py	Fri Apr 24 10:43:12 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,


More information about the Mercurial-devel mailing list