##// END OF EJS Templates
templater: introduce word function...
Ryan McElroy -
r21846:8f23f809 default
parent child Browse files
Show More
@@ -72,6 +72,8 b' In addition to filters, there are some b'
72
72
73 - sub(pat, repl, expr)
73 - sub(pat, repl, expr)
74
74
75 - word(number, text[, separator])
76
75 Also, for any expression that returns a list, there is a list operator:
77 Also, for any expression that returns a list, there is a list operator:
76
78
77 - expr % "{template}"
79 - expr % "{template}"
@@ -130,3 +132,7 b' Some sample command line templates:'
130 - Show only commit descriptions that start with "template"::
132 - Show only commit descriptions that start with "template"::
131
133
132 $ hg log --template "{startswith(\"template\", firstline(desc))}\n"
134 $ hg log --template "{startswith(\"template\", firstline(desc))}\n"
135
136 - Print the first word of each line of a commit message::
137
138 $ hg log --template "{word(\"0\", desc)}\n"
@@ -477,6 +477,25 b' def startswith(context, mapping, args):'
477 return ''
477 return ''
478
478
479
479
480 def word(context, mapping, args):
481 """return nth word from a string"""
482 if not (2 <= len(args) <= 3):
483 raise error.ParseError(_("word expects two or three arguments, got %d")
484 % len(args))
485
486 num = int(stringify(args[0][0](context, mapping, args[0][1])))
487 text = stringify(args[1][0](context, mapping, args[1][1]))
488 if len(args) == 3:
489 splitter = stringify(args[2][0](context, mapping, args[2][1]))
490 else:
491 splitter = None
492
493 tokens = text.split(splitter)
494 if num >= len(tokens):
495 return ''
496 else:
497 return tokens[num]
498
480 methods = {
499 methods = {
481 "string": lambda e, c: (runstring, e[1]),
500 "string": lambda e, c: (runstring, e[1]),
482 "rawstring": lambda e, c: (runrawstring, e[1]),
501 "rawstring": lambda e, c: (runrawstring, e[1]),
@@ -504,6 +523,7 b' funcs = {'
504 "startswith": startswith,
523 "startswith": startswith,
505 "strip": strip,
524 "strip": strip,
506 "sub": sub,
525 "sub": sub,
526 "word": word,
507 }
527 }
508
528
509 # template engine
529 # template engine
@@ -1917,3 +1917,61 b' Test bad template with better error mess'
1917 $ hg log -Gv -R a --template '{desc|user()}'
1917 $ hg log -Gv -R a --template '{desc|user()}'
1918 hg: parse error: expected a symbol, got 'func'
1918 hg: parse error: expected a symbol, got 'func'
1919 [255]
1919 [255]
1920
1921 Test word function (including index out of bounds graceful failure)
1922
1923 $ hg log -Gv -R a --template "{word('1', desc)}"
1924 @
1925 |
1926 o
1927 |
1928 o
1929
1930 o
1931 |\
1932 | o head
1933 | |
1934 o | branch
1935 |/
1936 o user,
1937 |
1938 o person
1939 |
1940 o 1
1941 |
1942 o 1
1943
1944
1945 Test word third parameter used as splitter
1946
1947 $ hg log -Gv -R a --template "{word('0', desc, 'o')}"
1948 @ future
1949 |
1950 o third
1951 |
1952 o sec
1953
1954 o merge
1955 |\
1956 | o new head
1957 | |
1958 o | new branch
1959 |/
1960 o n
1961 |
1962 o n
1963 |
1964 o
1965 |
1966 o line 1
1967 line 2
1968
1969 Test word error messages for not enough and too many arguments
1970
1971 $ hg log -Gv -R a --template "{word('0')}"
1972 hg: parse error: word expects two or three arguments, got 1
1973 [255]
1974
1975 $ hg log -Gv -R a --template "{word('0', desc, 'o', 'h', 'b', 'o', 'y')}"
1976 hg: parse error: word expects two or three arguments, got 7
1977 [255]
General Comments 0
You need to be logged in to leave comments. Login now