mercurial/templater.py
changeset 25597 fd5bc660c9f0
parent 25596 c1975809a6b5
child 25598 55c2cb65bdfa
equal deleted inserted replaced
25596:c1975809a6b5 25597:fd5bc660c9f0
   144     return context._filters[f]
   144     return context._filters[f]
   145 
   145 
   146 def gettemplate(exp, context):
   146 def gettemplate(exp, context):
   147     if exp[0] == 'template':
   147     if exp[0] == 'template':
   148         return compiletemplate(exp[1], context)
   148         return compiletemplate(exp[1], context)
   149     if exp[0] == 'rawstring':
       
   150         return compiletemplate(exp[1], context, strtoken=exp[0])
       
   151     if exp[0] == 'symbol':
   149     if exp[0] == 'symbol':
   152         return context._load(exp[1])
   150         return context._load(exp[1])
   153     raise error.ParseError(_("expected template specifier"))
   151     raise error.ParseError(_("expected template specifier"))
   154 
   152 
   155 def runinteger(context, mapping, data):
   153 def runinteger(context, mapping, data):
   300             width = int(stringify(args[1][0](context, mapping, args[1][1])))
   298             width = int(stringify(args[1][0](context, mapping, args[1][1])))
   301         except ValueError:
   299         except ValueError:
   302             # i18n: "fill" is a keyword
   300             # i18n: "fill" is a keyword
   303             raise error.ParseError(_("fill expects an integer width"))
   301             raise error.ParseError(_("fill expects an integer width"))
   304         try:
   302         try:
   305             initindent = stringify(_evalifliteral(args[2], context, mapping))
   303             initindent = stringify(args[2][0](context, mapping, args[2][1]))
   306             hangindent = stringify(_evalifliteral(args[3], context, mapping))
   304             hangindent = stringify(args[3][0](context, mapping, args[3][1]))
   307         except IndexError:
   305         except IndexError:
   308             pass
   306             pass
   309 
   307 
   310     return templatefilters.fill(text, width, initindent, hangindent)
   308     return templatefilters.fill(text, width, initindent, hangindent)
   311 
   309 
   316         # i18n: "pad" is a keyword
   314         # i18n: "pad" is a keyword
   317         raise error.ParseError(_("pad() expects two to four arguments"))
   315         raise error.ParseError(_("pad() expects two to four arguments"))
   318 
   316 
   319     width = int(args[1][1])
   317     width = int(args[1][1])
   320 
   318 
   321     text = stringify(_evalifliteral(args[0], context, mapping))
   319     text = stringify(args[0][0](context, mapping, args[0][1]))
   322 
   320 
   323     right = False
   321     right = False
   324     fillchar = ' '
   322     fillchar = ' '
   325     if len(args) > 2:
   323     if len(args) > 2:
   326         fillchar = stringify(args[2][0](context, mapping, args[2][1]))
   324         fillchar = stringify(args[2][0](context, mapping, args[2][1]))
   366         raise error.ParseError(_("get() expects a dict as first argument"))
   364         raise error.ParseError(_("get() expects a dict as first argument"))
   367 
   365 
   368     key = args[1][0](context, mapping, args[1][1])
   366     key = args[1][0](context, mapping, args[1][1])
   369     yield dictarg.get(key)
   367     yield dictarg.get(key)
   370 
   368 
   371 def _evalifliteral(arg, context, mapping):
       
   372     # get back to token tag to reinterpret string as template
       
   373     strtoken = {runrawstring: 'rawstring'}.get(arg[0])
       
   374     if strtoken:
       
   375         yield runtemplate(context, mapping,
       
   376                           compiletemplate(arg[1], context, strtoken))
       
   377     else:
       
   378         yield stringify(arg[0](context, mapping, arg[1]))
       
   379 
       
   380 def if_(context, mapping, args):
   369 def if_(context, mapping, args):
   381     """:if(expr, then[, else]): Conditionally execute based on the result of
   370     """:if(expr, then[, else]): Conditionally execute based on the result of
   382     an expression."""
   371     an expression."""
   383     if not (2 <= len(args) <= 3):
   372     if not (2 <= len(args) <= 3):
   384         # i18n: "if" is a keyword
   373         # i18n: "if" is a keyword
   385         raise error.ParseError(_("if expects two or three arguments"))
   374         raise error.ParseError(_("if expects two or three arguments"))
   386 
   375 
   387     test = stringify(args[0][0](context, mapping, args[0][1]))
   376     test = stringify(args[0][0](context, mapping, args[0][1]))
   388     if test:
   377     if test:
   389         yield _evalifliteral(args[1], context, mapping)
   378         yield args[1][0](context, mapping, args[1][1])
   390     elif len(args) == 3:
   379     elif len(args) == 3:
   391         yield _evalifliteral(args[2], context, mapping)
   380         yield args[2][0](context, mapping, args[2][1])
   392 
   381 
   393 def ifcontains(context, mapping, args):
   382 def ifcontains(context, mapping, args):
   394     """:ifcontains(search, thing, then[, else]): Conditionally execute based
   383     """:ifcontains(search, thing, then[, else]): Conditionally execute based
   395     on whether the item "search" is in "thing"."""
   384     on whether the item "search" is in "thing"."""
   396     if not (3 <= len(args) <= 4):
   385     if not (3 <= len(args) <= 4):
   399 
   388 
   400     item = stringify(args[0][0](context, mapping, args[0][1]))
   389     item = stringify(args[0][0](context, mapping, args[0][1]))
   401     items = args[1][0](context, mapping, args[1][1])
   390     items = args[1][0](context, mapping, args[1][1])
   402 
   391 
   403     if item in items:
   392     if item in items:
   404         yield _evalifliteral(args[2], context, mapping)
   393         yield args[2][0](context, mapping, args[2][1])
   405     elif len(args) == 4:
   394     elif len(args) == 4:
   406         yield _evalifliteral(args[3], context, mapping)
   395         yield args[3][0](context, mapping, args[3][1])
   407 
   396 
   408 def ifeq(context, mapping, args):
   397 def ifeq(context, mapping, args):
   409     """:ifeq(expr1, expr2, then[, else]): Conditionally execute based on
   398     """:ifeq(expr1, expr2, then[, else]): Conditionally execute based on
   410     whether 2 items are equivalent."""
   399     whether 2 items are equivalent."""
   411     if not (3 <= len(args) <= 4):
   400     if not (3 <= len(args) <= 4):
   413         raise error.ParseError(_("ifeq expects three or four arguments"))
   402         raise error.ParseError(_("ifeq expects three or four arguments"))
   414 
   403 
   415     test = stringify(args[0][0](context, mapping, args[0][1]))
   404     test = stringify(args[0][0](context, mapping, args[0][1]))
   416     match = stringify(args[1][0](context, mapping, args[1][1]))
   405     match = stringify(args[1][0](context, mapping, args[1][1]))
   417     if test == match:
   406     if test == match:
   418         yield _evalifliteral(args[2], context, mapping)
   407         yield args[2][0](context, mapping, args[2][1])
   419     elif len(args) == 4:
   408     elif len(args) == 4:
   420         yield _evalifliteral(args[3], context, mapping)
   409         yield args[3][0](context, mapping, args[3][1])
   421 
   410 
   422 def join(context, mapping, args):
   411 def join(context, mapping, args):
   423     """:join(list, sep): Join items in a list with a delimiter."""
   412     """:join(list, sep): Join items in a list with a delimiter."""
   424     if not (1 <= len(args) <= 2):
   413     if not (1 <= len(args) <= 2):
   425         # i18n: "join" is a keyword
   414         # i18n: "join" is a keyword
   449     if len(args) != 2:
   438     if len(args) != 2:
   450         # i18n: "label" is a keyword
   439         # i18n: "label" is a keyword
   451         raise error.ParseError(_("label expects two arguments"))
   440         raise error.ParseError(_("label expects two arguments"))
   452 
   441 
   453     # ignore args[0] (the label string) since this is supposed to be a a no-op
   442     # ignore args[0] (the label string) since this is supposed to be a a no-op
   454     yield _evalifliteral(args[1], context, mapping)
   443     yield args[1][0](context, mapping, args[1][1])
   455 
   444 
   456 def revset(context, mapping, args):
   445 def revset(context, mapping, args):
   457     """:revset(query[, formatargs...]): Execute a revision set query. See
   446     """:revset(query[, formatargs...]): Execute a revision set query. See
   458     :hg:`help revset`."""
   447     :hg:`help revset`."""
   459     if not len(args) > 0:
   448     if not len(args) > 0:
   565         # i18n: "sub" is a keyword
   554         # i18n: "sub" is a keyword
   566         raise error.ParseError(_("sub expects three arguments"))
   555         raise error.ParseError(_("sub expects three arguments"))
   567 
   556 
   568     pat = stringify(args[0][0](context, mapping, args[0][1]))
   557     pat = stringify(args[0][0](context, mapping, args[0][1]))
   569     rpl = stringify(args[1][0](context, mapping, args[1][1]))
   558     rpl = stringify(args[1][0](context, mapping, args[1][1]))
   570     src = stringify(_evalifliteral(args[2], context, mapping))
   559     src = stringify(args[2][0](context, mapping, args[2][1]))
   571     yield re.sub(pat, rpl, src)
   560     yield re.sub(pat, rpl, src)
   572 
   561 
   573 def startswith(context, mapping, args):
   562 def startswith(context, mapping, args):
   574     """:startswith(pattern, text): Returns the value from the "text" argument
   563     """:startswith(pattern, text): Returns the value from the "text" argument
   575     if it begins with the content from the "pattern" argument."""
   564     if it begins with the content from the "pattern" argument."""