templater: sort functions alphabetically, as filters are

This commit is contained in:
Alexander Plavin 2013-06-29 14:27:53 +04:00
parent 8225ea1d54
commit 66771f0e45
2 changed files with 75 additions and 75 deletions

View File

@ -58,11 +58,11 @@ In addition to filters, there are some basic built-in functions:
- label(label, expr)
- sub(pat, repl, expr)
- rstdoc(text, style)
- strip(text, chars)
- strip(text[, chars])
- sub(pat, repl, expr)
Also, for any expression that returns a list, there is a list operator:

View File

@ -205,6 +205,41 @@ def buildfunc(exp, context):
f = context._filters[n]
return (runfilter, (args[0][0], args[0][1], f))
def date(context, mapping, args):
if not (1 <= len(args) <= 2):
raise error.ParseError(_("date expects one or two arguments"))
date = args[0][0](context, mapping, args[0][1])
if len(args) == 2:
fmt = stringify(args[1][0](context, mapping, args[1][1]))
return util.datestr(date, fmt)
return util.datestr(date)
def fill(context, mapping, args):
if not (1 <= len(args) <= 4):
raise error.ParseError(_("fill expects one to four arguments"))
text = stringify(args[0][0](context, mapping, args[0][1]))
width = 76
initindent = ''
hangindent = ''
if 2 <= len(args) <= 4:
try:
width = int(stringify(args[1][0](context, mapping, args[1][1])))
except ValueError:
raise error.ParseError(_("fill expects an integer width"))
try:
initindent = stringify(args[2][0](context, mapping, args[2][1]))
initindent = stringify(runtemplate(context, mapping,
compiletemplate(initindent, context)))
hangindent = stringify(args[3][0](context, mapping, args[3][1]))
hangindent = stringify(runtemplate(context, mapping,
compiletemplate(hangindent, context)))
except IndexError:
pass
return templatefilters.fill(text, width, initindent, hangindent)
def get(context, mapping, args):
if len(args) != 2:
# i18n: "get" is a keyword
@ -218,40 +253,6 @@ def get(context, mapping, args):
key = args[1][0](context, mapping, args[1][1])
yield dictarg.get(key)
def join(context, mapping, args):
if not (1 <= len(args) <= 2):
# i18n: "join" is a keyword
raise error.ParseError(_("join expects one or two arguments"))
joinset = args[0][0](context, mapping, args[0][1])
if util.safehasattr(joinset, '__call__'):
jf = joinset.joinfmt
joinset = [jf(x) for x in joinset()]
joiner = " "
if len(args) > 1:
joiner = args[1][0](context, mapping, args[1][1])
first = True
for x in joinset:
if first:
first = False
else:
yield joiner
yield x
def sub(context, mapping, args):
if len(args) != 3:
# i18n: "sub" is a keyword
raise error.ParseError(_("sub expects three arguments"))
pat = stringify(args[0][0](context, mapping, args[0][1]))
rpl = stringify(args[1][0](context, mapping, args[1][1]))
src = stringify(args[2][0](context, mapping, args[2][1]))
src = stringify(runtemplate(context, mapping,
compiletemplate(src, context)))
yield re.sub(pat, rpl, src)
def if_(context, mapping, args):
if not (2 <= len(args) <= 3):
# i18n: "if" is a keyword
@ -279,6 +280,28 @@ def ifeq(context, mapping, args):
t = stringify(args[3][0](context, mapping, args[3][1]))
yield runtemplate(context, mapping, compiletemplate(t, context))
def join(context, mapping, args):
if not (1 <= len(args) <= 2):
# i18n: "join" is a keyword
raise error.ParseError(_("join expects one or two arguments"))
joinset = args[0][0](context, mapping, args[0][1])
if util.safehasattr(joinset, '__call__'):
jf = joinset.joinfmt
joinset = [jf(x) for x in joinset()]
joiner = " "
if len(args) > 1:
joiner = args[1][0](context, mapping, args[1][1])
first = True
for x in joinset:
if first:
first = False
else:
yield joiner
yield x
def label(context, mapping, args):
if len(args) != 2:
# i18n: "label" is a keyword
@ -298,41 +321,6 @@ def rstdoc(context, mapping, args):
return minirst.format(text, style=style, keep=['verbose'])
def fill(context, mapping, args):
if not (1 <= len(args) <= 4):
raise error.ParseError(_("fill expects one to four arguments"))
text = stringify(args[0][0](context, mapping, args[0][1]))
width = 76
initindent = ''
hangindent = ''
if 2 <= len(args) <= 4:
try:
width = int(stringify(args[1][0](context, mapping, args[1][1])))
except ValueError:
raise error.ParseError(_("fill expects an integer width"))
try:
initindent = stringify(args[2][0](context, mapping, args[2][1]))
initindent = stringify(runtemplate(context, mapping,
compiletemplate(initindent, context)))
hangindent = stringify(args[3][0](context, mapping, args[3][1]))
hangindent = stringify(runtemplate(context, mapping,
compiletemplate(hangindent, context)))
except IndexError:
pass
return templatefilters.fill(text, width, initindent, hangindent)
def date(context, mapping, args):
if not (1 <= len(args) <= 2):
raise error.ParseError(_("date expects one or two arguments"))
date = args[0][0](context, mapping, args[0][1])
if len(args) == 2:
fmt = stringify(args[1][0](context, mapping, args[1][1]))
return util.datestr(date, fmt)
return util.datestr(date)
def strip(context, mapping, args):
if not (1 <= len(args) <= 2):
raise error.ParseError(_("strip expects one or two arguments"))
@ -343,6 +331,18 @@ def strip(context, mapping, args):
return text.strip(chars)
return text.strip()
def sub(context, mapping, args):
if len(args) != 3:
# i18n: "sub" is a keyword
raise error.ParseError(_("sub expects three arguments"))
pat = stringify(args[0][0](context, mapping, args[0][1]))
rpl = stringify(args[1][0](context, mapping, args[1][1]))
src = stringify(args[2][0](context, mapping, args[2][1]))
src = stringify(runtemplate(context, mapping,
compiletemplate(src, context)))
yield re.sub(pat, rpl, src)
methods = {
"string": lambda e, c: (runstring, e[1]),
"symbol": lambda e, c: (runsymbol, e[1]),
@ -354,16 +354,16 @@ methods = {
}
funcs = {
"date": date,
"fill": fill,
"get": get,
"if": if_,
"ifeq": ifeq,
"join": join,
"label": label,
"rstdoc": rstdoc,
"sub": sub,
"fill": fill,
"date": date,
"strip": strip,
"sub": sub,
}
# template engine