annotate mercurial/templatefuncs.py @ 39122:5d3b58472660

filemerge: set actual capabilities of internal merge tools This information is used to detect actual capabilities of internal merge tools by subsequent patches. For convenience, this patch assumes that merge tools typed as "nomerge" have both binary files and symlinks capabilities.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Tue, 14 Aug 2018 20:08:27 +0900
parents 3588e41f796d
children a2a5d4ad5276
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36922
521f6c7e1756 templater: split template functions to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
1 # templatefuncs.py - common template functions
1909
37b9f80a5fbb add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1906
diff changeset
2 #
37b9f80a5fbb add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1906
diff changeset
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
37b9f80a5fbb add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1906
diff changeset
4 #
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8223
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9842
diff changeset
6 # GNU General Public License version 2 or any later version.
1909
37b9f80a5fbb add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1906
diff changeset
7
36922
521f6c7e1756 templater: split template functions to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
8 from __future__ import absolute_import
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
9
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
10 import re
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
11
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
12 from .i18n import _
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
13 from .node import (
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
14 bin,
37859
66dc9db6ed2c shortest: make {shortest("fffffffff")} work again
Martin von Zweigbergk <martinvonz@google.com>
parents: 37709
diff changeset
15 wdirid,
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
16 )
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
17 from . import (
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31520
diff changeset
18 color,
31520
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
19 encoding,
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
20 error,
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
21 minirst,
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
22 obsutil,
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
23 registrar,
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
24 revset as revsetmod,
31024
0b8356705de6 revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30625
diff changeset
25 revsetlang,
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
26 scmutil,
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
27 templatefilters,
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
28 templatekw,
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
29 templateutil,
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
30 util,
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
31 )
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
32 from .utils import (
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
33 dateutil,
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
34 stringutil,
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
35 )
13176
895f54a79c6e templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents: 13175
diff changeset
36
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
37 evalrawexp = templateutil.evalrawexp
38227
1c8098cf560a templater: always join() over a wrapped object (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38225
diff changeset
38 evalwrapped = templateutil.evalwrapped
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
39 evalfuncarg = templateutil.evalfuncarg
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
40 evalboolean = templateutil.evalboolean
37224
67efce231633 templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents: 37210
diff changeset
41 evaldate = templateutil.evaldate
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
42 evalinteger = templateutil.evalinteger
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
43 evalstring = templateutil.evalstring
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
44 evalstringliteral = templateutil.evalstringliteral
36443
8dbd97aef915 templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents: 36245
diff changeset
45
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
46 # dict of template built-in functions
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
47 funcs = {}
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
48 templatefunc = registrar.templatefunc(funcs)
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
49
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
50 @templatefunc('date(date[, fmt])')
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
51 def date(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
52 """Format a date. See :hg:`help dates` for formatting
26106
c568c4db036f templatefilters: remove redundant 'date' and 'strip' filters
Yuya Nishihara <yuya@tcha.org>
parents: 26105
diff changeset
53 strings. The default is a Unix date format, including the timezone:
c568c4db036f templatefilters: remove redundant 'date' and 'strip' filters
Yuya Nishihara <yuya@tcha.org>
parents: 26105
diff changeset
54 "Mon Sep 04 15:13:13 2006 0700"."""
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
55 if not (1 <= len(args) <= 2):
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
56 # i18n: "date" is a keyword
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
57 raise error.ParseError(_("date expects one or two arguments"))
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
58
37225
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
59 date = evaldate(context, mapping, args[0],
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
60 # i18n: "date" is a keyword
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
61 _("date expects a date information"))
24903
09124cce913f templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents: 24886
diff changeset
62 fmt = None
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
63 if len(args) == 2:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
64 fmt = evalstring(context, mapping, args[1])
37225
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
65 if fmt is None:
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
66 return dateutil.datestr(date)
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
67 else:
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
68 return dateutil.datestr(date, fmt)
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
69
31928
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
70 @templatefunc('dict([[key=]value...])', argspec='*args **kwargs')
31926
932241b8c644 templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents: 31922
diff changeset
71 def dict_(context, mapping, args):
31928
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
72 """Construct a dict from key-value pairs. A key may be omitted if
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
73 a value expression can provide an unambiguous name."""
31926
932241b8c644 templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents: 31922
diff changeset
74 data = util.sortdict()
31928
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
75
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
76 for v in args['args']:
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
77 k = templateutil.findsymbolicname(v)
31928
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
78 if not k:
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
79 raise error.ParseError(_('dict key cannot be inferred'))
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
80 if k in data or k in args['kwargs']:
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
81 raise error.ParseError(_("duplicated dict key '%s' inferred") % k)
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
82 data[k] = evalfuncarg(context, mapping, v)
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
83
31926
932241b8c644 templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents: 31922
diff changeset
84 data.update((k, evalfuncarg(context, mapping, v))
932241b8c644 templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents: 31922
diff changeset
85 for k, v in args['kwargs'].iteritems())
36921
32f9b7e3f056 templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents: 36920
diff changeset
86 return templateutil.hybriddict(data)
31926
932241b8c644 templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents: 31922
diff changeset
87
38428
aa98392eb5b0 templatefuncs: declare resource requirements for future use
Yuya Nishihara <yuya@tcha.org>
parents: 38427
diff changeset
88 @templatefunc('diff([includepattern [, excludepattern]])', requires={'ctx'})
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
89 def diff(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
90 """Show a diff, optionally
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
91 specifying files to include or exclude."""
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
92 if len(args) > 2:
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
93 # i18n: "diff" is a keyword
27293
9e06e7fb037d grammar: favor zero, one, two over ... or no
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
94 raise error.ParseError(_("diff expects zero, one, or two arguments"))
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
95
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
96 def getpatterns(i):
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
97 if i < len(args):
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
98 s = evalstring(context, mapping, args[i]).strip()
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
99 if s:
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
100 return [s]
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
101 return []
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
102
35467
d6cfa722b044 templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents: 35414
diff changeset
103 ctx = context.resource(mapping, 'ctx')
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
104 chunks = ctx.diff(match=ctx.match([], getpatterns(0), getpatterns(1)))
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
105
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
106 return ''.join(chunks)
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
107
38428
aa98392eb5b0 templatefuncs: declare resource requirements for future use
Yuya Nishihara <yuya@tcha.org>
parents: 38427
diff changeset
108 @templatefunc('extdata(source)', argspec='source', requires={'ctx', 'cache'})
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
109 def extdata(context, mapping, args):
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
110 """Show a text read from the specified extdata source. (EXPERIMENTAL)"""
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
111 if 'source' not in args:
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
112 # i18n: "extdata" is a keyword
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
113 raise error.ParseError(_('extdata expects one argument'))
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
114
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
115 source = evalstring(context, mapping, args['source'])
37931
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
116 if not source:
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
117 sym = templateutil.findsymbolicname(args['source'])
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
118 if sym:
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
119 raise error.ParseError(_('empty data source specified'),
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
120 hint=_("did you mean extdata('%s')?") % sym)
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
121 else:
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
122 raise error.ParseError(_('empty data source specified'))
35467
d6cfa722b044 templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents: 35414
diff changeset
123 cache = context.resource(mapping, 'cache').setdefault('extdata', {})
d6cfa722b044 templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents: 35414
diff changeset
124 ctx = context.resource(mapping, 'ctx')
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
125 if source in cache:
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
126 data = cache[source]
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
127 else:
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
128 data = cache[source] = scmutil.extdatasource(ctx.repo(), source)
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
129 return data.get(ctx.rev(), '')
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
130
38428
aa98392eb5b0 templatefuncs: declare resource requirements for future use
Yuya Nishihara <yuya@tcha.org>
parents: 38427
diff changeset
131 @templatefunc('files(pattern)', requires={'ctx'})
30008
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
132 def files(context, mapping, args):
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
133 """All files of the current changeset matching the pattern. See
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
134 :hg:`help patterns`."""
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
135 if not len(args) == 1:
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
136 # i18n: "files" is a keyword
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
137 raise error.ParseError(_("files expects one argument"))
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
138
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
139 raw = evalstring(context, mapping, args[0])
35467
d6cfa722b044 templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents: 35414
diff changeset
140 ctx = context.resource(mapping, 'ctx')
30008
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
141 m = ctx.match([raw])
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
142 files = list(ctx.matches(m))
36921
32f9b7e3f056 templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents: 36920
diff changeset
143 return templateutil.compatlist(context, mapping, "file", files)
30008
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
144
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
145 @templatefunc('fill(text[, width[, initialident[, hangindent]]])')
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
146 def fill(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
147 """Fill many
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
148 paragraphs with optional indentation. See the "fill" filter."""
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
149 if not (1 <= len(args) <= 4):
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
150 # i18n: "fill" is a keyword
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
151 raise error.ParseError(_("fill expects one to four arguments"))
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
152
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
153 text = evalstring(context, mapping, args[0])
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
154 width = 76
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
155 initindent = ''
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
156 hangindent = ''
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
157 if 2 <= len(args) <= 4:
28343
a6c2310b3827 templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents: 28334
diff changeset
158 width = evalinteger(context, mapping, args[1],
a6c2310b3827 templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents: 28334
diff changeset
159 # i18n: "fill" is a keyword
a6c2310b3827 templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents: 28334
diff changeset
160 _("fill expects an integer width"))
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
161 try:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
162 initindent = evalstring(context, mapping, args[2])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
163 hangindent = evalstring(context, mapping, args[3])
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
164 except IndexError:
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
165 pass
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
166
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
167 return templatefilters.fill(text, width, initindent, hangindent)
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
168
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
169 @templatefunc('filter(iterable[, expr])')
38448
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
170 def filter_(context, mapping, args):
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
171 """Remove empty elements from a list or a dict. If expr specified, it's
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
172 applied to each element to test emptiness."""
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
173 if not (1 <= len(args) <= 2):
38448
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
174 # i18n: "filter" is a keyword
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
175 raise error.ParseError(_("filter expects one or two arguments"))
38448
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
176 iterable = evalwrapped(context, mapping, args[0])
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
177 if len(args) == 1:
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
178 def select(w):
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
179 return w.tobool(context, mapping)
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
180 else:
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
181 def select(w):
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
182 if not isinstance(w, templateutil.mappable):
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
183 raise error.ParseError(_("not filterable by expression"))
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
184 lm = context.overlaymap(mapping, w.tomap(context))
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
185 return evalboolean(context, lm, args[1])
38448
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
186 return iterable.filter(context, mapping, select)
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
187
38428
aa98392eb5b0 templatefuncs: declare resource requirements for future use
Yuya Nishihara <yuya@tcha.org>
parents: 38427
diff changeset
188 @templatefunc('formatnode(node)', requires={'ui'})
31169
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
189 def formatnode(context, mapping, args):
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
190 """Obtain the preferred form of a changeset hash. (DEPRECATED)"""
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
191 if len(args) != 1:
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
192 # i18n: "formatnode" is a keyword
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
193 raise error.ParseError(_("formatnode expects one argument"))
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
194
35467
d6cfa722b044 templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents: 35414
diff changeset
195 ui = context.resource(mapping, 'ui')
31169
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
196 node = evalstring(context, mapping, args[0])
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
197 if ui.debugflag:
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
198 return node
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
199 return templatefilters.short(node)
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
200
38428
aa98392eb5b0 templatefuncs: declare resource requirements for future use
Yuya Nishihara <yuya@tcha.org>
parents: 38427
diff changeset
201 @templatefunc('mailmap(author)', requires={'repo', 'cache'})
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
202 def mailmap(context, mapping, args):
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
203 """Return the author, updated according to the value
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
204 set in the .mailmap file"""
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
205 if len(args) != 1:
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
206 raise error.ParseError(_("mailmap expects one argument"))
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
207
37260
8e57c3b0dce4 templatefuncs: do not crash because of invalid value fed to mailmap()
Yuya Nishihara <yuya@tcha.org>
parents: 37244
diff changeset
208 author = evalstring(context, mapping, args[0])
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
209
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
210 cache = context.resource(mapping, 'cache')
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
211 repo = context.resource(mapping, 'repo')
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
212
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
213 if 'mailmap' not in cache:
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
214 data = repo.wvfs.tryread('.mailmap')
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
215 cache['mailmap'] = stringutil.parsemailmap(data)
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
216
37244
3685a79ea51b templatefuncs: remove redundant "or author" from mailmap return statement
Connor Sheehan <sheehan@mozilla.com>
parents: 37225
diff changeset
217 return stringutil.mapname(cache['mailmap'], author)
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
218
31887
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
219 @templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])',
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
220 argspec='text width fillchar left')
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
221 def pad(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
222 """Pad text with a
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
223 fill character."""
31887
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
224 if 'text' not in args or 'width' not in args:
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
225 # i18n: "pad" is a keyword
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
226 raise error.ParseError(_("pad() expects two to four arguments"))
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
227
31887
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
228 width = evalinteger(context, mapping, args['width'],
28345
d81437c91a26 templater: fix pad() to evaluate int argument and handle error
Yuya Nishihara <yuya@tcha.org>
parents: 28344
diff changeset
229 # i18n: "pad" is a keyword
d81437c91a26 templater: fix pad() to evaluate int argument and handle error
Yuya Nishihara <yuya@tcha.org>
parents: 28344
diff changeset
230 _("pad() expects an integer width"))
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
231
31887
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
232 text = evalstring(context, mapping, args['text'])
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
233
29818
407879b0893b templater: rename "right" argument of pad() function
Yuya Nishihara <yuya@tcha.org>
parents: 29817
diff changeset
234 left = False
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
235 fillchar = ' '
31887
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
236 if 'fillchar' in args:
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
237 fillchar = evalstring(context, mapping, args['fillchar'])
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31520
diff changeset
238 if len(color.stripeffects(fillchar)) != 1:
31519
3725986b151a templater: reject bad fillchar argument passed to pad()
Yuya Nishihara <yuya@tcha.org>
parents: 31169
diff changeset
239 # i18n: "pad" is a keyword
3725986b151a templater: reject bad fillchar argument passed to pad()
Yuya Nishihara <yuya@tcha.org>
parents: 31169
diff changeset
240 raise error.ParseError(_("pad() expects a single fill character"))
31887
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
241 if 'left' in args:
f7b3677f66cd templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31886
diff changeset
242 left = evalboolean(context, mapping, args['left'])
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
243
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31520
diff changeset
244 fillwidth = width - encoding.colwidth(color.stripeffects(text))
31520
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
245 if fillwidth <= 0:
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
246 return text
29818
407879b0893b templater: rename "right" argument of pad() function
Yuya Nishihara <yuya@tcha.org>
parents: 29817
diff changeset
247 if left:
31520
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
248 return fillchar * fillwidth + text
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
249 else:
31520
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
250 return text + fillchar * fillwidth
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
251
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
252 @templatefunc('indent(text, indentchars[, firstline])')
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
253 def indent(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
254 """Indents all non-empty lines
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
255 with the characters given in the indentchars string. An optional
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
256 third parameter will override the indent for the first line only
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
257 if present."""
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
258 if not (2 <= len(args) <= 3):
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
259 # i18n: "indent" is a keyword
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
260 raise error.ParseError(_("indent() expects two or three arguments"))
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
261
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
262 text = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
263 indent = evalstring(context, mapping, args[1])
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
264
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
265 if len(args) == 3:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
266 firstline = evalstring(context, mapping, args[2])
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
267 else:
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
268 firstline = indent
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
269
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
270 # the indent function doesn't indent the first line, so we do it here
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
271 return templatefilters.indent(firstline + text, indent)
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
272
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
273 @templatefunc('get(dict, key)')
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
274 def get(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
275 """Get an attribute/key from an object. Some keywords
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
276 are complex types. This function allows you to obtain the value of an
26197
fb6c08a9b40a templater: fix get English
timeless@mozdev.org
parents: 26188
diff changeset
277 attribute on these types."""
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
278 if len(args) != 2:
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
279 # i18n: "get" is a keyword
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
280 raise error.ParseError(_("get() expects two arguments"))
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
281
38240
c2456a7726c1 templater: do dict lookup over a wrapped object
Yuya Nishihara <yuya@tcha.org>
parents: 38239
diff changeset
282 dictarg = evalwrapped(context, mapping, args[0])
38244
688fbb758ba9 templater: resolve type of dict key in getmember()
Yuya Nishihara <yuya@tcha.org>
parents: 38243
diff changeset
283 key = evalrawexp(context, mapping, args[1])
38243
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
284 try:
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
285 return dictarg.getmember(context, mapping, key)
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
286 except error.ParseError as err:
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
287 # i18n: "get" is a keyword
38243
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
288 hint = _("get() expects a dict as first argument")
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
289 raise error.ParseError(bytes(err), hint=hint)
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
290
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
291 @templatefunc('if(expr, then[, else])')
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
292 def if_(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
293 """Conditionally execute based on the result of
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
294 an expression."""
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
295 if not (2 <= len(args) <= 3):
17890
ca6850b9dd9e i18n: add "i18n" comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17729
diff changeset
296 # i18n: "if" is a keyword
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
297 raise error.ParseError(_("if expects two or three arguments"))
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
298
29816
034412ca28c3 templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents: 29815
diff changeset
299 test = evalboolean(context, mapping, args[0])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
300 if test:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
301 return evalrawexp(context, mapping, args[1])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
302 elif len(args) == 3:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
303 return evalrawexp(context, mapping, args[2])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
304
30049
f18cc848b48e templater: use "needle" and "haystack" as (meta-)variables for ifcontains()
Anton Shestakov <av6@dwimlabs.net>
parents: 30008
diff changeset
305 @templatefunc('ifcontains(needle, haystack, then[, else])')
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
306 def ifcontains(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
307 """Conditionally execute based
30049
f18cc848b48e templater: use "needle" and "haystack" as (meta-)variables for ifcontains()
Anton Shestakov <av6@dwimlabs.net>
parents: 30008
diff changeset
308 on whether the item "needle" is in "haystack"."""
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
309 if not (3 <= len(args) <= 4):
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
310 # i18n: "ifcontains" is a keyword
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
311 raise error.ParseError(_("ifcontains expects three or four arguments"))
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
312
38267
fb874fc1d9b4 templater: abstract ifcontains() over wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38265
diff changeset
313 haystack = evalwrapped(context, mapping, args[1])
34659
3edfd472f3cb templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents: 34581
diff changeset
314 try:
37163
0fb28899e81a templater: factor out unwrapastype() from evalastype()
Yuya Nishihara <yuya@tcha.org>
parents: 37015
diff changeset
315 needle = evalrawexp(context, mapping, args[0])
38267
fb874fc1d9b4 templater: abstract ifcontains() over wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38265
diff changeset
316 found = haystack.contains(context, mapping, needle)
34659
3edfd472f3cb templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents: 34581
diff changeset
317 except error.ParseError:
3edfd472f3cb templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents: 34581
diff changeset
318 found = False
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
319
34659
3edfd472f3cb templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents: 34581
diff changeset
320 if found:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
321 return evalrawexp(context, mapping, args[2])
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
322 elif len(args) == 4:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
323 return evalrawexp(context, mapping, args[3])
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
324
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
325 @templatefunc('ifeq(expr1, expr2, then[, else])')
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
326 def ifeq(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
327 """Conditionally execute based on
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
328 whether 2 items are equivalent."""
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
329 if not (3 <= len(args) <= 4):
17890
ca6850b9dd9e i18n: add "i18n" comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17729
diff changeset
330 # i18n: "ifeq" is a keyword
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
331 raise error.ParseError(_("ifeq expects three or four arguments"))
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
332
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
333 test = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
334 match = evalstring(context, mapping, args[1])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
335 if test == match:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
336 return evalrawexp(context, mapping, args[2])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
337 elif len(args) == 4:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
338 return evalrawexp(context, mapping, args[3])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
339
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
340 @templatefunc('join(list, sep)')
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
341 def join(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
342 """Join items in a list with a delimiter."""
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
343 if not (1 <= len(args) <= 2):
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
344 # i18n: "join" is a keyword
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
345 raise error.ParseError(_("join expects one or two arguments"))
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
346
38227
1c8098cf560a templater: always join() over a wrapped object (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38225
diff changeset
347 joinset = evalwrapped(context, mapping, args[0])
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
348 joiner = " "
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
349 if len(args) > 1:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
350 joiner = evalstring(context, mapping, args[1])
38227
1c8098cf560a templater: always join() over a wrapped object (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38225
diff changeset
351 return joinset.join(context, mapping, joiner)
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
352
38428
aa98392eb5b0 templatefuncs: declare resource requirements for future use
Yuya Nishihara <yuya@tcha.org>
parents: 38427
diff changeset
353 @templatefunc('label(label, expr)', requires={'ui'})
18289
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
354 def label(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
355 """Apply a label to generated content. Content with
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
356 a label applied can result in additional post-processing, such as
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
357 automatic colorization."""
18289
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
358 if len(args) != 2:
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
359 # i18n: "label" is a keyword
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
360 raise error.ParseError(_("label expects two arguments"))
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
361
35467
d6cfa722b044 templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents: 35414
diff changeset
362 ui = context.resource(mapping, 'ui')
28374
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
363 thing = evalstring(context, mapping, args[1])
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
364 # preserve unknown symbol as literal so effects like 'red', 'bold',
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
365 # etc. don't need to be quoted
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
366 label = evalstringliteral(context, mapping, args[0])
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
367
28384
3356bf61fa25 formatter: make labels work with templated output
Kostia Balytskyi <ikostia@fb.com>
parents: 28374
diff changeset
368 return ui.label(thing, label)
18289
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
369
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
370 @templatefunc('latesttag([pattern])')
26485
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
371 def latesttag(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
372 """The global tags matching the given pattern on the
31850
f0d719e513fc templatekw: clarify the result of {latesttag} when no tag exists
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
373 most recent globally tagged ancestor of this changeset.
f0d719e513fc templatekw: clarify the result of {latesttag} when no tag exists
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
374 If no such tags exist, the "{tag}" template resolves to
38144
bd7a3fa71a72 help: mention pattern syntax of latesttag() template function
Yuya Nishihara <yuya@tcha.org>
parents: 37931
diff changeset
375 the string "null". See :hg:`help revisions.patterns` for the pattern
bd7a3fa71a72 help: mention pattern syntax of latesttag() template function
Yuya Nishihara <yuya@tcha.org>
parents: 37931
diff changeset
376 syntax.
bd7a3fa71a72 help: mention pattern syntax of latesttag() template function
Yuya Nishihara <yuya@tcha.org>
parents: 37931
diff changeset
377 """
26485
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
378 if len(args) > 1:
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
379 # i18n: "latesttag" is a keyword
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
380 raise error.ParseError(_("latesttag expects at most one argument"))
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
381
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
382 pattern = None
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
383 if len(args) == 1:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
384 pattern = evalstring(context, mapping, args[0])
36596
b5d39a09656a templatekw: switch latesttags template keywords to new API
Yuya Nishihara <yuya@tcha.org>
parents: 36595
diff changeset
385 return templatekw.showlatesttags(context, mapping, pattern)
26485
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
386
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
387 @templatefunc('localdate(date[, tz])')
26127
7012be5ab5bd templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents: 26125
diff changeset
388 def localdate(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
389 """Converts a date to the specified timezone.
26128
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
390 The default is local date."""
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
391 if not (1 <= len(args) <= 2):
26127
7012be5ab5bd templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents: 26125
diff changeset
392 # i18n: "localdate" is a keyword
26128
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
393 raise error.ParseError(_("localdate expects one or two arguments"))
26127
7012be5ab5bd templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents: 26125
diff changeset
394
37224
67efce231633 templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents: 37210
diff changeset
395 date = evaldate(context, mapping, args[0],
67efce231633 templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents: 37210
diff changeset
396 # i18n: "localdate" is a keyword
67efce231633 templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents: 37210
diff changeset
397 _("localdate expects a date information"))
26128
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
398 if len(args) >= 2:
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
399 tzoffset = None
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
400 tz = evalfuncarg(context, mapping, args[1])
36550
7f6be7121b28 py3: replace type 'str' by 'bytes' in templater.py
Yuya Nishihara <yuya@tcha.org>
parents: 36546
diff changeset
401 if isinstance(tz, bytes):
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36596
diff changeset
402 tzoffset, remainder = dateutil.parsetimezone(tz)
29636
84ef4517de03 date: refactor timezone parsing
Matt Mackall <mpm@selenic.com>
parents: 29085
diff changeset
403 if remainder:
84ef4517de03 date: refactor timezone parsing
Matt Mackall <mpm@selenic.com>
parents: 29085
diff changeset
404 tzoffset = None
26128
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
405 if tzoffset is None:
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
406 try:
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
407 tzoffset = int(tz)
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
408 except (TypeError, ValueError):
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
409 # i18n: "localdate" is a keyword
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
410 raise error.ParseError(_("localdate expects a timezone"))
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
411 else:
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36596
diff changeset
412 tzoffset = dateutil.makedate()[1]
38285
8d6109b49b31 templater: introduce a wrapper for date tuple (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38267
diff changeset
413 return templateutil.date((date[0], tzoffset))
26127
7012be5ab5bd templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents: 26125
diff changeset
414
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
415 @templatefunc('max(iterable)')
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
416 def max_(context, mapping, args, **kwargs):
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
417 """Return the max of an iterable"""
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
418 if len(args) != 1:
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
419 # i18n: "max" is a keyword
35386
fdd09d87635b templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents: 34838
diff changeset
420 raise error.ParseError(_("max expects one argument"))
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
421
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
422 iterable = evalwrapped(context, mapping, args[0])
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
423 try:
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
424 return iterable.getmax(context, mapping)
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
425 except error.ParseError as err:
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
426 # i18n: "max" is a keyword
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
427 hint = _("max first argument should be an iterable")
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
428 raise error.ParseError(bytes(err), hint=hint)
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
429
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
430 @templatefunc('min(iterable)')
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
431 def min_(context, mapping, args, **kwargs):
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
432 """Return the min of an iterable"""
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
433 if len(args) != 1:
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
434 # i18n: "min" is a keyword
35386
fdd09d87635b templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents: 34838
diff changeset
435 raise error.ParseError(_("min expects one argument"))
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
436
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
437 iterable = evalwrapped(context, mapping, args[0])
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
438 try:
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
439 return iterable.getmin(context, mapping)
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
440 except error.ParseError as err:
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
441 # i18n: "min" is a keyword
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
442 hint = _("min first argument should be an iterable")
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
443 raise error.ParseError(bytes(err), hint=hint)
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
444
30115
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
445 @templatefunc('mod(a, b)')
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
446 def mod(context, mapping, args):
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
447 """Calculate a mod b such that a / b + a mod b == a"""
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
448 if not len(args) == 2:
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
449 # i18n: "mod" is a keyword
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
450 raise error.ParseError(_("mod expects two arguments"))
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
451
30116
1c01fa29630f templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents: 30115
diff changeset
452 func = lambda a, b: a % b
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
453 return templateutil.runarithmetic(context, mapping,
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
454 (func, args[0], args[1]))
30115
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
455
34287
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
456 @templatefunc('obsfateoperations(markers)')
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
457 def obsfateoperations(context, mapping, args):
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
458 """Compute obsfate related information based on markers (EXPERIMENTAL)"""
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
459 if len(args) != 1:
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
460 # i18n: "obsfateoperations" is a keyword
35386
fdd09d87635b templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents: 34838
diff changeset
461 raise error.ParseError(_("obsfateoperations expects one argument"))
34287
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
462
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
463 markers = evalfuncarg(context, mapping, args[0])
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
464
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
465 try:
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
466 data = obsutil.markersoperations(markers)
36921
32f9b7e3f056 templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents: 36920
diff changeset
467 return templateutil.hybridlist(data, name='operation')
34287
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
468 except (TypeError, KeyError):
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
469 # i18n: "obsfateoperations" is a keyword
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
470 errmsg = _("obsfateoperations first argument should be an iterable")
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
471 raise error.ParseError(errmsg)
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
472
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
473 @templatefunc('obsfatedate(markers)')
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
474 def obsfatedate(context, mapping, args):
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
475 """Compute obsfate related information based on markers (EXPERIMENTAL)"""
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
476 if len(args) != 1:
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
477 # i18n: "obsfatedate" is a keyword
35386
fdd09d87635b templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents: 34838
diff changeset
478 raise error.ParseError(_("obsfatedate expects one argument"))
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
479
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
480 markers = evalfuncarg(context, mapping, args[0])
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
481
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
482 try:
38285
8d6109b49b31 templater: introduce a wrapper for date tuple (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38267
diff changeset
483 # TODO: maybe this has to be a wrapped list of date wrappers?
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
484 data = obsutil.markersdates(markers)
36921
32f9b7e3f056 templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents: 36920
diff changeset
485 return templateutil.hybridlist(data, name='date', fmt='%d %d')
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
486 except (TypeError, KeyError):
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
487 # i18n: "obsfatedate" is a keyword
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
488 errmsg = _("obsfatedate first argument should be an iterable")
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
489 raise error.ParseError(errmsg)
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
490
33994
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
491 @templatefunc('obsfateusers(markers)')
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
492 def obsfateusers(context, mapping, args):
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
493 """Compute obsfate related information based on markers (EXPERIMENTAL)"""
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
494 if len(args) != 1:
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
495 # i18n: "obsfateusers" is a keyword
35386
fdd09d87635b templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents: 34838
diff changeset
496 raise error.ParseError(_("obsfateusers expects one argument"))
33994
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
497
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
498 markers = evalfuncarg(context, mapping, args[0])
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
499
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
500 try:
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
501 data = obsutil.markersusers(markers)
36921
32f9b7e3f056 templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents: 36920
diff changeset
502 return templateutil.hybridlist(data, name='user')
33994
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
503 except (TypeError, KeyError, ValueError):
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
504 # i18n: "obsfateusers" is a keyword
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
505 msg = _("obsfateusers first argument should be an iterable of "
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
506 "obsmakers")
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
507 raise error.ParseError(msg)
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
508
35010
b81ad5b78a81 obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents: 34838
diff changeset
509 @templatefunc('obsfateverb(successors, markers)')
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
510 def obsfateverb(context, mapping, args):
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
511 """Compute obsfate related information based on successors (EXPERIMENTAL)"""
35010
b81ad5b78a81 obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents: 34838
diff changeset
512 if len(args) != 2:
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
513 # i18n: "obsfateverb" is a keyword
35010
b81ad5b78a81 obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents: 34838
diff changeset
514 raise error.ParseError(_("obsfateverb expects two arguments"))
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
515
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
516 successors = evalfuncarg(context, mapping, args[0])
35010
b81ad5b78a81 obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents: 34838
diff changeset
517 markers = evalfuncarg(context, mapping, args[1])
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
518
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
519 try:
35010
b81ad5b78a81 obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents: 34838
diff changeset
520 return obsutil.obsfateverb(successors, markers)
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
521 except TypeError:
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
522 # i18n: "obsfateverb" is a keyword
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
523 errmsg = _("obsfateverb first argument should be countable")
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
524 raise error.ParseError(errmsg)
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
525
38428
aa98392eb5b0 templatefuncs: declare resource requirements for future use
Yuya Nishihara <yuya@tcha.org>
parents: 38427
diff changeset
526 @templatefunc('relpath(path)', requires={'repo'})
30083
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
527 def relpath(context, mapping, args):
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
528 """Convert a repository-absolute path into a filesystem path relative to
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
529 the current working directory."""
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
530 if len(args) != 1:
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
531 # i18n: "relpath" is a keyword
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
532 raise error.ParseError(_("relpath expects one argument"))
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
533
38427
4b73f316ba0e templatefuncs: minimize resource requirements
Yuya Nishihara <yuya@tcha.org>
parents: 38285
diff changeset
534 repo = context.resource(mapping, 'repo')
30083
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
535 path = evalstring(context, mapping, args[0])
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
536 return repo.pathto(path)
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
537
38428
aa98392eb5b0 templatefuncs: declare resource requirements for future use
Yuya Nishihara <yuya@tcha.org>
parents: 38427
diff changeset
538 @templatefunc('revset(query[, formatargs...])', requires={'repo', 'cache'})
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
539 def revset(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
540 """Execute a revision set query. See
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
541 :hg:`help revset`."""
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
542 if not len(args) > 0:
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
543 # i18n: "revset" is a keyword
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
544 raise error.ParseError(_("revset expects one or more arguments"))
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
545
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
546 raw = evalstring(context, mapping, args[0])
38427
4b73f316ba0e templatefuncs: minimize resource requirements
Yuya Nishihara <yuya@tcha.org>
parents: 38285
diff changeset
547 repo = context.resource(mapping, 'repo')
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
548
22304
5678b0e3608f templater: enable alias predicates to be used in "revset()" function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21960
diff changeset
549 def query(expr):
37674
f83cb91b052e revset: pass in lookup function instead of repo (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37327
diff changeset
550 m = revsetmod.match(repo.ui, expr, lookup=revsetmod.lookupfn(repo))
24114
fafd9a1284cf revset: make match function initiate query from full set by default
Yuya Nishihara <yuya@tcha.org>
parents: 23167
diff changeset
551 return m(repo)
22304
5678b0e3608f templater: enable alias predicates to be used in "revset()" function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21960
diff changeset
552
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
553 if len(args) > 1:
28333
41373244f4e5 templater: fix revset() to evaluate format arguments eagerly
Yuya Nishihara <yuya@tcha.org>
parents: 28332
diff changeset
554 formatargs = [evalfuncarg(context, mapping, a) for a in args[1:]]
31024
0b8356705de6 revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30625
diff changeset
555 revs = query(revsetlang.formatspec(raw, *formatargs))
28178
96f2d50fb9f6 templater: factor out type conversion of revset() result
Yuya Nishihara <yuya@tcha.org>
parents: 27940
diff changeset
556 revs = list(revs)
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
557 else:
35467
d6cfa722b044 templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents: 35414
diff changeset
558 cache = context.resource(mapping, 'cache')
d6cfa722b044 templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents: 35414
diff changeset
559 revsetcache = cache.setdefault("revsetcache", {})
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
560 if raw in revsetcache:
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
561 revs = revsetcache[raw]
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
562 else:
22304
5678b0e3608f templater: enable alias predicates to be used in "revset()" function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21960
diff changeset
563 revs = query(raw)
28178
96f2d50fb9f6 templater: factor out type conversion of revset() result
Yuya Nishihara <yuya@tcha.org>
parents: 27940
diff changeset
564 revs = list(revs)
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
565 revsetcache[raw] = revs
36595
2da414105809 templatekw: switch revset() to new API
Yuya Nishihara <yuya@tcha.org>
parents: 36591
diff changeset
566 return templatekw.showrevslist(context, mapping, "revision", revs)
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
567
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
568 @templatefunc('rstdoc(text, style)')
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
569 def rstdoc(context, mapping, args):
30332
318a24b52eeb spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents: 30232
diff changeset
570 """Format reStructuredText."""
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
571 if len(args) != 2:
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
572 # i18n: "rstdoc" is a keyword
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
573 raise error.ParseError(_("rstdoc expects two arguments"))
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
574
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
575 text = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
576 style = evalstring(context, mapping, args[1])
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
577
38185
3277940a935f templatefuncs: only render text portion of minirst.format() result
Anton Shestakov <av6@dwimlabs.net>
parents: 38040
diff changeset
578 return minirst.format(text, style=style, keep=['verbose'])[0]
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
579
38040
a3b4ccbec269 help: correct signature of separate() template function
Yuya Nishihara <yuya@tcha.org>
parents: 37709
diff changeset
580 @templatefunc('separate(sep, args...)', argspec='sep *args')
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
581 def separate(context, mapping, args):
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
582 """Add a separator between non-empty arguments."""
31886
bdda942f4b9c templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31885
diff changeset
583 if 'sep' not in args:
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
584 # i18n: "separate" is a keyword
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
585 raise error.ParseError(_("separate expects at least one argument"))
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
586
31886
bdda942f4b9c templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31885
diff changeset
587 sep = evalstring(context, mapping, args['sep'])
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
588 first = True
31886
bdda942f4b9c templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 31885
diff changeset
589 for arg in args['args']:
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
590 argstr = evalstring(context, mapping, arg)
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
591 if not argstr:
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
592 continue
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
593 if first:
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
594 first = False
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
595 else:
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
596 yield sep
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
597 yield argstr
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
598
38853
3588e41f796d shortest: cache disambiguation revset
Martin von Zweigbergk <martinvonz@google.com>
parents: 38449
diff changeset
599 @templatefunc('shortest(node, minlength=4)', requires={'repo', 'cache'})
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
600 def shortest(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
601 """Obtain the shortest representation of
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
602 a node."""
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
603 if not (1 <= len(args) <= 2):
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
604 # i18n: "shortest" is a keyword
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
605 raise error.ParseError(_("shortest() expects one or two arguments"))
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
606
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
607 hexnode = evalstring(context, mapping, args[0])
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
608
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
609 minlength = 4
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
610 if len(args) > 1:
28346
542d200bd261 templater: fix shortest() to evaluate int argument and handle error
Yuya Nishihara <yuya@tcha.org>
parents: 28345
diff changeset
611 minlength = evalinteger(context, mapping, args[1],
542d200bd261 templater: fix shortest() to evaluate int argument and handle error
Yuya Nishihara <yuya@tcha.org>
parents: 28345
diff changeset
612 # i18n: "shortest" is a keyword
542d200bd261 templater: fix shortest() to evaluate int argument and handle error
Yuya Nishihara <yuya@tcha.org>
parents: 28345
diff changeset
613 _("shortest() expects an integer minlength"))
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
614
38427
4b73f316ba0e templatefuncs: minimize resource requirements
Yuya Nishihara <yuya@tcha.org>
parents: 38285
diff changeset
615 repo = context.resource(mapping, 'repo')
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
616 if len(hexnode) > 40:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
617 return hexnode
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
618 elif len(hexnode) == 40:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
619 try:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
620 node = bin(hexnode)
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
621 except TypeError:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
622 return hexnode
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
623 else:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
624 try:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
625 node = scmutil.resolvehexnodeidprefix(repo, hexnode)
37859
66dc9db6ed2c shortest: make {shortest("fffffffff")} work again
Martin von Zweigbergk <martinvonz@google.com>
parents: 37709
diff changeset
626 except error.WdirUnsupported:
66dc9db6ed2c shortest: make {shortest("fffffffff")} work again
Martin von Zweigbergk <martinvonz@google.com>
parents: 37709
diff changeset
627 node = wdirid
66dc9db6ed2c shortest: make {shortest("fffffffff")} work again
Martin von Zweigbergk <martinvonz@google.com>
parents: 37709
diff changeset
628 except error.LookupError:
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
629 return hexnode
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
630 if not node:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
631 return hexnode
38853
3588e41f796d shortest: cache disambiguation revset
Martin von Zweigbergk <martinvonz@google.com>
parents: 38449
diff changeset
632 cache = context.resource(mapping, 'cache')
37865
da083d9fafab shortest: don't keep checking for longer prefix if node doesn't exist (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 37859
diff changeset
633 try:
38853
3588e41f796d shortest: cache disambiguation revset
Martin von Zweigbergk <martinvonz@google.com>
parents: 38449
diff changeset
634 return scmutil.shortesthexnodeidprefix(repo, node, minlength, cache)
37865
da083d9fafab shortest: don't keep checking for longer prefix if node doesn't exist (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 37859
diff changeset
635 except error.RepoLookupError:
da083d9fafab shortest: don't keep checking for longer prefix if node doesn't exist (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 37859
diff changeset
636 return hexnode
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
637
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
638 @templatefunc('strip(text[, chars])')
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
639 def strip(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
640 """Strip characters from a string. By default,
26106
c568c4db036f templatefilters: remove redundant 'date' and 'strip' filters
Yuya Nishihara <yuya@tcha.org>
parents: 26105
diff changeset
641 strips all leading and trailing whitespace."""
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
642 if not (1 <= len(args) <= 2):
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
643 # i18n: "strip" is a keyword
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
644 raise error.ParseError(_("strip expects one or two arguments"))
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
645
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
646 text = evalstring(context, mapping, args[0])
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
647 if len(args) == 2:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
648 chars = evalstring(context, mapping, args[1])
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
649 return text.strip(chars)
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
650 return text.strip()
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
651
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
652 @templatefunc('sub(pattern, replacement, expression)')
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
653 def sub(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
654 """Perform text substitution
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
655 using regular expressions."""
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
656 if len(args) != 3:
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
657 # i18n: "sub" is a keyword
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
658 raise error.ParseError(_("sub expects three arguments"))
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
659
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
660 pat = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
661 rpl = evalstring(context, mapping, args[1])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
662 src = evalstring(context, mapping, args[2])
26188
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
663 try:
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
664 patre = re.compile(pat)
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
665 except re.error:
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
666 # i18n: "sub" is a keyword
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
667 raise error.ParseError(_("sub got an invalid pattern: %s") % pat)
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
668 try:
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
669 yield patre.sub(rpl, src)
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
670 except re.error:
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
671 # i18n: "sub" is a keyword
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
672 raise error.ParseError(_("sub got an invalid replacement: %s") % rpl)
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
673
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
674 @templatefunc('startswith(pattern, text)')
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
675 def startswith(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
676 """Returns the value from the "text" argument
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
677 if it begins with the content from the "pattern" argument."""
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
678 if len(args) != 2:
21960
2896d450fec4 templater: add i18n comments to error messages of newly added functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21846
diff changeset
679 # i18n: "startswith" is a keyword
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
680 raise error.ParseError(_("startswith expects two arguments"))
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
681
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
682 patn = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
683 text = evalstring(context, mapping, args[1])
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
684 if text.startswith(patn):
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
685 return text
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
686 return ''
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
687
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
688 @templatefunc('word(number, text[, separator])')
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
689 def word(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
690 """Return the nth word from a string."""
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
691 if not (2 <= len(args) <= 3):
21960
2896d450fec4 templater: add i18n comments to error messages of newly added functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21846
diff changeset
692 # i18n: "word" is a keyword
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
693 raise error.ParseError(_("word expects two or three arguments, got %d")
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
694 % len(args))
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
695
28343
a6c2310b3827 templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents: 28334
diff changeset
696 num = evalinteger(context, mapping, args[0],
a6c2310b3827 templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents: 28334
diff changeset
697 # i18n: "word" is a keyword
a6c2310b3827 templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents: 28334
diff changeset
698 _("word expects an integer index"))
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
699 text = evalstring(context, mapping, args[1])
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
700 if len(args) == 3:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
701 splitter = evalstring(context, mapping, args[2])
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
702 else:
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
703 splitter = None
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
704
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
705 tokens = text.split(splitter)
26502
4ca98a389152 templater: protect word() from crashing on out of range negative value
Matt Harbison <matt_harbison@yahoo.com>
parents: 25815
diff changeset
706 if num >= len(tokens) or num < -len(tokens):
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
707 return ''
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
708 else:
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
709 return tokens[num]
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
710
28695
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
711 def loadfunction(ui, extname, registrarobj):
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
712 """Load template function from specified registrarobj
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
713 """
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
714 for name, func in registrarobj._table.iteritems():
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
715 funcs[name] = func
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
716
24601
d80819f67d59 templater: tell hggettext to collect help of template functions
Yuya Nishihara <yuya@tcha.org>
parents: 24586
diff changeset
717 # tell hggettext to extract docstrings from these functions:
d80819f67d59 templater: tell hggettext to collect help of template functions
Yuya Nishihara <yuya@tcha.org>
parents: 24586
diff changeset
718 i18nfunctions = funcs.values()