From patchwork Fri Dec 21 05:37:53 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [03,of,10,RFC] templatefilter: add labelify and delabelify functions From: Sean Farley X-Patchwork-Id: 238 Message-Id: <19bea005cd095a819418.1356068273@laptop.local> Date: Thu, 20 Dec 2012 23:37:53 -0600 # HG changeset patch # User Sean Farley # Date 1355961231 21600 # Node ID 19bea005cd095a81941820efcf5b924cc354d293 # Parent 812607049cddf4653985ed96ecf9ef9c8c7dab78 templatefilter: add labelify and delabelify functions There are many directions that we could go here and I need some guidance as to which one to follow. The route taken in this patch series is to wrap the value in a dictionary (e.g. {data: "foo", label: "log.user"}). The repercussions of this are that _flatten, _showlist, and a few other pieces of code must be taught to not clobber the labeled data. The upside to this model is that it's relatively simple to wrap data with a label. Alternatives could be: - Using recursive templates similar to how {files} expand. This would, I think, be very fragile because later filters could potentially truncate the label data (e.g. {'node|short'}) - Using a custom class? - Using something else more fancy? diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py --- a/mercurial/templatefilters.py +++ b/mercurial/templatefilters.py @@ -420,5 +420,33 @@ "date": datefunc, } # tell hggettext to extract docstrings from these functions: i18nfunctions = filters.values() + +# should this be a class? +def _islabel(thing): + return isinstance(thing, dict) and 'data' in thing and 'label' in thing + +def labelify(thing, label=''): + if thing is None: + return None + + if label is None: + label = '' + + if _islabel(thing): + if thing['label'] not in label: + label += ' ' + thing['label'] + thing = thing['data'] + + label = label.strip() + + return {'data':thing, 'label':label} + +def delabelify(thing, label=''): + thing = labelify(thing, label) + if _islabel(thing): + label = thing['label'] + thing = thing['data'] + + return thing, label