Mercurial > hg > mercurial-source
comparison mercurial/templater.py @ 19390:3af3a165db18
templater: sort functions alphabetically, as filters are
author | Alexander Plavin <me@aplavin.ru> |
---|---|
date | Sat, 29 Jun 2013 14:27:53 +0400 |
parents | 867b9957d895 |
children | 0361163efbaf |
comparison
equal
deleted
inserted
replaced
19389:41c4bdd1d585 | 19390:3af3a165db18 |
---|---|
203 if len(args) != 1: | 203 if len(args) != 1: |
204 raise error.ParseError(_("filter %s expects one argument") % n) | 204 raise error.ParseError(_("filter %s expects one argument") % n) |
205 f = context._filters[n] | 205 f = context._filters[n] |
206 return (runfilter, (args[0][0], args[0][1], f)) | 206 return (runfilter, (args[0][0], args[0][1], f)) |
207 | 207 |
208 def get(context, mapping, args): | 208 def date(context, mapping, args): |
209 if len(args) != 2: | |
210 # i18n: "get" is a keyword | |
211 raise error.ParseError(_("get() expects two arguments")) | |
212 | |
213 dictarg = args[0][0](context, mapping, args[0][1]) | |
214 if not util.safehasattr(dictarg, 'get'): | |
215 # i18n: "get" is a keyword | |
216 raise error.ParseError(_("get() expects a dict as first argument")) | |
217 | |
218 key = args[1][0](context, mapping, args[1][1]) | |
219 yield dictarg.get(key) | |
220 | |
221 def join(context, mapping, args): | |
222 if not (1 <= len(args) <= 2): | 209 if not (1 <= len(args) <= 2): |
223 # i18n: "join" is a keyword | 210 raise error.ParseError(_("date expects one or two arguments")) |
224 raise error.ParseError(_("join expects one or two arguments")) | 211 |
225 | 212 date = args[0][0](context, mapping, args[0][1]) |
226 joinset = args[0][0](context, mapping, args[0][1]) | 213 if len(args) == 2: |
227 if util.safehasattr(joinset, '__call__'): | 214 fmt = stringify(args[1][0](context, mapping, args[1][1])) |
228 jf = joinset.joinfmt | 215 return util.datestr(date, fmt) |
229 joinset = [jf(x) for x in joinset()] | 216 return util.datestr(date) |
230 | |
231 joiner = " " | |
232 if len(args) > 1: | |
233 joiner = args[1][0](context, mapping, args[1][1]) | |
234 | |
235 first = True | |
236 for x in joinset: | |
237 if first: | |
238 first = False | |
239 else: | |
240 yield joiner | |
241 yield x | |
242 | |
243 def sub(context, mapping, args): | |
244 if len(args) != 3: | |
245 # i18n: "sub" is a keyword | |
246 raise error.ParseError(_("sub expects three arguments")) | |
247 | |
248 pat = stringify(args[0][0](context, mapping, args[0][1])) | |
249 rpl = stringify(args[1][0](context, mapping, args[1][1])) | |
250 src = stringify(args[2][0](context, mapping, args[2][1])) | |
251 src = stringify(runtemplate(context, mapping, | |
252 compiletemplate(src, context))) | |
253 yield re.sub(pat, rpl, src) | |
254 | |
255 def if_(context, mapping, args): | |
256 if not (2 <= len(args) <= 3): | |
257 # i18n: "if" is a keyword | |
258 raise error.ParseError(_("if expects two or three arguments")) | |
259 | |
260 test = stringify(args[0][0](context, mapping, args[0][1])) | |
261 if test: | |
262 t = stringify(args[1][0](context, mapping, args[1][1])) | |
263 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
264 elif len(args) == 3: | |
265 t = stringify(args[2][0](context, mapping, args[2][1])) | |
266 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
267 | |
268 def ifeq(context, mapping, args): | |
269 if not (3 <= len(args) <= 4): | |
270 # i18n: "ifeq" is a keyword | |
271 raise error.ParseError(_("ifeq expects three or four arguments")) | |
272 | |
273 test = stringify(args[0][0](context, mapping, args[0][1])) | |
274 match = stringify(args[1][0](context, mapping, args[1][1])) | |
275 if test == match: | |
276 t = stringify(args[2][0](context, mapping, args[2][1])) | |
277 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
278 elif len(args) == 4: | |
279 t = stringify(args[3][0](context, mapping, args[3][1])) | |
280 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
281 | |
282 def label(context, mapping, args): | |
283 if len(args) != 2: | |
284 # i18n: "label" is a keyword | |
285 raise error.ParseError(_("label expects two arguments")) | |
286 | |
287 # ignore args[0] (the label string) since this is supposed to be a a no-op | |
288 t = stringify(args[1][0](context, mapping, args[1][1])) | |
289 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
290 | |
291 def rstdoc(context, mapping, args): | |
292 if len(args) != 2: | |
293 # i18n: "rstdoc" is a keyword | |
294 raise error.ParseError(_("rstdoc expects two arguments")) | |
295 | |
296 text = stringify(args[0][0](context, mapping, args[0][1])) | |
297 style = stringify(args[1][0](context, mapping, args[1][1])) | |
298 | |
299 return minirst.format(text, style=style, keep=['verbose']) | |
300 | 217 |
301 def fill(context, mapping, args): | 218 def fill(context, mapping, args): |
302 if not (1 <= len(args) <= 4): | 219 if not (1 <= len(args) <= 4): |
303 raise error.ParseError(_("fill expects one to four arguments")) | 220 raise error.ParseError(_("fill expects one to four arguments")) |
304 | 221 |
321 except IndexError: | 238 except IndexError: |
322 pass | 239 pass |
323 | 240 |
324 return templatefilters.fill(text, width, initindent, hangindent) | 241 return templatefilters.fill(text, width, initindent, hangindent) |
325 | 242 |
326 def date(context, mapping, args): | 243 def get(context, mapping, args): |
244 if len(args) != 2: | |
245 # i18n: "get" is a keyword | |
246 raise error.ParseError(_("get() expects two arguments")) | |
247 | |
248 dictarg = args[0][0](context, mapping, args[0][1]) | |
249 if not util.safehasattr(dictarg, 'get'): | |
250 # i18n: "get" is a keyword | |
251 raise error.ParseError(_("get() expects a dict as first argument")) | |
252 | |
253 key = args[1][0](context, mapping, args[1][1]) | |
254 yield dictarg.get(key) | |
255 | |
256 def if_(context, mapping, args): | |
257 if not (2 <= len(args) <= 3): | |
258 # i18n: "if" is a keyword | |
259 raise error.ParseError(_("if expects two or three arguments")) | |
260 | |
261 test = stringify(args[0][0](context, mapping, args[0][1])) | |
262 if test: | |
263 t = stringify(args[1][0](context, mapping, args[1][1])) | |
264 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
265 elif len(args) == 3: | |
266 t = stringify(args[2][0](context, mapping, args[2][1])) | |
267 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
268 | |
269 def ifeq(context, mapping, args): | |
270 if not (3 <= len(args) <= 4): | |
271 # i18n: "ifeq" is a keyword | |
272 raise error.ParseError(_("ifeq expects three or four arguments")) | |
273 | |
274 test = stringify(args[0][0](context, mapping, args[0][1])) | |
275 match = stringify(args[1][0](context, mapping, args[1][1])) | |
276 if test == match: | |
277 t = stringify(args[2][0](context, mapping, args[2][1])) | |
278 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
279 elif len(args) == 4: | |
280 t = stringify(args[3][0](context, mapping, args[3][1])) | |
281 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
282 | |
283 def join(context, mapping, args): | |
327 if not (1 <= len(args) <= 2): | 284 if not (1 <= len(args) <= 2): |
328 raise error.ParseError(_("date expects one or two arguments")) | 285 # i18n: "join" is a keyword |
329 | 286 raise error.ParseError(_("join expects one or two arguments")) |
330 date = args[0][0](context, mapping, args[0][1]) | 287 |
331 if len(args) == 2: | 288 joinset = args[0][0](context, mapping, args[0][1]) |
332 fmt = stringify(args[1][0](context, mapping, args[1][1])) | 289 if util.safehasattr(joinset, '__call__'): |
333 return util.datestr(date, fmt) | 290 jf = joinset.joinfmt |
334 return util.datestr(date) | 291 joinset = [jf(x) for x in joinset()] |
292 | |
293 joiner = " " | |
294 if len(args) > 1: | |
295 joiner = args[1][0](context, mapping, args[1][1]) | |
296 | |
297 first = True | |
298 for x in joinset: | |
299 if first: | |
300 first = False | |
301 else: | |
302 yield joiner | |
303 yield x | |
304 | |
305 def label(context, mapping, args): | |
306 if len(args) != 2: | |
307 # i18n: "label" is a keyword | |
308 raise error.ParseError(_("label expects two arguments")) | |
309 | |
310 # ignore args[0] (the label string) since this is supposed to be a a no-op | |
311 t = stringify(args[1][0](context, mapping, args[1][1])) | |
312 yield runtemplate(context, mapping, compiletemplate(t, context)) | |
313 | |
314 def rstdoc(context, mapping, args): | |
315 if len(args) != 2: | |
316 # i18n: "rstdoc" is a keyword | |
317 raise error.ParseError(_("rstdoc expects two arguments")) | |
318 | |
319 text = stringify(args[0][0](context, mapping, args[0][1])) | |
320 style = stringify(args[1][0](context, mapping, args[1][1])) | |
321 | |
322 return minirst.format(text, style=style, keep=['verbose']) | |
335 | 323 |
336 def strip(context, mapping, args): | 324 def strip(context, mapping, args): |
337 if not (1 <= len(args) <= 2): | 325 if not (1 <= len(args) <= 2): |
338 raise error.ParseError(_("strip expects one or two arguments")) | 326 raise error.ParseError(_("strip expects one or two arguments")) |
339 | 327 |
340 text = args[0][0](context, mapping, args[0][1]) | 328 text = args[0][0](context, mapping, args[0][1]) |
341 if len(args) == 2: | 329 if len(args) == 2: |
342 chars = args[1][0](context, mapping, args[1][1]) | 330 chars = args[1][0](context, mapping, args[1][1]) |
343 return text.strip(chars) | 331 return text.strip(chars) |
344 return text.strip() | 332 return text.strip() |
333 | |
334 def sub(context, mapping, args): | |
335 if len(args) != 3: | |
336 # i18n: "sub" is a keyword | |
337 raise error.ParseError(_("sub expects three arguments")) | |
338 | |
339 pat = stringify(args[0][0](context, mapping, args[0][1])) | |
340 rpl = stringify(args[1][0](context, mapping, args[1][1])) | |
341 src = stringify(args[2][0](context, mapping, args[2][1])) | |
342 src = stringify(runtemplate(context, mapping, | |
343 compiletemplate(src, context))) | |
344 yield re.sub(pat, rpl, src) | |
345 | 345 |
346 methods = { | 346 methods = { |
347 "string": lambda e, c: (runstring, e[1]), | 347 "string": lambda e, c: (runstring, e[1]), |
348 "symbol": lambda e, c: (runsymbol, e[1]), | 348 "symbol": lambda e, c: (runsymbol, e[1]), |
349 "group": lambda e, c: compileexp(e[1], c), | 349 "group": lambda e, c: compileexp(e[1], c), |
352 "%": buildmap, | 352 "%": buildmap, |
353 "func": buildfunc, | 353 "func": buildfunc, |
354 } | 354 } |
355 | 355 |
356 funcs = { | 356 funcs = { |
357 "date": date, | |
358 "fill": fill, | |
357 "get": get, | 359 "get": get, |
358 "if": if_, | 360 "if": if_, |
359 "ifeq": ifeq, | 361 "ifeq": ifeq, |
360 "join": join, | 362 "join": join, |
361 "label": label, | 363 "label": label, |
362 "rstdoc": rstdoc, | 364 "rstdoc": rstdoc, |
365 "strip": strip, | |
363 "sub": sub, | 366 "sub": sub, |
364 "fill": fill, | |
365 "date": date, | |
366 "strip": strip, | |
367 } | 367 } |
368 | 368 |
369 # template engine | 369 # template engine |
370 | 370 |
371 path = ['templates', '../templates'] | 371 path = ['templates', '../templates'] |