# HG changeset patch # User Aleix Conchillo Flaque # Date 2009-04-24 16:37:44 # Node ID 1bef3656d9fef609814be7b48102eebd7deee674 # Parent 77c5877a668ce75a52f01220e6a30c3409168a62 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 --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py --- a/mercurial/templatefilters.py +++ b/mercurial/templatefilters.py @@ -157,9 +157,18 @@ def json(obj): 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,