##// END OF EJS Templates
templater: allow dynamically switching the default dict/list formatting...
Yuya Nishihara -
r36651:034a07e6 default
parent child Browse files
Show More
@@ -176,10 +176,10 b' class baseformatter(object):'
176 def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'):
176 def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'):
177 '''convert date tuple to appropriate format'''
177 '''convert date tuple to appropriate format'''
178 return self._converter.formatdate(date, fmt)
178 return self._converter.formatdate(date, fmt)
179 def formatdict(self, data, key='key', value='value', fmt='%s=%s', sep=' '):
179 def formatdict(self, data, key='key', value='value', fmt=None, sep=' '):
180 '''convert dict or key-value pairs to appropriate dict format'''
180 '''convert dict or key-value pairs to appropriate dict format'''
181 return self._converter.formatdict(data, key, value, fmt, sep)
181 return self._converter.formatdict(data, key, value, fmt, sep)
182 def formatlist(self, data, name, fmt='%s', sep=' '):
182 def formatlist(self, data, name, fmt=None, sep=' '):
183 '''convert iterable to appropriate list format'''
183 '''convert iterable to appropriate list format'''
184 # name is mandatory argument for now, but it could be optional if
184 # name is mandatory argument for now, but it could be optional if
185 # we have default template keyword, e.g. {item}
185 # we have default template keyword, e.g. {item}
@@ -248,10 +248,14 b' class _plainconverter(object):'
248 @staticmethod
248 @staticmethod
249 def formatdict(data, key, value, fmt, sep):
249 def formatdict(data, key, value, fmt, sep):
250 '''stringify key-value pairs separated by sep'''
250 '''stringify key-value pairs separated by sep'''
251 if fmt is None:
252 fmt = '%s=%s'
251 return sep.join(fmt % (k, v) for k, v in _iteritems(data))
253 return sep.join(fmt % (k, v) for k, v in _iteritems(data))
252 @staticmethod
254 @staticmethod
253 def formatlist(data, name, fmt, sep):
255 def formatlist(data, name, fmt, sep):
254 '''stringify iterable separated by sep'''
256 '''stringify iterable separated by sep'''
257 if fmt is None:
258 fmt = '%s'
255 return sep.join(fmt % e for e in data)
259 return sep.join(fmt % e for e in data)
256
260
257 class plainformatter(baseformatter):
261 class plainformatter(baseformatter):
@@ -97,13 +97,17 b' class _mappable(object):'
97 def itermaps(self):
97 def itermaps(self):
98 yield self.tomap()
98 yield self.tomap()
99
99
100 def hybriddict(data, key='key', value='value', fmt='%s=%s', gen=None):
100 def hybriddict(data, key='key', value='value', fmt=None, gen=None):
101 """Wrap data to support both dict-like and string-like operations"""
101 """Wrap data to support both dict-like and string-like operations"""
102 if fmt is None:
103 fmt = '%s=%s'
102 return _hybrid(gen, data, lambda k: {key: k, value: data[k]},
104 return _hybrid(gen, data, lambda k: {key: k, value: data[k]},
103 lambda k: fmt % (k, data[k]))
105 lambda k: fmt % (k, data[k]))
104
106
105 def hybridlist(data, name, fmt='%s', gen=None):
107 def hybridlist(data, name, fmt=None, gen=None):
106 """Wrap data to support both list-like and string-like operations"""
108 """Wrap data to support both list-like and string-like operations"""
109 if fmt is None:
110 fmt = '%s'
107 return _hybrid(gen, data, lambda x: {name: x}, lambda x: fmt % x)
111 return _hybrid(gen, data, lambda x: {name: x}, lambda x: fmt % x)
108
112
109 def unwraphybrid(thing):
113 def unwraphybrid(thing):
@@ -137,7 +141,7 b' def wraphybridvalue(container, key, valu'
137 return _mappable(None, key, value, makemap)
141 return _mappable(None, key, value, makemap)
138
142
139 def compatdict(context, mapping, name, data, key='key', value='value',
143 def compatdict(context, mapping, name, data, key='key', value='value',
140 fmt='%s=%s', plural=None, separator=' '):
144 fmt=None, plural=None, separator=' '):
141 """Wrap data like hybriddict(), but also supports old-style list template
145 """Wrap data like hybriddict(), but also supports old-style list template
142
146
143 This exists for backward compatibility with the old-style template. Use
147 This exists for backward compatibility with the old-style template. Use
@@ -148,7 +152,7 b' def compatdict(context, mapping, name, d'
148 f = _showlist(name, c, t, mapping, plural, separator)
152 f = _showlist(name, c, t, mapping, plural, separator)
149 return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
153 return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
150
154
151 def compatlist(context, mapping, name, data, element=None, fmt='%s',
155 def compatlist(context, mapping, name, data, element=None, fmt=None,
152 plural=None, separator=' '):
156 plural=None, separator=' '):
153 """Wrap data like hybridlist(), but also supports old-style list template
157 """Wrap data like hybridlist(), but also supports old-style list template
154
158
@@ -160,7 +164,7 b' def compatlist(context, mapping, name, d'
160 return hybridlist(data, name=element or name, fmt=fmt, gen=f)
164 return hybridlist(data, name=element or name, fmt=fmt, gen=f)
161
165
162 def showdict(name, data, mapping, plural=None, key='key', value='value',
166 def showdict(name, data, mapping, plural=None, key='key', value='value',
163 fmt='%s=%s', separator=' '):
167 fmt=None, separator=' '):
164 ui = mapping.get('ui')
168 ui = mapping.get('ui')
165 if ui:
169 if ui:
166 ui.deprecwarn("templatekw.showdict() is deprecated, use compatdict()",
170 ui.deprecwarn("templatekw.showdict() is deprecated, use compatdict()",
General Comments 0
You need to be logged in to leave comments. Login now