Mercurial > hg
annotate mercurial/templater.py @ 36681:340e4b711df7
bdiff: avoid pointer arithmetic on void*
MSVC 2008 complains:
mercurial/cext/bdiff.c(106) : error C2036: 'void *' : unknown size
mercurial/cext/bdiff.c(107) : error C2036: 'void *' : unknown size
Maybe it's a gcc extension?
https://stackoverflow.com/questions/37460579/error-c2036-void-unknown-size
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sat, 03 Mar 2018 23:29:40 -0500 |
parents | 44048f1bcee5 |
children | 1b179d151578 |
rev | line source |
---|---|
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1 # templater.py - template expansion for output |
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 | 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 |
34137
a8994d08e4a2
doctest: use print_function and convert bytes to unicode where needed
Yuya Nishihara <yuya@tcha.org>
parents:
34131
diff
changeset
|
8 from __future__ import absolute_import, print_function |
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 os |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
11 import re |
17982
e06e9fd2d99f
template engine: convert generator-based iterator to list-based iterator
Weiwen <weiwen@fb.com>
parents:
17890
diff
changeset
|
12 import types |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
13 |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
14 from .i18n import _ |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
15 from . import ( |
31521
44c591f63458
templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents:
31520
diff
changeset
|
16 color, |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
17 config, |
31520
6f150bb19317
templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents:
31519
diff
changeset
|
18 encoding, |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
19 error, |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
20 minirst, |
33993
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
21 obsutil, |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
22 parser, |
30615
bb77654dc7ae
py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30332
diff
changeset
|
23 pycompat, |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
24 registrar, |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
25 revset as revsetmod, |
31024
0b8356705de6
revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
30625
diff
changeset
|
26 revsetlang, |
34458
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
27 scmutil, |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
28 templatefilters, |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
29 templatekw, |
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 ) |
36607
c6061cadb400
util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents:
36596
diff
changeset
|
32 from .utils import dateutil |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
33 |
36444
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36443
diff
changeset
|
34 class ResourceUnavailable(error.Abort): |
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36443
diff
changeset
|
35 pass |
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36443
diff
changeset
|
36 |
36443
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36245
diff
changeset
|
37 class TemplateNotFound(error.Abort): |
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36245
diff
changeset
|
38 pass |
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36245
diff
changeset
|
39 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
40 # template parsing |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
41 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
42 elements = { |
25815
e71e5629e006
parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents:
25801
diff
changeset
|
43 # token-type: binding-strength, primary, prefix, infix, suffix |
e71e5629e006
parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents:
25801
diff
changeset
|
44 "(": (20, None, ("group", 1, ")"), ("func", 1, ")"), None), |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
45 ".": (18, None, None, (".", 18), None), |
34329
6367318327f0
templater: adjust binding strength of '%' and '|' operators (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
34328
diff
changeset
|
46 "%": (15, None, None, ("%", 15), None), |
31884
0926ca37a990
templater: adjust binding strengths to make room for key-value operator
Yuya Nishihara <yuya@tcha.org>
parents:
31883
diff
changeset
|
47 "|": (15, None, None, ("|", 15), None), |
0926ca37a990
templater: adjust binding strengths to make room for key-value operator
Yuya Nishihara <yuya@tcha.org>
parents:
31883
diff
changeset
|
48 "*": (5, None, None, ("*", 5), None), |
0926ca37a990
templater: adjust binding strengths to make room for key-value operator
Yuya Nishihara <yuya@tcha.org>
parents:
31883
diff
changeset
|
49 "/": (5, None, None, ("/", 5), None), |
0926ca37a990
templater: adjust binding strengths to make room for key-value operator
Yuya Nishihara <yuya@tcha.org>
parents:
31883
diff
changeset
|
50 "+": (4, None, None, ("+", 4), None), |
0926ca37a990
templater: adjust binding strengths to make room for key-value operator
Yuya Nishihara <yuya@tcha.org>
parents:
31883
diff
changeset
|
51 "-": (4, None, ("negate", 19), ("-", 4), None), |
31885
d18b624c1c06
templater: add parsing rule for key-value pair
Yuya Nishihara <yuya@tcha.org>
parents:
31884
diff
changeset
|
52 "=": (3, None, None, ("keyvalue", 3), None), |
31883
25be03a33f50
templater: sort token table by binding strength
Yuya Nishihara <yuya@tcha.org>
parents:
31880
diff
changeset
|
53 ",": (2, None, None, ("list", 2), None), |
25be03a33f50
templater: sort token table by binding strength
Yuya Nishihara <yuya@tcha.org>
parents:
31880
diff
changeset
|
54 ")": (0, None, None, None, None), |
25815
e71e5629e006
parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents:
25801
diff
changeset
|
55 "integer": (0, "integer", None, None, None), |
e71e5629e006
parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents:
25801
diff
changeset
|
56 "symbol": (0, "symbol", None, None, None), |
e71e5629e006
parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents:
25801
diff
changeset
|
57 "string": (0, "string", None, None, None), |
e71e5629e006
parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents:
25801
diff
changeset
|
58 "template": (0, "template", None, None, None), |
e71e5629e006
parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents:
25801
diff
changeset
|
59 "end": (0, None, None, None, None), |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
60 } |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
61 |
28911
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
62 def tokenize(program, start, end, term=None): |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
63 """Parse a template expression into a stream of tokens, which must end |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
64 with term if specified""" |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
65 pos = start |
32154
52e222eef646
py3: use pycompat.bytestr instead of bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32037
diff
changeset
|
66 program = pycompat.bytestr(program) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
67 while pos < end: |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
68 c = program[pos] |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
69 if c.isspace(): # skip inter-token whitespace |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
70 pass |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
71 elif c in "(=,).%|+-*/": # handle simple operators |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
72 yield (c, None, pos) |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
73 elif c in '"\'': # handle quoted templates |
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
74 s = pos + 1 |
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
75 data, pos = _parsetemplate(program, s, end, c) |
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
76 yield ('template', data, s) |
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
77 pos -= 1 |
25784
33e613687dab
templater: remove processing of "string" literals from tokenizer
Yuya Nishihara <yuya@tcha.org>
parents:
25783
diff
changeset
|
78 elif c == 'r' and program[pos:pos + 2] in ("r'", 'r"'): |
33e613687dab
templater: remove processing of "string" literals from tokenizer
Yuya Nishihara <yuya@tcha.org>
parents:
25783
diff
changeset
|
79 # handle quoted strings |
33e613687dab
templater: remove processing of "string" literals from tokenizer
Yuya Nishihara <yuya@tcha.org>
parents:
25783
diff
changeset
|
80 c = program[pos + 1] |
33e613687dab
templater: remove processing of "string" literals from tokenizer
Yuya Nishihara <yuya@tcha.org>
parents:
25783
diff
changeset
|
81 s = pos = pos + 2 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
82 while pos < end: # find closing quote |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
83 d = program[pos] |
25638
6047b60cdd09
templater: fix handling of \-escapes in raw string literals
Yuya Nishihara <yuya@tcha.org>
parents:
25637
diff
changeset
|
84 if d == '\\': # skip over escaped characters |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
85 pos += 2 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
86 continue |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
87 if d == c: |
25785
f976b7dc5e7b
templater: unify "string" and "rawstring"
Yuya Nishihara <yuya@tcha.org>
parents:
25784
diff
changeset
|
88 yield ('string', program[s:pos], s) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
89 break |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
90 pos += 1 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
91 else: |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
92 raise error.ParseError(_("unterminated string"), s) |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
93 elif c.isdigit(): |
25002
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
94 s = pos |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
95 while pos < end: |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
96 d = program[pos] |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
97 if not d.isdigit(): |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
98 break |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
99 pos += 1 |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
100 yield ('integer', program[s:pos], s) |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
101 pos -= 1 |
36546
14bbb54a9c5b
py3: fix type of string literals in templater.tokenize()
Yuya Nishihara <yuya@tcha.org>
parents:
36545
diff
changeset
|
102 elif (c == '\\' and program[pos:pos + 2] in (br"\'", br'\"') |
14bbb54a9c5b
py3: fix type of string literals in templater.tokenize()
Yuya Nishihara <yuya@tcha.org>
parents:
36545
diff
changeset
|
103 or c == 'r' and program[pos:pos + 3] in (br"r\'", br'r\"')): |
25676
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
104 # handle escaped quoted strings for compatibility with 2.9.2-3.4, |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
105 # where some of nested templates were preprocessed as strings and |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
106 # then compiled. therefore, \"...\" was allowed. (issue4733) |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
107 # |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
108 # processing flow of _evalifliteral() at 5ab28a2e9962: |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
109 # outer template string -> stringify() -> compiletemplate() |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
110 # ------------------------ ------------ ------------------ |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
111 # {f("\\\\ {g(\"\\\"\")}"} \\ {g("\"")} [r'\\', {g("\"")}] |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
112 # ~~~~~~~~ |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
113 # escaped quoted string |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
114 if c == 'r': |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
115 pos += 1 |
25785
f976b7dc5e7b
templater: unify "string" and "rawstring"
Yuya Nishihara <yuya@tcha.org>
parents:
25784
diff
changeset
|
116 token = 'string' |
25676
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
117 else: |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
118 token = 'template' |
25676
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
119 quote = program[pos:pos + 2] |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
120 s = pos = pos + 2 |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
121 while pos < end: # find closing escaped quote |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
122 if program.startswith('\\\\\\', pos, end): |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
123 pos += 4 # skip over double escaped characters |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
124 continue |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
125 if program.startswith(quote, pos, end): |
26215
72aad184f061
templater: create string unescape helper (issue4798)
Matt Mackall <mpm@selenic.com>
parents:
26197
diff
changeset
|
126 # interpret as if it were a part of an outer string |
26231
87c9c562c37a
parser: move unescape helper from templater
Yuya Nishihara <yuya@tcha.org>
parents:
26215
diff
changeset
|
127 data = parser.unescapestr(program[s:pos]) |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
128 if token == 'template': |
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
129 data = _parsetemplate(data, 0, len(data))[0] |
25676
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
130 yield (token, data, s) |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
131 pos += 1 |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
132 break |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
133 pos += 1 |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
134 else: |
ec9c258e666d
templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733)
Yuya Nishihara <yuya@tcha.org>
parents:
25638
diff
changeset
|
135 raise error.ParseError(_("unterminated string"), s) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
136 elif c.isalnum() or c in '_': |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
137 s = pos |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
138 pos += 1 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
139 while pos < end: # find end of symbol |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
140 d = program[pos] |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
141 if not (d.isalnum() or d == "_"): |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
142 break |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
143 pos += 1 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
144 sym = program[s:pos] |
18893
74ea61318ea8
templater: back out 0615b22da148, it breaks schemes ({1})
Brendan Cully <brendan@kublai.com>
parents:
18889
diff
changeset
|
145 yield ('symbol', sym, s) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
146 pos -= 1 |
28911
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
147 elif c == term: |
25782
babd2c93bd99
templater: check existence of closing brace of template string
Yuya Nishihara <yuya@tcha.org>
parents:
25781
diff
changeset
|
148 yield ('end', None, pos + 1) |
babd2c93bd99
templater: check existence of closing brace of template string
Yuya Nishihara <yuya@tcha.org>
parents:
25781
diff
changeset
|
149 return |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
150 else: |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
151 raise error.ParseError(_("syntax error"), pos) |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
152 pos += 1 |
28911
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
153 if term: |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
154 raise error.ParseError(_("unterminated template expansion"), start) |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
155 yield ('end', None, pos) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
156 |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
157 def _parsetemplate(tmpl, start, stop, quote=''): |
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
158 r""" |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
159 >>> _parsetemplate(b'foo{bar}"baz', 0, 12) |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
160 ([('string', 'foo'), ('symbol', 'bar'), ('string', '"baz')], 12) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
161 >>> _parsetemplate(b'foo{bar}"baz', 0, 12, quote=b'"') |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
162 ([('string', 'foo'), ('symbol', 'bar')], 9) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
163 >>> _parsetemplate(b'foo"{bar}', 0, 9, quote=b'"') |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
164 ([('string', 'foo')], 4) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
165 >>> _parsetemplate(br'foo\"bar"baz', 0, 12, quote=b'"') |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
166 ([('string', 'foo"'), ('string', 'bar')], 9) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
167 >>> _parsetemplate(br'foo\\"bar', 0, 10, quote=b'"') |
25785
f976b7dc5e7b
templater: unify "string" and "rawstring"
Yuya Nishihara <yuya@tcha.org>
parents:
25784
diff
changeset
|
168 ([('string', 'foo\\')], 6) |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
169 """ |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
170 parsed = [] |
36244
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
171 for typ, val, pos in _scantemplate(tmpl, start, stop, quote): |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
172 if typ == 'string': |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
173 parsed.append((typ, val)) |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
174 elif typ == 'template': |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
175 parsed.append(val) |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
176 elif typ == 'end': |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
177 return parsed, pos |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
178 else: |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
179 raise error.ProgrammingError('unexpected type: %s' % typ) |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
180 raise error.ProgrammingError('unterminated scanning of template') |
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
181 |
36509
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
182 def scantemplate(tmpl, raw=False): |
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
183 r"""Scan (type, start, end) positions of outermost elements in template |
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
184 |
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
185 If raw=True, a backslash is not taken as an escape character just like |
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
186 r'' string in Python. Note that this is different from r'' literal in |
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
187 template in that no template fragment can appear in r'', e.g. r'{foo}' |
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
188 is a literal '{foo}', but ('{foo}', raw=True) is a template expression |
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
189 'foo'. |
36245
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
190 |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
191 >>> list(scantemplate(b'foo{bar}"baz')) |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
192 [('string', 0, 3), ('template', 3, 8), ('string', 8, 12)] |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
193 >>> list(scantemplate(b'outer{"inner"}outer')) |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
194 [('string', 0, 5), ('template', 5, 14), ('string', 14, 19)] |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
195 >>> list(scantemplate(b'foo\\{escaped}')) |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
196 [('string', 0, 5), ('string', 5, 13)] |
36509
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
197 >>> list(scantemplate(b'foo\\{escaped}', raw=True)) |
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
198 [('string', 0, 4), ('template', 4, 13)] |
36245
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
199 """ |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
200 last = None |
36509
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
201 for typ, val, pos in _scantemplate(tmpl, 0, len(tmpl), raw=raw): |
36245
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
202 if last: |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
203 yield last + (pos,) |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
204 if typ == 'end': |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
205 return |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
206 else: |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
207 last = (typ, pos) |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
208 raise error.ProgrammingError('unterminated scanning of template') |
c6ce479f7a28
templater: add function to help substituting patterns in template string
Yuya Nishihara <yuya@tcha.org>
parents:
36244
diff
changeset
|
209 |
36509
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
210 def _scantemplate(tmpl, start, stop, quote='', raw=False): |
36244
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
211 """Parse template string into chunks of strings and template expressions""" |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
212 sepchars = '{' + quote |
36509
638c012a87ef
templater: add option to parse template string just like raw string literal
Yuya Nishihara <yuya@tcha.org>
parents:
36500
diff
changeset
|
213 unescape = [parser.unescapestr, pycompat.identity][raw] |
25781
82c918509ef5
templater: extract function that parses template string
Yuya Nishihara <yuya@tcha.org>
parents:
25780
diff
changeset
|
214 pos = start |
25654
af329a84310c
parser: accept iterator of tokens instead of tokenizer function and program
Yuya Nishihara <yuya@tcha.org>
parents:
25599
diff
changeset
|
215 p = parser.parser(elements) |
36669
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
216 try: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
217 while pos < stop: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
218 n = min((tmpl.find(c, pos, stop) for c in sepchars), |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
219 key=lambda n: (n < 0, n)) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
220 if n < 0: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
221 yield ('string', unescape(tmpl[pos:stop]), pos) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
222 pos = stop |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
223 break |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
224 c = tmpl[n:n + 1] |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
225 bs = 0 # count leading backslashes |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
226 if not raw: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
227 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\')) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
228 if bs % 2 == 1: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
229 # escaped (e.g. '\{', '\\\{', but not '\\{') |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
230 yield ('string', unescape(tmpl[pos:n - 1]) + c, pos) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
231 pos = n + 1 |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
232 continue |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
233 if n > pos: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
234 yield ('string', unescape(tmpl[pos:n]), pos) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
235 if c == quote: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
236 yield ('end', None, n + 1) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
237 return |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
238 |
36669
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
239 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}')) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
240 if not tmpl.endswith('}', n + 1, pos): |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
241 raise error.ParseError(_("invalid token"), pos) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
242 yield ('template', parseres, n) |
25783
1f6878c87c25
templater: introduce one-pass parsing of nested template strings
Yuya Nishihara <yuya@tcha.org>
parents:
25782
diff
changeset
|
243 |
36669
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
244 if quote: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
245 raise error.ParseError(_("unterminated string"), start) |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
246 except error.ParseError as inst: |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
247 if len(inst.args) > 1: # has location |
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
248 loc = inst.args[1] |
36670
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
249 # Offset the caret location by the number of newlines before the |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
250 # location of the error, since we will replace one-char newlines |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
251 # with the two-char literal r'\n'. |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
252 offset = tmpl[:loc].count('\n') |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
253 tmpl = tmpl.replace('\n', br'\n') |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
254 # We want the caret to point to the place in the template that |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
255 # failed to parse, but in a hint we get a open paren at the |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
256 # start. Therefore, we print "loc" spaces (instead of "loc - 1") |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
257 # to line up the caret with the location of the error. |
44048f1bcee5
templater: provide hint for multi-line templates with parse errors
Ryan McElroy <rmcelroy@fb.com>
parents:
36669
diff
changeset
|
258 inst.hint = tmpl + '\n' + ' ' * (loc + offset) + '^ ' + _('here') |
36669
80d7fb6c2dec
templater: add hint to template parse errors to help locate issues
Ryan McElroy <rmcelroy@fb.com>
parents:
36648
diff
changeset
|
259 raise |
36244
18bdfad8506e
templater: extract function scanning template string
Yuya Nishihara <yuya@tcha.org>
parents:
35744
diff
changeset
|
260 yield ('end', None, pos) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
261 |
28547
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
262 def _unnesttemplatelist(tree): |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
263 """Expand list of templates to node tuple |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
264 |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
265 >>> def f(tree): |
34137
a8994d08e4a2
doctest: use print_function and convert bytes to unicode where needed
Yuya Nishihara <yuya@tcha.org>
parents:
34131
diff
changeset
|
266 ... print(pycompat.sysstr(prettyformat(_unnesttemplatelist(tree)))) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
267 >>> f((b'template', [])) |
34073
7bbc4e113e5f
parser: stabilize output of prettyformat() by using byte-safe repr()
Yuya Nishihara <yuya@tcha.org>
parents:
34071
diff
changeset
|
268 (string '') |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
269 >>> f((b'template', [(b'string', b'foo')])) |
34073
7bbc4e113e5f
parser: stabilize output of prettyformat() by using byte-safe repr()
Yuya Nishihara <yuya@tcha.org>
parents:
34071
diff
changeset
|
270 (string 'foo') |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
271 >>> f((b'template', [(b'string', b'foo'), (b'symbol', b'rev')])) |
28547
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
272 (template |
34073
7bbc4e113e5f
parser: stabilize output of prettyformat() by using byte-safe repr()
Yuya Nishihara <yuya@tcha.org>
parents:
34071
diff
changeset
|
273 (string 'foo') |
7bbc4e113e5f
parser: stabilize output of prettyformat() by using byte-safe repr()
Yuya Nishihara <yuya@tcha.org>
parents:
34071
diff
changeset
|
274 (symbol 'rev')) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
275 >>> f((b'template', [(b'symbol', b'rev')])) # template(rev) -> str |
28547
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
276 (template |
34073
7bbc4e113e5f
parser: stabilize output of prettyformat() by using byte-safe repr()
Yuya Nishihara <yuya@tcha.org>
parents:
34071
diff
changeset
|
277 (symbol 'rev')) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
278 >>> f((b'template', [(b'template', [(b'string', b'foo')])])) |
34073
7bbc4e113e5f
parser: stabilize output of prettyformat() by using byte-safe repr()
Yuya Nishihara <yuya@tcha.org>
parents:
34071
diff
changeset
|
279 (string 'foo') |
28547
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
280 """ |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
281 if not isinstance(tree, tuple): |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
282 return tree |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
283 op = tree[0] |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
284 if op != 'template': |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
285 return (op,) + tuple(_unnesttemplatelist(x) for x in tree[1:]) |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
286 |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
287 assert len(tree) == 2 |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
288 xs = tuple(_unnesttemplatelist(x) for x in tree[1]) |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
289 if not xs: |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
290 return ('string', '') # empty template "" |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
291 elif len(xs) == 1 and xs[0][0] == 'string': |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
292 return xs[0] # fast path for string with no template fragment "x" |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
293 else: |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
294 return (op,) + xs |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
295 |
28545
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
296 def parse(tmpl): |
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
297 """Parse template string into tree""" |
25781
82c918509ef5
templater: extract function that parses template string
Yuya Nishihara <yuya@tcha.org>
parents:
25780
diff
changeset
|
298 parsed, pos = _parsetemplate(tmpl, 0, len(tmpl)) |
28545
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
299 assert pos == len(tmpl), 'unquoted template should be consumed' |
28547
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
300 return _unnesttemplatelist(('template', parsed)) |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
301 |
28911
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
302 def _parseexpr(expr): |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
303 """Parse a template expression into tree |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
304 |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
305 >>> _parseexpr(b'"foo"') |
28911
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
306 ('string', 'foo') |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
307 >>> _parseexpr(b'foo(bar)') |
28911
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
308 ('func', ('symbol', 'foo'), ('symbol', 'bar')) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
309 >>> _parseexpr(b'foo(') |
28911
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
310 Traceback (most recent call last): |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
311 ... |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
312 ParseError: ('not a prefix: end', 4) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
313 >>> _parseexpr(b'"foo" "bar"') |
28911
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
314 Traceback (most recent call last): |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
315 ... |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
316 ParseError: ('invalid token', 7) |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
317 """ |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
318 p = parser.parser(elements) |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
319 tree, pos = p.parse(tokenize(expr, 0, len(expr))) |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
320 if pos != len(expr): |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
321 raise error.ParseError(_('invalid token'), pos) |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
322 return _unnesttemplatelist(tree) |
35da19348143
templater: add function to parse whole string as template expression
Yuya Nishihara <yuya@tcha.org>
parents:
28837
diff
changeset
|
323 |
28547
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
324 def prettyformat(tree): |
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
325 return parser.prettyformat(tree, ('integer', 'string', 'symbol')) |
28545
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
326 |
25001
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
327 def compileexp(exp, context, curmethods): |
28956
eea98190ed73
templater: inline compiletemplate() function into engine
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
328 """Compile parsed template tree to (func, data) pair""" |
35744
8685192a8733
templater: fix crash by empty group expression
Yuya Nishihara <yuya@tcha.org>
parents:
35680
diff
changeset
|
329 if not exp: |
8685192a8733
templater: fix crash by empty group expression
Yuya Nishihara <yuya@tcha.org>
parents:
35680
diff
changeset
|
330 raise error.ParseError(_("missing argument")) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
331 t = exp[0] |
25001
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
332 if t in curmethods: |
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
333 return curmethods[t](exp, context) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
334 raise error.ParseError(_("unknown method '%s'") % t) |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
335 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
336 # template evaluation |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
337 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
338 def getsymbol(exp): |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
339 if exp[0] == 'symbol': |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
340 return exp[1] |
21822
028a48105191
templater: add symbol to error
Ryan McElroy <rmcelroy@fb.com>
parents:
21821
diff
changeset
|
341 raise error.ParseError(_("expected a symbol, got '%s'") % exp[0]) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
342 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
343 def getlist(x): |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
344 if not x: |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
345 return [] |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
346 if x[0] == 'list': |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
347 return getlist(x[1]) + [x[2]] |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
348 return [x] |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
349 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
350 def gettemplate(exp, context): |
28545
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
351 """Compile given template tree or load named template from map file; |
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
352 returns (func, data) pair""" |
28546
1987ed32efca
templater: relax type of mapped template
Yuya Nishihara <yuya@tcha.org>
parents:
28545
diff
changeset
|
353 if exp[0] in ('template', 'string'): |
28545
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
354 return compileexp(exp, context, methods) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
355 if exp[0] == 'symbol': |
25599
695b93a79d17
templater: comment that gettemplate() has different name resolution order
Yuya Nishihara <yuya@tcha.org>
parents:
25598
diff
changeset
|
356 # unlike runsymbol(), here 'symbol' is always taken as template name |
695b93a79d17
templater: comment that gettemplate() has different name resolution order
Yuya Nishihara <yuya@tcha.org>
parents:
25598
diff
changeset
|
357 # even if it exists in mapping. this allows us to override mapping |
695b93a79d17
templater: comment that gettemplate() has different name resolution order
Yuya Nishihara <yuya@tcha.org>
parents:
25598
diff
changeset
|
358 # by web templates, e.g. 'changelogtag' is redefined in map file. |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
359 return context._load(exp[1]) |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
360 raise error.ParseError(_("expected template specifier")) |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
361 |
31927
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
362 def findsymbolicname(arg): |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
363 """Find symbolic name for the given compiled expression; returns None |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
364 if nothing found reliably""" |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
365 while True: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
366 func, data = arg |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
367 if func is runsymbol: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
368 return data |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
369 elif func is runfilter: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
370 arg = data[0] |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
371 else: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
372 return None |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
373 |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
374 def evalrawexp(context, mapping, arg): |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
375 """Evaluate given argument as a bare template object which may require |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
376 further processing (such as folding generator of strings)""" |
26124
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
377 func, data = arg |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
378 return func(context, mapping, data) |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
379 |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
380 def evalfuncarg(context, mapping, arg): |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
381 """Evaluate given argument as value type""" |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
382 thing = evalrawexp(context, mapping, arg) |
34330
89aec1834a86
templatekw: add new-style template expansion to {manifest}
Yuya Nishihara <yuya@tcha.org>
parents:
34329
diff
changeset
|
383 thing = templatekw.unwrapvalue(thing) |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
384 # evalrawexp() may return string, generator of strings or arbitrary object |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
385 # such as date tuple, but filter does not want generator. |
26124
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
386 if isinstance(thing, types.GeneratorType): |
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
387 thing = stringify(thing) |
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
388 return thing |
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
389 |
29816
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
390 def evalboolean(context, mapping, arg): |
29817
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
391 """Evaluate given argument as boolean, but also takes boolean literals""" |
29816
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
392 func, data = arg |
29817
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
393 if func is runsymbol: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
394 thing = func(context, mapping, data, default=None) |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
395 if thing is None: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
396 # not a template keyword, takes as a boolean literal |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
397 thing = util.parsebool(data) |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
398 else: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
399 thing = func(context, mapping, data) |
34330
89aec1834a86
templatekw: add new-style template expansion to {manifest}
Yuya Nishihara <yuya@tcha.org>
parents:
34329
diff
changeset
|
400 thing = templatekw.unwrapvalue(thing) |
29816
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
401 if isinstance(thing, bool): |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
402 return thing |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
403 # other objects are evaluated as strings, which means 0 is True, but |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
404 # empty dict/list should be False as they are expected to be '' |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
405 return bool(stringify(thing)) |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
406 |
34581
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
407 def evalinteger(context, mapping, arg, err=None): |
28344
ac371d4c007f
templater: drop redundant type conversion when evaluating integer argument
Yuya Nishihara <yuya@tcha.org>
parents:
28343
diff
changeset
|
408 v = evalfuncarg(context, mapping, arg) |
28343
a6c2310b3827
templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents:
28334
diff
changeset
|
409 try: |
28344
ac371d4c007f
templater: drop redundant type conversion when evaluating integer argument
Yuya Nishihara <yuya@tcha.org>
parents:
28343
diff
changeset
|
410 return int(v) |
ac371d4c007f
templater: drop redundant type conversion when evaluating integer argument
Yuya Nishihara <yuya@tcha.org>
parents:
28343
diff
changeset
|
411 except (TypeError, ValueError): |
34581
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
412 raise error.ParseError(err or _('not an integer')) |
28343
a6c2310b3827
templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents:
28334
diff
changeset
|
413 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
414 def evalstring(context, mapping, arg): |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
415 return stringify(evalrawexp(context, mapping, arg)) |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
416 |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
417 def evalstringliteral(context, mapping, arg): |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
418 """Evaluate given argument as string template, but returns symbol name |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
419 if it is unknown""" |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
420 func, data = arg |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
421 if func is runsymbol: |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
422 thing = func(context, mapping, data, default=data) |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
423 else: |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
424 thing = func(context, mapping, data) |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
425 return stringify(thing) |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
426 |
34581
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
427 _evalfuncbytype = { |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
428 bool: evalboolean, |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
429 bytes: evalstring, |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
430 int: evalinteger, |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
431 } |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
432 |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
433 def evalastype(context, mapping, arg, typ): |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
434 """Evaluate given argument and coerce its type""" |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
435 try: |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
436 f = _evalfuncbytype[typ] |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
437 except KeyError: |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
438 raise error.ProgrammingError('invalid type specified: %r' % typ) |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
439 return f(context, mapping, arg) |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
440 |
25002
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
441 def runinteger(context, mapping, data): |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
442 return int(data) |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
443 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
444 def runstring(context, mapping, data): |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
445 return data |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
446 |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
447 def _recursivesymbolblocker(key): |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
448 def showrecursion(**args): |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
449 raise error.Abort(_("recursive reference '%s' in template") % key) |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
450 return showrecursion |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
451 |
27940
cfe7da66f555
templater: abort if infinite recursion detected while compiling
Yuya Nishihara <yuya@tcha.org>
parents:
27939
diff
changeset
|
452 def _runrecursivesymbol(context, mapping, key): |
cfe7da66f555
templater: abort if infinite recursion detected while compiling
Yuya Nishihara <yuya@tcha.org>
parents:
27939
diff
changeset
|
453 raise error.Abort(_("recursive reference '%s' in template") % key) |
cfe7da66f555
templater: abort if infinite recursion detected while compiling
Yuya Nishihara <yuya@tcha.org>
parents:
27939
diff
changeset
|
454 |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
455 def runsymbol(context, mapping, key, default=''): |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
456 v = context.symbol(mapping, key) |
19770
0361163efbaf
templater: support using templates with non-standard names from map file
Alexander Plavin <alexander@plav.in>
parents:
19390
diff
changeset
|
457 if v is None: |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
458 # put poison to cut recursion. we can't move this to parsing phase |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
459 # because "x = {x}" is allowed if "x" is a keyword. (issue4758) |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
460 safemapping = mapping.copy() |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
461 safemapping[key] = _recursivesymbolblocker(key) |
19770
0361163efbaf
templater: support using templates with non-standard names from map file
Alexander Plavin <alexander@plav.in>
parents:
19390
diff
changeset
|
462 try: |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
463 v = context.process(key, safemapping) |
19770
0361163efbaf
templater: support using templates with non-standard names from map file
Alexander Plavin <alexander@plav.in>
parents:
19390
diff
changeset
|
464 except TemplateNotFound: |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
465 v = default |
36445
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
466 if callable(v) and getattr(v, '_requires', None) is None: |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
467 # old templatekw: expand all keywords and resources |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
468 props = context._resources.copy() |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
469 props.update(mapping) |
35588
dadbf213a765
py3: convert dict keys' to str before passing as kwargs
Pulkit Goyal <7895pulkit@gmail.com>
parents:
35481
diff
changeset
|
470 return v(**pycompat.strkwargs(props)) |
36445
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
471 if callable(v): |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
472 # new templatekw |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
473 try: |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
474 return v(context, mapping) |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
475 except ResourceUnavailable: |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
476 # unsupported keyword is mapped to empty just like unknown keyword |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
477 return None |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
478 return v |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
479 |
25596
c1975809a6b5
templater: take any string literals as template, but not for rawstring (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25595
diff
changeset
|
480 def buildtemplate(exp, context): |
28547
73d01cba5810
templater: expand list of parsed templates to template node
Yuya Nishihara <yuya@tcha.org>
parents:
28546
diff
changeset
|
481 ctmpl = [compileexp(e, context, methods) for e in exp[1:]] |
25596
c1975809a6b5
templater: take any string literals as template, but not for rawstring (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25595
diff
changeset
|
482 return (runtemplate, ctmpl) |
c1975809a6b5
templater: take any string literals as template, but not for rawstring (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25595
diff
changeset
|
483 |
25595
a7dd6692e5cb
templater: move runtemplate function out of buildmap/runmap pair
Yuya Nishihara <yuya@tcha.org>
parents:
25580
diff
changeset
|
484 def runtemplate(context, mapping, template): |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
485 for arg in template: |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
486 yield evalrawexp(context, mapping, arg) |
25595
a7dd6692e5cb
templater: move runtemplate function out of buildmap/runmap pair
Yuya Nishihara <yuya@tcha.org>
parents:
25580
diff
changeset
|
487 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
488 def buildfilter(exp, context): |
26104
0f1bc7faa50d
templater: inline getfilter() to buildfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
25985
diff
changeset
|
489 n = getsymbol(exp[2]) |
0f1bc7faa50d
templater: inline getfilter() to buildfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
25985
diff
changeset
|
490 if n in context._filters: |
0f1bc7faa50d
templater: inline getfilter() to buildfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
25985
diff
changeset
|
491 filt = context._filters[n] |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
492 arg = compileexp(exp[1], context, methods) |
26125
c990afab2243
templater: drop unneeded destructuring of argument tuple at buildfilter
Yuya Nishihara <yuya@tcha.org>
parents:
26124
diff
changeset
|
493 return (runfilter, (arg, filt)) |
26105
d67341f55429
templater: introduce unified filter syntax for unary functions
Yuya Nishihara <yuya@tcha.org>
parents:
26104
diff
changeset
|
494 if n in funcs: |
d67341f55429
templater: introduce unified filter syntax for unary functions
Yuya Nishihara <yuya@tcha.org>
parents:
26104
diff
changeset
|
495 f = funcs[n] |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
496 args = _buildfuncargs(exp[1], context, methods, n, f._argspec) |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
497 return (f, args) |
26104
0f1bc7faa50d
templater: inline getfilter() to buildfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
25985
diff
changeset
|
498 raise error.ParseError(_("unknown function '%s'") % n) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
499 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
500 def runfilter(context, mapping, data): |
26125
c990afab2243
templater: drop unneeded destructuring of argument tuple at buildfilter
Yuya Nishihara <yuya@tcha.org>
parents:
26124
diff
changeset
|
501 arg, filt = data |
c990afab2243
templater: drop unneeded destructuring of argument tuple at buildfilter
Yuya Nishihara <yuya@tcha.org>
parents:
26124
diff
changeset
|
502 thing = evalfuncarg(context, mapping, arg) |
17383
099c778ceb33
templater: abort when a template filter raises an exception (issue2987)
Neil Kodner <neilk@fb.com>
parents:
17334
diff
changeset
|
503 try: |
24280
6c55e37ba5f2
templater: allow piping generator-type function output to filters
Yuya Nishihara <yuya@tcha.org>
parents:
24240
diff
changeset
|
504 return filt(thing) |
17383
099c778ceb33
templater: abort when a template filter raises an exception (issue2987)
Neil Kodner <neilk@fb.com>
parents:
17334
diff
changeset
|
505 except (ValueError, AttributeError, TypeError): |
31927
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
506 sym = findsymbolicname(arg) |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
507 if sym: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
508 msg = (_("template filter '%s' is not compatible with keyword '%s'") |
34838
d3ea6a1c798f
templater: use pycompat.sysbytes to bytes-ify some __name__ attrs
Augie Fackler <augie@google.com>
parents:
34808
diff
changeset
|
509 % (pycompat.sysbytes(filt.__name__), sym)) |
17383
099c778ceb33
templater: abort when a template filter raises an exception (issue2987)
Neil Kodner <neilk@fb.com>
parents:
17334
diff
changeset
|
510 else: |
34838
d3ea6a1c798f
templater: use pycompat.sysbytes to bytes-ify some __name__ attrs
Augie Fackler <augie@google.com>
parents:
34808
diff
changeset
|
511 msg = (_("incompatible use of template filter '%s'") |
d3ea6a1c798f
templater: use pycompat.sysbytes to bytes-ify some __name__ attrs
Augie Fackler <augie@google.com>
parents:
34808
diff
changeset
|
512 % pycompat.sysbytes(filt.__name__)) |
31927
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
513 raise error.Abort(msg) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
514 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
515 def buildmap(exp, context): |
34325
86d050abd5c1
templater: do not destructure operands in buildmap()
Yuya Nishihara <yuya@tcha.org>
parents:
34324
diff
changeset
|
516 darg = compileexp(exp[1], context, methods) |
86d050abd5c1
templater: do not destructure operands in buildmap()
Yuya Nishihara <yuya@tcha.org>
parents:
34324
diff
changeset
|
517 targ = gettemplate(exp[2], context) |
86d050abd5c1
templater: do not destructure operands in buildmap()
Yuya Nishihara <yuya@tcha.org>
parents:
34324
diff
changeset
|
518 return (runmap, (darg, targ)) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
519 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
520 def runmap(context, mapping, data): |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
521 darg, targ = data |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
522 d = evalrawexp(context, mapping, darg) |
27891
ac8c0ee5c3b8
templater: make _hybrid not callable to avoid conflicting semantics
Yuya Nishihara <yuya@tcha.org>
parents:
27637
diff
changeset
|
523 if util.safehasattr(d, 'itermaps'): |
28349
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
524 diter = d.itermaps() |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
525 else: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
526 try: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
527 diter = iter(d) |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
528 except TypeError: |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
529 sym = findsymbolicname(darg) |
34324
e473f482b9b3
templater: use helper function to get name of non-iterable keyword
Yuya Nishihara <yuya@tcha.org>
parents:
34287
diff
changeset
|
530 if sym: |
e473f482b9b3
templater: use helper function to get name of non-iterable keyword
Yuya Nishihara <yuya@tcha.org>
parents:
34287
diff
changeset
|
531 raise error.ParseError(_("keyword '%s' is not iterable") % sym) |
28349
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
532 else: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
533 raise error.ParseError(_("%r is not iterable") % d) |
17631
0b241d7a8c62
templating: make new-style templating features work with command line lists
Matt Mackall <mpm@selenic.com>
parents:
17383
diff
changeset
|
534 |
31807
e6eb86b154c5
templater: provide loop counter as "index" keyword
Yuya Nishihara <yuya@tcha.org>
parents:
31806
diff
changeset
|
535 for i, v in enumerate(diter): |
28225
5c11702fe2a3
templater: fix list templating bug
Kostia Balytskyi <ikostia@fb.com>
parents:
28178
diff
changeset
|
536 lm = mapping.copy() |
31807
e6eb86b154c5
templater: provide loop counter as "index" keyword
Yuya Nishihara <yuya@tcha.org>
parents:
31806
diff
changeset
|
537 lm['index'] = i |
31806
8f203b491bb5
templater: rename variable "i" to "v" in runmap()
Yuya Nishihara <yuya@tcha.org>
parents:
31521
diff
changeset
|
538 if isinstance(v, dict): |
8f203b491bb5
templater: rename variable "i" to "v" in runmap()
Yuya Nishihara <yuya@tcha.org>
parents:
31521
diff
changeset
|
539 lm.update(v) |
17991
d605a82cf189
hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents:
17982
diff
changeset
|
540 lm['originalnode'] = mapping.get('node') |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
541 yield evalrawexp(context, lm, targ) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
542 else: |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
543 # v is not an iterable of dicts, this happen when 'key' |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
544 # has been fully expanded already and format is useless. |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
545 # If so, return the expanded value. |
31806
8f203b491bb5
templater: rename variable "i" to "v" in runmap()
Yuya Nishihara <yuya@tcha.org>
parents:
31521
diff
changeset
|
546 yield v |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
547 |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
548 def buildmember(exp, context): |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
549 darg = compileexp(exp[1], context, methods) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
550 memb = getsymbol(exp[2]) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
551 return (runmember, (darg, memb)) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
552 |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
553 def runmember(context, mapping, data): |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
554 darg, memb = data |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
555 d = evalrawexp(context, mapping, darg) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
556 if util.safehasattr(d, 'tomap'): |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
557 lm = mapping.copy() |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
558 lm.update(d.tomap()) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
559 return runsymbol(context, lm, memb) |
34536
4c1cfe54c08d
templater: extend dot operator as a short for get(dict, key)
Yuya Nishihara <yuya@tcha.org>
parents:
34535
diff
changeset
|
560 if util.safehasattr(d, 'get'): |
4c1cfe54c08d
templater: extend dot operator as a short for get(dict, key)
Yuya Nishihara <yuya@tcha.org>
parents:
34535
diff
changeset
|
561 return _getdictitem(d, memb) |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
562 |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
563 sym = findsymbolicname(darg) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
564 if sym: |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
565 raise error.ParseError(_("keyword '%s' has no member") % sym) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
566 else: |
36545
ab7f86a748e6
py3: drop b'' from error message generated by templater.runmember()
Yuya Nishihara <yuya@tcha.org>
parents:
36544
diff
changeset
|
567 raise error.ParseError(_("%r has no member") % pycompat.bytestr(d)) |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
568 |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
569 def buildnegate(exp, context): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
570 arg = compileexp(exp[1], context, exprmethods) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
571 return (runnegate, arg) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
572 |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
573 def runnegate(context, mapping, data): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
574 data = evalinteger(context, mapping, data, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
575 _('negation needs an integer argument')) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
576 return -data |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
577 |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
578 def buildarithmetic(exp, context, func): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
579 left = compileexp(exp[1], context, exprmethods) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
580 right = compileexp(exp[2], context, exprmethods) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
581 return (runarithmetic, (func, left, right)) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
582 |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
583 def runarithmetic(context, mapping, data): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
584 func, left, right = data |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
585 left = evalinteger(context, mapping, left, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
586 _('arithmetic only defined on integers')) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
587 right = evalinteger(context, mapping, right, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
588 _('arithmetic only defined on integers')) |
30116
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
589 try: |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
590 return func(left, right) |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
591 except ZeroDivisionError: |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
592 raise error.Abort(_('division by zero is not defined')) |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
593 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
594 def buildfunc(exp, context): |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
595 n = getsymbol(exp[1]) |
14925
ab545a15d807
templater: use a global funcs table
Matt Mackall <mpm@selenic.com>
parents:
14168
diff
changeset
|
596 if n in funcs: |
ab545a15d807
templater: use a global funcs table
Matt Mackall <mpm@selenic.com>
parents:
14168
diff
changeset
|
597 f = funcs[n] |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
598 args = _buildfuncargs(exp[2], context, exprmethods, n, f._argspec) |
14925
ab545a15d807
templater: use a global funcs table
Matt Mackall <mpm@selenic.com>
parents:
14168
diff
changeset
|
599 return (f, args) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
600 if n in context._filters: |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
601 args = _buildfuncargs(exp[2], context, exprmethods, n, argspec=None) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
602 if len(args) != 1: |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
603 raise error.ParseError(_("filter %s expects one argument") % n) |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
604 f = context._filters[n] |
26125
c990afab2243
templater: drop unneeded destructuring of argument tuple at buildfilter
Yuya Nishihara <yuya@tcha.org>
parents:
26124
diff
changeset
|
605 return (runfilter, (args[0], f)) |
20857
6eb55310fcbc
templater: raise error for unknown func
Sean Farley <sean.michael.farley@gmail.com>
parents:
20663
diff
changeset
|
606 raise error.ParseError(_("unknown function '%s'") % n) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
607 |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
608 def _buildfuncargs(exp, context, curmethods, funcname, argspec): |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
609 """Compile parsed tree of function arguments into list or dict of |
31921
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
610 (func, data) pairs |
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
611 |
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
612 >>> context = engine(lambda t: (runsymbol, t)) |
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
613 >>> def fargs(expr, argspec): |
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
614 ... x = _parseexpr(expr) |
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
615 ... n = getsymbol(x[1]) |
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
616 ... return _buildfuncargs(x[2], context, exprmethods, n, argspec) |
34139
be00af4a1ac5
doctest: coerce dict.keys() to list
Yuya Nishihara <yuya@tcha.org>
parents:
34137
diff
changeset
|
617 >>> list(fargs(b'a(l=1, k=2)', b'k l m').keys()) |
31922
0f41f1e3c75c
parser: preserve order of keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31921
diff
changeset
|
618 ['l', 'k'] |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
34073
diff
changeset
|
619 >>> args = fargs(b'a(opts=1, k=2)', b'**opts') |
34139
be00af4a1ac5
doctest: coerce dict.keys() to list
Yuya Nishihara <yuya@tcha.org>
parents:
34137
diff
changeset
|
620 >>> list(args.keys()), list(args[b'opts'].keys()) |
31922
0f41f1e3c75c
parser: preserve order of keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31921
diff
changeset
|
621 (['opts'], ['opts', 'k']) |
31921
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
622 """ |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
623 def compiledict(xs): |
31922
0f41f1e3c75c
parser: preserve order of keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31921
diff
changeset
|
624 return util.sortdict((k, compileexp(x, context, curmethods)) |
0f41f1e3c75c
parser: preserve order of keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31921
diff
changeset
|
625 for k, x in xs.iteritems()) |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
626 def compilelist(xs): |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
627 return [compileexp(x, context, curmethods) for x in xs] |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
628 |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
629 if not argspec: |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
630 # filter or function with no argspec: return list of positional args |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
631 return compilelist(getlist(exp)) |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
632 |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
633 # function with argspec: return dict of named args |
31921
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
634 _poskeys, varkey, _keys, optkey = argspec = parser.splitargspec(argspec) |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
635 treeargs = parser.buildargsdict(getlist(exp), funcname, argspec, |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
636 keyvaluenode='keyvalue', keynode='symbol') |
31922
0f41f1e3c75c
parser: preserve order of keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31921
diff
changeset
|
637 compargs = util.sortdict() |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
638 if varkey: |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
639 compargs[varkey] = compilelist(treeargs.pop(varkey)) |
31921
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
640 if optkey: |
2156934b7917
parser: extend buildargsdict() to support arbitrary number of **kwargs
Yuya Nishihara <yuya@tcha.org>
parents:
31887
diff
changeset
|
641 compargs[optkey] = compiledict(treeargs.pop(optkey)) |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
642 compargs.update(compiledict(treeargs)) |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
643 return compargs |
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
644 |
31885
d18b624c1c06
templater: add parsing rule for key-value pair
Yuya Nishihara <yuya@tcha.org>
parents:
31884
diff
changeset
|
645 def buildkeyvaluepair(exp, content): |
d18b624c1c06
templater: add parsing rule for key-value pair
Yuya Nishihara <yuya@tcha.org>
parents:
31884
diff
changeset
|
646 raise error.ParseError(_("can't use a key-value pair in this context")) |
d18b624c1c06
templater: add parsing rule for key-value pair
Yuya Nishihara <yuya@tcha.org>
parents:
31884
diff
changeset
|
647 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
648 # 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
|
649 funcs = {} |
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
650 |
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
651 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
|
652 |
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
653 @templatefunc('date(date[, fmt])') |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
654 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
|
655 """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
|
656 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
|
657 "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
|
658 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
|
659 # i18n: "date" is a keyword |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
660 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
|
661 |
28334
9dc340f51e06
templater: make date() use helper function to evaluate argument
Yuya Nishihara <yuya@tcha.org>
parents:
28333
diff
changeset
|
662 date = evalfuncarg(context, mapping, args[0]) |
24903
09124cce913f
templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents:
24886
diff
changeset
|
663 fmt = None |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
664 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
|
665 fmt = evalstring(context, mapping, args[1]) |
24903
09124cce913f
templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents:
24886
diff
changeset
|
666 try: |
09124cce913f
templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents:
24886
diff
changeset
|
667 if fmt is None: |
36607
c6061cadb400
util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents:
36596
diff
changeset
|
668 return dateutil.datestr(date) |
24903
09124cce913f
templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents:
24886
diff
changeset
|
669 else: |
36607
c6061cadb400
util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents:
36596
diff
changeset
|
670 return dateutil.datestr(date, fmt) |
24903
09124cce913f
templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents:
24886
diff
changeset
|
671 except (TypeError, ValueError): |
09124cce913f
templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents:
24886
diff
changeset
|
672 # i18n: "date" is a keyword |
09124cce913f
templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents:
24886
diff
changeset
|
673 raise error.ParseError(_("date expects a date information")) |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
674 |
31928
277b3e2d711b
templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents:
31927
diff
changeset
|
675 @templatefunc('dict([[key=]value...])', argspec='*args **kwargs') |
31926
932241b8c644
templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents:
31922
diff
changeset
|
676 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
|
677 """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
|
678 a value expression can provide an unambiguous name.""" |
31926
932241b8c644
templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents:
31922
diff
changeset
|
679 data = util.sortdict() |
31928
277b3e2d711b
templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents:
31927
diff
changeset
|
680 |
277b3e2d711b
templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents:
31927
diff
changeset
|
681 for v in args['args']: |
277b3e2d711b
templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents:
31927
diff
changeset
|
682 k = findsymbolicname(v) |
277b3e2d711b
templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents:
31927
diff
changeset
|
683 if not k: |
277b3e2d711b
templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents:
31927
diff
changeset
|
684 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
|
685 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
|
686 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
|
687 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
|
688 |
31926
932241b8c644
templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents:
31922
diff
changeset
|
689 data.update((k, evalfuncarg(context, mapping, v)) |
932241b8c644
templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents:
31922
diff
changeset
|
690 for k, v in args['kwargs'].iteritems()) |
932241b8c644
templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents:
31922
diff
changeset
|
691 return templatekw.hybriddict(data) |
932241b8c644
templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents:
31922
diff
changeset
|
692 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
693 @templatefunc('diff([includepattern [, excludepattern]])') |
22434
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
694 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
|
695 """Show a diff, optionally |
24586
90e3f5d22dad
templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24337
diff
changeset
|
696 specifying files to include or exclude.""" |
22434
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
697 if len(args) > 2: |
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
698 # i18n: "diff" is a keyword |
27293
9e06e7fb037d
grammar: favor zero, one, two over ... or no
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
699 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
|
700 |
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
701 def getpatterns(i): |
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
702 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
|
703 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
|
704 if s: |
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
705 return [s] |
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
706 return [] |
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
707 |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
708 ctx = context.resource(mapping, 'ctx') |
22434
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
709 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
|
710 |
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
711 return ''.join(chunks) |
40ce05b50148
templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
22304
diff
changeset
|
712 |
34458
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
713 @templatefunc('extdata(source)', argspec='source') |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
714 def extdata(context, mapping, args): |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
715 """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
|
716 if 'source' not in args: |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
717 # i18n: "extdata" is a keyword |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
718 raise error.ParseError(_('extdata expects one argument')) |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
719 |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
720 source = evalstring(context, mapping, args['source']) |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
721 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
|
722 ctx = context.resource(mapping, 'ctx') |
34458
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
723 if source in cache: |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
724 data = cache[source] |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
725 else: |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
726 data = cache[source] = scmutil.extdatasource(ctx.repo(), source) |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
727 return data.get(ctx.rev(), '') |
a1b89c8ad32d
templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents:
34346
diff
changeset
|
728 |
30008
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
729 @templatefunc('files(pattern)') |
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
730 def files(context, mapping, args): |
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
731 """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
|
732 :hg:`help patterns`.""" |
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
733 if not len(args) == 1: |
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
734 # i18n: "files" is a keyword |
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
735 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
|
736 |
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
737 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
|
738 ctx = context.resource(mapping, 'ctx') |
30008
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
739 m = ctx.match([raw]) |
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
740 files = list(ctx.matches(m)) |
36591
121a20e5da56
templatekw: switch most of showlist template keywords to new API (issue5779)
Yuya Nishihara <yuya@tcha.org>
parents:
36550
diff
changeset
|
741 return templatekw.compatlist(context, mapping, "file", files) |
30008
e83f89d3b1f7
templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents:
29848
diff
changeset
|
742 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
743 @templatefunc('fill(text[, width[, initialident[, hangindent]]])') |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
744 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
|
745 """Fill many |
24586
90e3f5d22dad
templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24337
diff
changeset
|
746 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
|
747 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
|
748 # i18n: "fill" is a keyword |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
749 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
|
750 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
751 text = evalstring(context, mapping, args[0]) |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
752 width = 76 |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
753 initindent = '' |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
754 hangindent = '' |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
755 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
|
756 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
|
757 # i18n: "fill" is a keyword |
a6c2310b3827
templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents:
28334
diff
changeset
|
758 _("fill expects an integer width")) |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
759 try: |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
760 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
|
761 hangindent = evalstring(context, mapping, args[3]) |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
762 except IndexError: |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
763 pass |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
764 |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
765 return templatefilters.fill(text, width, initindent, hangindent) |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
766 |
31169
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
767 @templatefunc('formatnode(node)') |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
768 def formatnode(context, mapping, args): |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
769 """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
|
770 if len(args) != 1: |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
771 # i18n: "formatnode" is a keyword |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
772 raise error.ParseError(_("formatnode expects one argument")) |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
773 |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
774 ui = context.resource(mapping, 'ui') |
31169
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
775 node = evalstring(context, mapping, args[0]) |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
776 if ui.debugflag: |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
777 return node |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
778 return templatefilters.short(node) |
48a8b2e5fe31
templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents:
31024
diff
changeset
|
779 |
31887
f7b3677f66cd
templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31886
diff
changeset
|
780 @templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])', |
f7b3677f66cd
templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31886
diff
changeset
|
781 argspec='text width fillchar left') |
20370
aa51392da507
template: add pad function for padding output
Durham Goode <durham@fb.com>
parents:
20369
diff
changeset
|
782 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
|
783 """Pad text with a |
24586
90e3f5d22dad
templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24337
diff
changeset
|
784 fill character.""" |
31887
f7b3677f66cd
templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31886
diff
changeset
|
785 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
|
786 # i18n: "pad" is a keyword |
20370
aa51392da507
template: add pad function for padding output
Durham Goode <durham@fb.com>
parents:
20369
diff
changeset
|
787 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
|
788 |
31887
f7b3677f66cd
templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31886
diff
changeset
|
789 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
|
790 # 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
|
791 _("pad() expects an integer width")) |
20370
aa51392da507
template: add pad function for padding output
Durham Goode <durham@fb.com>
parents:
20369
diff
changeset
|
792 |
31887
f7b3677f66cd
templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31886
diff
changeset
|
793 text = evalstring(context, mapping, args['text']) |
20370
aa51392da507
template: add pad function for padding output
Durham Goode <durham@fb.com>
parents:
20369
diff
changeset
|
794 |
29818
407879b0893b
templater: rename "right" argument of pad() function
Yuya Nishihara <yuya@tcha.org>
parents:
29817
diff
changeset
|
795 left = False |
20370
aa51392da507
template: add pad function for padding output
Durham Goode <durham@fb.com>
parents:
20369
diff
changeset
|
796 fillchar = ' ' |
31887
f7b3677f66cd
templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31886
diff
changeset
|
797 if 'fillchar' in args: |
f7b3677f66cd
templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31886
diff
changeset
|
798 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
|
799 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
|
800 # i18n: "pad" is a keyword |
3725986b151a
templater: reject bad fillchar argument passed to pad()
Yuya Nishihara <yuya@tcha.org>
parents:
31169
diff
changeset
|
801 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
|
802 if 'left' in args: |
f7b3677f66cd
templater: port pad() to take keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31886
diff
changeset
|
803 left = evalboolean(context, mapping, args['left']) |
20370
aa51392da507
template: add pad function for padding output
Durham Goode <durham@fb.com>
parents:
20369
diff
changeset
|
804 |
31521
44c591f63458
templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents:
31520
diff
changeset
|
805 fillwidth = width - encoding.colwidth(color.stripeffects(text)) |
31520
6f150bb19317
templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents:
31519
diff
changeset
|
806 if fillwidth <= 0: |
6f150bb19317
templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents:
31519
diff
changeset
|
807 return text |
29818
407879b0893b
templater: rename "right" argument of pad() function
Yuya Nishihara <yuya@tcha.org>
parents:
29817
diff
changeset
|
808 if left: |
31520
6f150bb19317
templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents:
31519
diff
changeset
|
809 return fillchar * fillwidth + text |
20370
aa51392da507
template: add pad function for padding output
Durham Goode <durham@fb.com>
parents:
20369
diff
changeset
|
810 else: |
31520
6f150bb19317
templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents:
31519
diff
changeset
|
811 return text + fillchar * fillwidth |
20370
aa51392da507
template: add pad function for padding output
Durham Goode <durham@fb.com>
parents:
20369
diff
changeset
|
812 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
813 @templatefunc('indent(text, indentchars[, firstline])') |
25489
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
814 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
|
815 """Indents all non-empty lines |
25489
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
816 with the characters given in the indentchars string. An optional |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
817 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
|
818 if present.""" |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
819 if not (2 <= len(args) <= 3): |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
820 # i18n: "indent" is a keyword |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
821 raise error.ParseError(_("indent() expects two or three arguments")) |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
822 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
823 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
|
824 indent = evalstring(context, mapping, args[1]) |
25489
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
825 |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
826 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
|
827 firstline = evalstring(context, mapping, args[2]) |
25489
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
828 else: |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
829 firstline = indent |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
830 |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
831 # 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
|
832 return templatefilters.indent(firstline + text, indent) |
ef8956aa8755
templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents:
25096
diff
changeset
|
833 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
834 @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
|
835 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
|
836 """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
|
837 are complex types. This function allows you to obtain the value of an |
26197 | 838 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
|
839 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
|
840 # 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
|
841 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
|
842 |
28331
2874db5462d3
templater: fix get() to evaluate arguments eagerly
Yuya Nishihara <yuya@tcha.org>
parents:
28225
diff
changeset
|
843 dictarg = evalfuncarg(context, mapping, args[0]) |
18582
ef78450c8df6
templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
18289
diff
changeset
|
844 if not util.safehasattr(dictarg, 'get'): |
ef78450c8df6
templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
18289
diff
changeset
|
845 # 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
|
846 raise error.ParseError(_("get() expects a dict as first argument")) |
ef78450c8df6
templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
18289
diff
changeset
|
847 |
28331
2874db5462d3
templater: fix get() to evaluate arguments eagerly
Yuya Nishihara <yuya@tcha.org>
parents:
28225
diff
changeset
|
848 key = evalfuncarg(context, mapping, args[1]) |
34536
4c1cfe54c08d
templater: extend dot operator as a short for get(dict, key)
Yuya Nishihara <yuya@tcha.org>
parents:
34535
diff
changeset
|
849 return _getdictitem(dictarg, key) |
4c1cfe54c08d
templater: extend dot operator as a short for get(dict, key)
Yuya Nishihara <yuya@tcha.org>
parents:
34535
diff
changeset
|
850 |
4c1cfe54c08d
templater: extend dot operator as a short for get(dict, key)
Yuya Nishihara <yuya@tcha.org>
parents:
34535
diff
changeset
|
851 def _getdictitem(dictarg, key): |
34534
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34458
diff
changeset
|
852 val = dictarg.get(key) |
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34458
diff
changeset
|
853 if val is None: |
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34458
diff
changeset
|
854 return |
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34458
diff
changeset
|
855 return templatekw.wraphybridvalue(dictarg, key, val) |
18582
ef78450c8df6
templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
18289
diff
changeset
|
856 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
857 @templatefunc('if(expr, then[, else])') |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
858 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
|
859 """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
|
860 an expression.""" |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
861 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
|
862 # i18n: "if" is a keyword |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
863 raise error.ParseError(_("if expects two or three arguments")) |
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
864 |
29816
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
865 test = evalboolean(context, mapping, args[0]) |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
866 if test: |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
867 yield evalrawexp(context, mapping, args[1]) |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
868 elif len(args) == 3: |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
869 yield evalrawexp(context, mapping, args[2]) |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
870 |
30049
f18cc848b48e
templater: use "needle" and "haystack" as (meta-)variables for ifcontains()
Anton Shestakov <av6@dwimlabs.net>
parents:
30008
diff
changeset
|
871 @templatefunc('ifcontains(needle, haystack, then[, else])') |
20518
1e43f15a647f
template: add ifcontains template function
Durham Goode <durham@fb.com>
parents:
20371
diff
changeset
|
872 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
|
873 """Conditionally execute based |
30049
f18cc848b48e
templater: use "needle" and "haystack" as (meta-)variables for ifcontains()
Anton Shestakov <av6@dwimlabs.net>
parents:
30008
diff
changeset
|
874 on whether the item "needle" is in "haystack".""" |
20518
1e43f15a647f
template: add ifcontains template function
Durham Goode <durham@fb.com>
parents:
20371
diff
changeset
|
875 if not (3 <= len(args) <= 4): |
1e43f15a647f
template: add ifcontains template function
Durham Goode <durham@fb.com>
parents:
20371
diff
changeset
|
876 # i18n: "ifcontains" is a keyword |
1e43f15a647f
template: add ifcontains template function
Durham Goode <durham@fb.com>
parents:
20371
diff
changeset
|
877 raise error.ParseError(_("ifcontains expects three or four arguments")) |
1e43f15a647f
template: add ifcontains template function
Durham Goode <durham@fb.com>
parents:
20371
diff
changeset
|
878 |
30049
f18cc848b48e
templater: use "needle" and "haystack" as (meta-)variables for ifcontains()
Anton Shestakov <av6@dwimlabs.net>
parents:
30008
diff
changeset
|
879 haystack = evalfuncarg(context, mapping, args[1]) |
34659
3edfd472f3cb
templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents:
34581
diff
changeset
|
880 try: |
3edfd472f3cb
templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents:
34581
diff
changeset
|
881 needle = evalastype(context, mapping, args[0], |
3edfd472f3cb
templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents:
34581
diff
changeset
|
882 getattr(haystack, 'keytype', None) or bytes) |
3edfd472f3cb
templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents:
34581
diff
changeset
|
883 found = (needle in haystack) |
3edfd472f3cb
templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents:
34581
diff
changeset
|
884 except error.ParseError: |
3edfd472f3cb
templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents:
34581
diff
changeset
|
885 found = False |
20518
1e43f15a647f
template: add ifcontains template function
Durham Goode <durham@fb.com>
parents:
20371
diff
changeset
|
886 |
34659
3edfd472f3cb
templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents:
34581
diff
changeset
|
887 if found: |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
888 yield evalrawexp(context, mapping, args[2]) |
20518
1e43f15a647f
template: add ifcontains template function
Durham Goode <durham@fb.com>
parents:
20371
diff
changeset
|
889 elif len(args) == 4: |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
890 yield evalrawexp(context, mapping, args[3]) |
20518
1e43f15a647f
template: add ifcontains template function
Durham Goode <durham@fb.com>
parents:
20371
diff
changeset
|
891 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
892 @templatefunc('ifeq(expr1, expr2, then[, else])') |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
893 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
|
894 """Conditionally execute based on |
24586
90e3f5d22dad
templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24337
diff
changeset
|
895 whether 2 items are equivalent.""" |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
896 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
|
897 # i18n: "ifeq" is a keyword |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
898 raise error.ParseError(_("ifeq expects three or four arguments")) |
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
899 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
900 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
|
901 match = evalstring(context, mapping, args[1]) |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
902 if test == match: |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
903 yield evalrawexp(context, mapping, args[2]) |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
904 elif len(args) == 4: |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
905 yield evalrawexp(context, mapping, args[3]) |
17636
ce18dbcd91c8
templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents:
17635
diff
changeset
|
906 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
907 @templatefunc('join(list, sep)') |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
908 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
|
909 """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
|
910 if not (1 <= len(args) <= 2): |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
911 # i18n: "join" is a keyword |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
912 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
|
913 |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
914 # TODO: perhaps this should be evalfuncarg(), but it can't because hgweb |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
915 # abuses generator as a keyword that returns a list of dicts. |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
916 joinset = evalrawexp(context, mapping, args[0]) |
34330
89aec1834a86
templatekw: add new-style template expansion to {manifest}
Yuya Nishihara <yuya@tcha.org>
parents:
34329
diff
changeset
|
917 joinset = templatekw.unwrapvalue(joinset) |
34328
dd28b1f55eb8
templatekw: just pass underlying value (or key) to joinfmt() function
Yuya Nishihara <yuya@tcha.org>
parents:
34326
diff
changeset
|
918 joinfmt = getattr(joinset, 'joinfmt', pycompat.identity) |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
919 joiner = " " |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
920 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
|
921 joiner = evalstring(context, mapping, args[1]) |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
922 |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
923 first = True |
36544
a16fceb686a7
py3: fix join(), min(), and max() template functions over string
Yuya Nishihara <yuya@tcha.org>
parents:
36509
diff
changeset
|
924 for x in pycompat.maybebytestr(joinset): |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
925 if first: |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
926 first = False |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
927 else: |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
928 yield joiner |
34328
dd28b1f55eb8
templatekw: just pass underlying value (or key) to joinfmt() function
Yuya Nishihara <yuya@tcha.org>
parents:
34326
diff
changeset
|
929 yield joinfmt(x) |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
930 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
931 @templatefunc('label(label, expr)') |
18289
9bfb53106328
templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents:
17991
diff
changeset
|
932 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
|
933 """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
|
934 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
|
935 automatic colorization.""" |
18289
9bfb53106328
templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents:
17991
diff
changeset
|
936 if len(args) != 2: |
9bfb53106328
templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents:
17991
diff
changeset
|
937 # i18n: "label" is a keyword |
9bfb53106328
templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents:
17991
diff
changeset
|
938 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
|
939 |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
940 ui = context.resource(mapping, 'ui') |
28374
af3bd9d1dbc1
templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents:
28373
diff
changeset
|
941 thing = evalstring(context, mapping, args[1]) |
af3bd9d1dbc1
templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents:
28373
diff
changeset
|
942 # 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
|
943 # etc. don't need to be quoted |
af3bd9d1dbc1
templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents:
28373
diff
changeset
|
944 label = evalstringliteral(context, mapping, args[0]) |
af3bd9d1dbc1
templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents:
28373
diff
changeset
|
945 |
28384
3356bf61fa25
formatter: make labels work with templated output
Kostia Balytskyi <ikostia@fb.com>
parents:
28374
diff
changeset
|
946 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
|
947 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
948 @templatefunc('latesttag([pattern])') |
26485
43bf9471fae9
templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents:
26334
diff
changeset
|
949 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
|
950 """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
|
951 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
|
952 If no such tags exist, the "{tag}" template resolves to |
f0d719e513fc
templatekw: clarify the result of {latesttag} when no tag exists
Matt Harbison <matt_harbison@yahoo.com>
parents:
31807
diff
changeset
|
953 the string "null".""" |
26485
43bf9471fae9
templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents:
26334
diff
changeset
|
954 if len(args) > 1: |
43bf9471fae9
templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents:
26334
diff
changeset
|
955 # 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
|
956 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
|
957 |
43bf9471fae9
templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents:
26334
diff
changeset
|
958 pattern = None |
43bf9471fae9
templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents:
26334
diff
changeset
|
959 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
|
960 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
|
961 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
|
962 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
963 @templatefunc('localdate(date[, tz])') |
26127
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
964 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
|
965 """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
|
966 The default is local date.""" |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
967 if not (1 <= len(args) <= 2): |
26127
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
968 # i18n: "localdate" is a keyword |
26128
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
969 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
|
970 |
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
971 date = evalfuncarg(context, mapping, args[0]) |
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
972 try: |
36607
c6061cadb400
util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents:
36596
diff
changeset
|
973 date = dateutil.parsedate(date) |
26127
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
974 except AttributeError: # not str nor date tuple |
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
975 # i18n: "localdate" is a keyword |
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
976 raise error.ParseError(_("localdate expects a date information")) |
26128
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
977 if len(args) >= 2: |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
978 tzoffset = None |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
979 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
|
980 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
|
981 tzoffset, remainder = dateutil.parsetimezone(tz) |
29636
84ef4517de03
date: refactor timezone parsing
Matt Mackall <mpm@selenic.com>
parents:
29085
diff
changeset
|
982 if remainder: |
84ef4517de03
date: refactor timezone parsing
Matt Mackall <mpm@selenic.com>
parents:
29085
diff
changeset
|
983 tzoffset = None |
26128
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
984 if tzoffset is None: |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
985 try: |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
986 tzoffset = int(tz) |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
987 except (TypeError, ValueError): |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
988 # i18n: "localdate" is a keyword |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
989 raise error.ParseError(_("localdate expects a timezone")) |
51f6940d3b4f
templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents:
26127
diff
changeset
|
990 else: |
36607
c6061cadb400
util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents:
36596
diff
changeset
|
991 tzoffset = dateutil.makedate()[1] |
26127
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
992 return (date[0], tzoffset) |
7012be5ab5bd
templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents:
26125
diff
changeset
|
993 |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
994 @templatefunc('max(iterable)') |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
995 def max_(context, mapping, args, **kwargs): |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
996 """Return the max of an iterable""" |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
997 if len(args) != 1: |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
998 # i18n: "max" is a keyword |
35386
fdd09d87635b
templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents:
34838
diff
changeset
|
999 raise error.ParseError(_("max expects one argument")) |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1000 |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1001 iterable = evalfuncarg(context, mapping, args[0]) |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1002 try: |
36544
a16fceb686a7
py3: fix join(), min(), and max() template functions over string
Yuya Nishihara <yuya@tcha.org>
parents:
36509
diff
changeset
|
1003 x = max(pycompat.maybebytestr(iterable)) |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1004 except (TypeError, ValueError): |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1005 # i18n: "max" is a keyword |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1006 raise error.ParseError(_("max first argument should be an iterable")) |
34534
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34458
diff
changeset
|
1007 return templatekw.wraphybridvalue(iterable, x, x) |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1008 |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1009 @templatefunc('min(iterable)') |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1010 def min_(context, mapping, args, **kwargs): |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1011 """Return the min of an iterable""" |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1012 if len(args) != 1: |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1013 # i18n: "min" is a keyword |
35386
fdd09d87635b
templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents:
34838
diff
changeset
|
1014 raise error.ParseError(_("min expects one argument")) |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1015 |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1016 iterable = evalfuncarg(context, mapping, args[0]) |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1017 try: |
36544
a16fceb686a7
py3: fix join(), min(), and max() template functions over string
Yuya Nishihara <yuya@tcha.org>
parents:
36509
diff
changeset
|
1018 x = min(pycompat.maybebytestr(iterable)) |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1019 except (TypeError, ValueError): |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1020 # i18n: "min" is a keyword |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1021 raise error.ParseError(_("min first argument should be an iterable")) |
34534
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34458
diff
changeset
|
1022 return templatekw.wraphybridvalue(iterable, x, x) |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1023 |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1024 @templatefunc('mod(a, b)') |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1025 def mod(context, mapping, args): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1026 """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
|
1027 if not len(args) == 2: |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1028 # i18n: "mod" is a keyword |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1029 raise error.ParseError(_("mod expects two arguments")) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1030 |
30116
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
1031 func = lambda a, b: a % b |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
1032 return runarithmetic(context, mapping, (func, args[0], args[1])) |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1033 |
34287
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1034 @templatefunc('obsfateoperations(markers)') |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1035 def obsfateoperations(context, mapping, args): |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1036 """Compute obsfate related information based on markers (EXPERIMENTAL)""" |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1037 if len(args) != 1: |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1038 # i18n: "obsfateoperations" is a keyword |
35386
fdd09d87635b
templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents:
34838
diff
changeset
|
1039 raise error.ParseError(_("obsfateoperations expects one argument")) |
34287
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1040 |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1041 markers = evalfuncarg(context, mapping, args[0]) |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1042 |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1043 try: |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1044 data = obsutil.markersoperations(markers) |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1045 return templatekw.hybridlist(data, name='operation') |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1046 except (TypeError, KeyError): |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1047 # i18n: "obsfateoperations" is a keyword |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1048 errmsg = _("obsfateoperations first argument should be an iterable") |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1049 raise error.ParseError(errmsg) |
7cdc8c5a481a
templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34250
diff
changeset
|
1050 |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1051 @templatefunc('obsfatedate(markers)') |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1052 def obsfatedate(context, mapping, args): |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1053 """Compute obsfate related information based on markers (EXPERIMENTAL)""" |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1054 if len(args) != 1: |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1055 # i18n: "obsfatedate" is a keyword |
35386
fdd09d87635b
templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents:
34838
diff
changeset
|
1056 raise error.ParseError(_("obsfatedate expects one argument")) |
33995
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1057 |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1058 markers = evalfuncarg(context, mapping, args[0]) |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1059 |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1060 try: |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1061 data = obsutil.markersdates(markers) |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1062 return templatekw.hybridlist(data, name='date', fmt='%d %d') |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1063 except (TypeError, KeyError): |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1064 # i18n: "obsfatedate" is a keyword |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1065 errmsg = _("obsfatedate first argument should be an iterable") |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1066 raise error.ParseError(errmsg) |
c35c0f54f420
template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents:
33994
diff
changeset
|
1067 |
33994
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1068 @templatefunc('obsfateusers(markers)') |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1069 def obsfateusers(context, mapping, args): |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1070 """Compute obsfate related information based on markers (EXPERIMENTAL)""" |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1071 if len(args) != 1: |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1072 # i18n: "obsfateusers" is a keyword |
35386
fdd09d87635b
templater: fix "one arguments"
Anton Shestakov <av6@dwimlabs.net>
parents:
34838
diff
changeset
|
1073 raise error.ParseError(_("obsfateusers expects one argument")) |
33994
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1074 |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1075 markers = evalfuncarg(context, mapping, args[0]) |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1076 |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1077 try: |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1078 data = obsutil.markersusers(markers) |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1079 return templatekw.hybridlist(data, name='user') |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1080 except (TypeError, KeyError, ValueError): |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1081 # i18n: "obsfateusers" is a keyword |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1082 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
|
1083 "obsmakers") |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1084 raise error.ParseError(msg) |
38f08eaba6b0
template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents:
33993
diff
changeset
|
1085 |
35010
b81ad5b78a81
obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents:
34838
diff
changeset
|
1086 @templatefunc('obsfateverb(successors, markers)') |
33993
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1087 def obsfateverb(context, mapping, args): |
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1088 """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
|
1089 if len(args) != 2: |
33993
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1090 # 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
|
1091 raise error.ParseError(_("obsfateverb expects two arguments")) |
33993
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1092 |
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1093 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
|
1094 markers = evalfuncarg(context, mapping, args[1]) |
33993
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1095 |
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1096 try: |
35010
b81ad5b78a81
obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents:
34838
diff
changeset
|
1097 return obsutil.obsfateverb(successors, markers) |
33993
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1098 except TypeError: |
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1099 # i18n: "obsfateverb" is a keyword |
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1100 errmsg = _("obsfateverb first argument should be countable") |
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1101 raise error.ParseError(errmsg) |
3d0f8918351b
template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents:
33554
diff
changeset
|
1102 |
30083
bd1f043d1ea3
templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents:
30049
diff
changeset
|
1103 @templatefunc('relpath(path)') |
bd1f043d1ea3
templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents:
30049
diff
changeset
|
1104 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
|
1105 """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
|
1106 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
|
1107 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
|
1108 # 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
|
1109 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
|
1110 |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1111 repo = context.resource(mapping, 'ctx').repo() |
30083
bd1f043d1ea3
templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents:
30049
diff
changeset
|
1112 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
|
1113 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
|
1114 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
1115 @templatefunc('revset(query[, formatargs...])') |
20519
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1116 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
|
1117 """Execute a revision set query. See |
24586
90e3f5d22dad
templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24337
diff
changeset
|
1118 :hg:`help revset`.""" |
20519
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1119 if not len(args) > 0: |
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1120 # i18n: "revset" is a keyword |
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1121 raise error.ParseError(_("revset expects one or more arguments")) |
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1122 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
1123 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
|
1124 ctx = context.resource(mapping, 'ctx') |
24337
696ab1a24ae0
templater: replace 'ctx._repo' with 'ctx.repo()'
Matt Harbison <matt_harbison@yahoo.com>
parents:
24306
diff
changeset
|
1125 repo = ctx.repo() |
20519
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1126 |
22304
5678b0e3608f
templater: enable alias predicates to be used in "revset()" function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
21960
diff
changeset
|
1127 def query(expr): |
33554
2943141f5e07
revset: pass repo when passing ui
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33017
diff
changeset
|
1128 m = revsetmod.match(repo.ui, expr, repo=repo) |
24114
fafd9a1284cf
revset: make match function initiate query from full set by default
Yuya Nishihara <yuya@tcha.org>
parents:
23167
diff
changeset
|
1129 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
|
1130 |
20519
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1131 if len(args) > 1: |
28333
41373244f4e5
templater: fix revset() to evaluate format arguments eagerly
Yuya Nishihara <yuya@tcha.org>
parents:
28332
diff
changeset
|
1132 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
|
1133 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
|
1134 revs = list(revs) |
20519
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1135 else: |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1136 cache = context.resource(mapping, 'cache') |
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1137 revsetcache = cache.setdefault("revsetcache", {}) |
20519
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1138 if raw in revsetcache: |
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1139 revs = revsetcache[raw] |
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1140 else: |
22304
5678b0e3608f
templater: enable alias predicates to be used in "revset()" function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
21960
diff
changeset
|
1141 revs = query(raw) |
28178
96f2d50fb9f6
templater: factor out type conversion of revset() result
Yuya Nishihara <yuya@tcha.org>
parents:
27940
diff
changeset
|
1142 revs = list(revs) |
20519
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1143 revsetcache[raw] = revs |
36595
2da414105809
templatekw: switch revset() to new API
Yuya Nishihara <yuya@tcha.org>
parents:
36591
diff
changeset
|
1144 return templatekw.showrevslist(context, mapping, "revision", revs) |
20519
cda9d2b6beab
template: add revset() template function
Durham Goode <durham@fb.com>
parents:
20518
diff
changeset
|
1145 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
1146 @templatefunc('rstdoc(text, style)') |
18747
f5db3092790f
hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
18582
diff
changeset
|
1147 def rstdoc(context, mapping, args): |
30332
318a24b52eeb
spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents:
30232
diff
changeset
|
1148 """Format reStructuredText.""" |
18747
f5db3092790f
hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
18582
diff
changeset
|
1149 if len(args) != 2: |
f5db3092790f
hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
18582
diff
changeset
|
1150 # i18n: "rstdoc" is a keyword |
f5db3092790f
hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
18582
diff
changeset
|
1151 raise error.ParseError(_("rstdoc expects two arguments")) |
f5db3092790f
hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
18582
diff
changeset
|
1152 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
1153 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
|
1154 style = evalstring(context, mapping, args[1]) |
18747
f5db3092790f
hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
18582
diff
changeset
|
1155 |
19079
1e433b5457fd
hgweb: make help verbose again (issue3899)
Alexander Plavin <me@aplavin.ru>
parents:
19058
diff
changeset
|
1156 return minirst.format(text, style=style, keep=['verbose']) |
18747
f5db3092790f
hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
18582
diff
changeset
|
1157 |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
1158 @templatefunc('separate(sep, args)', argspec='sep *args') |
29085
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1159 def separate(context, mapping, args): |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1160 """Add a separator between non-empty arguments.""" |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
1161 if 'sep' not in args: |
29085
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1162 # i18n: "separate" is a keyword |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1163 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
|
1164 |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
1165 sep = evalstring(context, mapping, args['sep']) |
29085
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1166 first = True |
31886
bdda942f4b9c
templater: add support for keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents:
31885
diff
changeset
|
1167 for arg in args['args']: |
29085
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1168 argstr = evalstring(context, mapping, arg) |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1169 if not argstr: |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1170 continue |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1171 if first: |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1172 first = False |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1173 else: |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1174 yield sep |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1175 yield argstr |
df838803c1d4
templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
28957
diff
changeset
|
1176 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
1177 @templatefunc('shortest(node, minlength=4)') |
20369
9c6b86dd2ed2
template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents:
20312
diff
changeset
|
1178 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
|
1179 """Obtain the shortest representation of |
24586
90e3f5d22dad
templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24337
diff
changeset
|
1180 a node.""" |
20369
9c6b86dd2ed2
template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents:
20312
diff
changeset
|
1181 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
|
1182 # i18n: "shortest" is a keyword |
20369
9c6b86dd2ed2
template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents:
20312
diff
changeset
|
1183 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
|
1184 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
1185 node = evalstring(context, mapping, args[0]) |
20369
9c6b86dd2ed2
template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents:
20312
diff
changeset
|
1186 |
9c6b86dd2ed2
template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents:
20312
diff
changeset
|
1187 minlength = 4 |
9c6b86dd2ed2
template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents:
20312
diff
changeset
|
1188 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
|
1189 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
|
1190 # 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
|
1191 _("shortest() expects an integer minlength")) |
20369
9c6b86dd2ed2
template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents:
20312
diff
changeset
|
1192 |
30232
362740e05460
templater: use unfiltered changelog to calculate shortest() at constant time
Yuya Nishihara <yuya@tcha.org>
parents:
30231
diff
changeset
|
1193 # _partialmatch() of filtered changelog could take O(len(repo)) time, |
362740e05460
templater: use unfiltered changelog to calculate shortest() at constant time
Yuya Nishihara <yuya@tcha.org>
parents:
30231
diff
changeset
|
1194 # which would be unacceptably slow. so we look for hash collision in |
362740e05460
templater: use unfiltered changelog to calculate shortest() at constant time
Yuya Nishihara <yuya@tcha.org>
parents:
30231
diff
changeset
|
1195 # unfiltered space, which means some hashes may be slightly longer. |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1196 cl = context.resource(mapping, 'ctx')._repo.unfiltered().changelog |
34250
448725a2ef73
templater: extract shortest() logic from template function
Martin von Zweigbergk <martinvonz@google.com>
parents:
34139
diff
changeset
|
1197 return cl.shortest(node, minlength) |
20369
9c6b86dd2ed2
template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents:
20312
diff
changeset
|
1198 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
1199 @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
|
1200 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
|
1201 """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
|
1202 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
|
1203 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
|
1204 # 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
|
1205 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
|
1206 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
1207 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
|
1208 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
|
1209 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
|
1210 return text.strip(chars) |
867b9957d895
templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents:
19228
diff
changeset
|
1211 return text.strip() |
867b9957d895
templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents:
19228
diff
changeset
|
1212 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
1213 @templatefunc('sub(pattern, replacement, expression)') |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
1214 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
|
1215 """Perform text substitution |
24586
90e3f5d22dad
templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24337
diff
changeset
|
1216 using regular expressions.""" |
19390
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
1217 if len(args) != 3: |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
1218 # i18n: "sub" is a keyword |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
1219 raise error.ParseError(_("sub expects three arguments")) |
3af3a165db18
templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents:
19330
diff
changeset
|
1220 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
1221 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
|
1222 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
|
1223 src = evalstring(context, mapping, args[2]) |
26188
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1224 try: |
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1225 patre = re.compile(pat) |
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1226 except re.error: |
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1227 # i18n: "sub" is a keyword |
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1228 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
|
1229 try: |
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1230 yield patre.sub(rpl, src) |
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1231 except re.error: |
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1232 # i18n: "sub" is a keyword |
662ea52d5dca
templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents:
26128
diff
changeset
|
1233 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
|
1234 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
1235 @templatefunc('startswith(pattern, text)') |
21821
4a445dc5abff
templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents:
21798
diff
changeset
|
1236 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
|
1237 """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
|
1238 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
|
1239 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
|
1240 # i18n: "startswith" is a keyword |
21821
4a445dc5abff
templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents:
21798
diff
changeset
|
1241 raise error.ParseError(_("startswith expects two arguments")) |
4a445dc5abff
templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents:
21798
diff
changeset
|
1242 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
1243 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
|
1244 text = evalstring(context, mapping, args[1]) |
21821
4a445dc5abff
templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents:
21798
diff
changeset
|
1245 if text.startswith(patn): |
4a445dc5abff
templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents:
21798
diff
changeset
|
1246 return text |
4a445dc5abff
templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents:
21798
diff
changeset
|
1247 return '' |
4a445dc5abff
templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents:
21798
diff
changeset
|
1248 |
28696
efa192203623
templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28695
diff
changeset
|
1249 @templatefunc('word(number, text[, separator])') |
21846
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1250 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
|
1251 """Return the nth word from a string.""" |
21846
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1252 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
|
1253 # i18n: "word" is a keyword |
21846
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1254 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
|
1255 % len(args)) |
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1256 |
28343
a6c2310b3827
templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents:
28334
diff
changeset
|
1257 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
|
1258 # i18n: "word" is a keyword |
a6c2310b3827
templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents:
28334
diff
changeset
|
1259 _("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
|
1260 text = evalstring(context, mapping, args[1]) |
21846
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1261 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
|
1262 splitter = evalstring(context, mapping, args[2]) |
21846
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1263 else: |
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1264 splitter = None |
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1265 |
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1266 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
|
1267 if num >= len(tokens) or num < -len(tokens): |
21846
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1268 return '' |
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1269 else: |
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1270 return tokens[num] |
8f23f8096606
templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents:
21822
diff
changeset
|
1271 |
25001
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
1272 # methods to interpret function arguments or inner expressions (e.g. {_(x)}) |
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
1273 exprmethods = { |
25002
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
1274 "integer": lambda e, c: (runinteger, e[1]), |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1275 "string": lambda e, c: (runstring, e[1]), |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1276 "symbol": lambda e, c: (runsymbol, e[1]), |
25596
c1975809a6b5
templater: take any string literals as template, but not for rawstring (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25595
diff
changeset
|
1277 "template": buildtemplate, |
25001
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
1278 "group": lambda e, c: compileexp(e[1], c, exprmethods), |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
1279 ".": buildmember, |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1280 "|": buildfilter, |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1281 "%": buildmap, |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1282 "func": buildfunc, |
31885
d18b624c1c06
templater: add parsing rule for key-value pair
Yuya Nishihara <yuya@tcha.org>
parents:
31884
diff
changeset
|
1283 "keyvalue": buildkeyvaluepair, |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1284 "+": lambda e, c: buildarithmetic(e, c, lambda a, b: a + b), |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1285 "-": lambda e, c: buildarithmetic(e, c, lambda a, b: a - b), |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1286 "negate": buildnegate, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1287 "*": lambda e, c: buildarithmetic(e, c, lambda a, b: a * b), |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
1288 "/": lambda e, c: buildarithmetic(e, c, lambda a, b: a // b), |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1289 } |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1290 |
25001
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
1291 # methods to interpret top-level template (e.g. {x}, {x|_}, {x % "y"}) |
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
1292 methods = exprmethods.copy() |
25002
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
1293 methods["integer"] = exprmethods["symbol"] # '{1}' as variable |
25001
9668c1a433b3
templater: switch methods table on compileexp() of func args and inner expr
Yuya Nishihara <yuya@tcha.org>
parents:
24988
diff
changeset
|
1294 |
28912
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1295 class _aliasrules(parser.basealiasrules): |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1296 """Parsing and expansion rule set of template aliases""" |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1297 _section = _('template alias') |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1298 _parse = staticmethod(_parseexpr) |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1299 |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1300 @staticmethod |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1301 def _trygetfunc(tree): |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1302 """Return (name, args) if tree is func(...) or ...|filter; otherwise |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1303 None""" |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1304 if tree[0] == 'func' and tree[1][0] == 'symbol': |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1305 return tree[1][1], getlist(tree[2]) |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1306 if tree[0] == '|' and tree[2][0] == 'symbol': |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1307 return tree[2][1], [tree[1]] |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1308 |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1309 def expandaliases(tree, aliases): |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1310 """Return new tree of aliases are expanded""" |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1311 aliasmap = _aliasrules.buildmap(aliases) |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1312 return _aliasrules.expand(aliasmap, tree) |
867d6ba2353d
templater: add parsing and expansion rules to process "templatealias" section
Yuya Nishihara <yuya@tcha.org>
parents:
28911
diff
changeset
|
1313 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1314 # template engine |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
1315 |
8360
acc202b71619
templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8312
diff
changeset
|
1316 stringify = templatefilters.stringify |
7107
125c8fedcbe0
Allow hgweb to search for templates in more than one path.
Brendan Cully <brendan@kublai.com>
parents:
6783
diff
changeset
|
1317 |
10850
a63391e26284
templater: use recursive flattening
Matt Mackall <mpm@selenic.com>
parents:
10849
diff
changeset
|
1318 def _flatten(thing): |
a63391e26284
templater: use recursive flattening
Matt Mackall <mpm@selenic.com>
parents:
10849
diff
changeset
|
1319 '''yield a single stream from a possibly nested set of iterators''' |
31880
a0f2d83f8083
templater: remove __iter__() from _hybrid, resolve it explicitly
Yuya Nishihara <yuya@tcha.org>
parents:
31850
diff
changeset
|
1320 thing = templatekw.unwraphybrid(thing) |
32970
11c0bb4ccc76
py3: replace str with bytes in isinstance()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32873
diff
changeset
|
1321 if isinstance(thing, bytes): |
10850
a63391e26284
templater: use recursive flattening
Matt Mackall <mpm@selenic.com>
parents:
10849
diff
changeset
|
1322 yield thing |
34723
b13c95919ff5
templater: explode if we try to emit a str
Augie Fackler <augie@google.com>
parents:
34715
diff
changeset
|
1323 elif isinstance(thing, str): |
b13c95919ff5
templater: explode if we try to emit a str
Augie Fackler <augie@google.com>
parents:
34715
diff
changeset
|
1324 # We can only hit this on Python 3, and it's here to guard |
b13c95919ff5
templater: explode if we try to emit a str
Augie Fackler <augie@google.com>
parents:
34715
diff
changeset
|
1325 # against infinite recursion. |
b13c95919ff5
templater: explode if we try to emit a str
Augie Fackler <augie@google.com>
parents:
34715
diff
changeset
|
1326 raise error.ProgrammingError('Mercurial IO including templates is done' |
b13c95919ff5
templater: explode if we try to emit a str
Augie Fackler <augie@google.com>
parents:
34715
diff
changeset
|
1327 ' with bytes, not strings') |
29815
0d5cc0c18b4e
templater: make it clearer that _flatten() omits None
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
1328 elif thing is None: |
0d5cc0c18b4e
templater: make it clearer that _flatten() omits None
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
1329 pass |
14944
e2c413bde8a5
globally: use safehasattr(x, '__iter__') instead of hasattr(x, '__iter__')
Augie Fackler <durin42@gmail.com>
parents:
14943
diff
changeset
|
1330 elif not util.safehasattr(thing, '__iter__'): |
32974
8779d35c168d
py3: use pycompat.bytestr() in place of str()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32970
diff
changeset
|
1331 yield pycompat.bytestr(thing) |
10852
0d50586a9d31
templater: raise nested functions
Matt Mackall <mpm@selenic.com>
parents:
10850
diff
changeset
|
1332 else: |
10850
a63391e26284
templater: use recursive flattening
Matt Mackall <mpm@selenic.com>
parents:
10849
diff
changeset
|
1333 for i in thing: |
31880
a0f2d83f8083
templater: remove __iter__() from _hybrid, resolve it explicitly
Yuya Nishihara <yuya@tcha.org>
parents:
31850
diff
changeset
|
1334 i = templatekw.unwraphybrid(i) |
32970
11c0bb4ccc76
py3: replace str with bytes in isinstance()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32873
diff
changeset
|
1335 if isinstance(i, bytes): |
10850
a63391e26284
templater: use recursive flattening
Matt Mackall <mpm@selenic.com>
parents:
10849
diff
changeset
|
1336 yield i |
29815
0d5cc0c18b4e
templater: make it clearer that _flatten() omits None
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
1337 elif i is None: |
0d5cc0c18b4e
templater: make it clearer that _flatten() omits None
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
1338 pass |
14944
e2c413bde8a5
globally: use safehasattr(x, '__iter__') instead of hasattr(x, '__iter__')
Augie Fackler <durin42@gmail.com>
parents:
14943
diff
changeset
|
1339 elif not util.safehasattr(i, '__iter__'): |
32974
8779d35c168d
py3: use pycompat.bytestr() in place of str()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32970
diff
changeset
|
1340 yield pycompat.bytestr(i) |
29815
0d5cc0c18b4e
templater: make it clearer that _flatten() omits None
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
1341 else: |
10850
a63391e26284
templater: use recursive flattening
Matt Mackall <mpm@selenic.com>
parents:
10849
diff
changeset
|
1342 for j in _flatten(i): |
a63391e26284
templater: use recursive flattening
Matt Mackall <mpm@selenic.com>
parents:
10849
diff
changeset
|
1343 yield j |
a63391e26284
templater: use recursive flattening
Matt Mackall <mpm@selenic.com>
parents:
10849
diff
changeset
|
1344 |
24988
e8ff0b09acac
templater: rename parsestring() to unquotestring() (API)
Yuya Nishihara <yuya@tcha.org>
parents:
24987
diff
changeset
|
1345 def unquotestring(s): |
28630
bf35644b9f3a
templater: relax unquotestring() to fall back to bare string
Yuya Nishihara <yuya@tcha.org>
parents:
28628
diff
changeset
|
1346 '''unwrap quotes if any; otherwise returns unmodified string''' |
28687
29c249dfb4ef
templater: do not strip non-quote characters from template config
Yuya Nishihara <yuya@tcha.org>
parents:
28630
diff
changeset
|
1347 if len(s) < 2 or s[0] not in "'\"" or s[0] != s[-1]: |
28630
bf35644b9f3a
templater: relax unquotestring() to fall back to bare string
Yuya Nishihara <yuya@tcha.org>
parents:
28628
diff
changeset
|
1348 return s |
25696
c1cac25ad1a6
templater: remove workaround for escaped quoted string in quoted template
Yuya Nishihara <yuya@tcha.org>
parents:
25695
diff
changeset
|
1349 return s[1:-1] |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1350 |
8218
e61cb2813d2a
templater: separate template management and actual string processing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8198
diff
changeset
|
1351 class engine(object): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1352 '''template expansion engine. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1353 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1354 template expansion works like this. a map file contains key=value |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1355 pairs. if value is quoted, it is treated as string. otherwise, it |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1356 is treated as name of template file. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1357 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1358 templater is asked to expand a key in map. it looks up key, and |
4334
66a3fe30f9fc
minor typo fix in templater's docstring
TK Soh <teekaysoh@yahoo.com>
parents:
3904
diff
changeset
|
1359 looks for strings like this: {foo}. it expands {foo} by looking up |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1360 foo in map, and substituting it. expansion is recursive: it stops |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1361 when there is no more {foo} to replace. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1362 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1363 expansion also allows formatting and filtering. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1364 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1365 format uses key to expand each item in list. syntax is |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1366 {key%format}. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1367 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1368 filter uses function to transform value. syntax is |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1369 {key|filter1|filter2|...}.''' |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1370 |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1371 def __init__(self, loader, filters=None, defaults=None, resources=None, |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1372 aliases=()): |
10848
01346cea5485
templater: privatize class variables
Matt Mackall <mpm@selenic.com>
parents:
10847
diff
changeset
|
1373 self._loader = loader |
26330
ec4f3755d997
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26234
diff
changeset
|
1374 if filters is None: |
ec4f3755d997
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26234
diff
changeset
|
1375 filters = {} |
10848
01346cea5485
templater: privatize class variables
Matt Mackall <mpm@selenic.com>
parents:
10847
diff
changeset
|
1376 self._filters = filters |
26331
2c6a741bf05e
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26330
diff
changeset
|
1377 if defaults is None: |
2c6a741bf05e
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26330
diff
changeset
|
1378 defaults = {} |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1379 if resources is None: |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1380 resources = {} |
10848
01346cea5485
templater: privatize class variables
Matt Mackall <mpm@selenic.com>
parents:
10847
diff
changeset
|
1381 self._defaults = defaults |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1382 self._resources = resources |
28957
d813132ea361
templater: load and expand aliases by template engine (API) (issue4842)
Yuya Nishihara <yuya@tcha.org>
parents:
28956
diff
changeset
|
1383 self._aliasmap = _aliasrules.buildmap(aliases) |
28545
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
1384 self._cache = {} # key: (func, data) |
8218
e61cb2813d2a
templater: separate template management and actual string processing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8198
diff
changeset
|
1385 |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1386 def symbol(self, mapping, key): |
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1387 """Resolve symbol to value or function; None if nothing found""" |
35470
a33be093ec62
templater: look up symbols/resources as if they were separated (issue5699)
Yuya Nishihara <yuya@tcha.org>
parents:
35468
diff
changeset
|
1388 v = None |
a33be093ec62
templater: look up symbols/resources as if they were separated (issue5699)
Yuya Nishihara <yuya@tcha.org>
parents:
35468
diff
changeset
|
1389 if key not in self._resources: |
a33be093ec62
templater: look up symbols/resources as if they were separated (issue5699)
Yuya Nishihara <yuya@tcha.org>
parents:
35468
diff
changeset
|
1390 v = mapping.get(key) |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1391 if v is None: |
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1392 v = self._defaults.get(key) |
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1393 return v |
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1394 |
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1395 def resource(self, mapping, key): |
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1396 """Return internal data (e.g. cache) used for keyword/function |
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1397 evaluation""" |
35470
a33be093ec62
templater: look up symbols/resources as if they were separated (issue5699)
Yuya Nishihara <yuya@tcha.org>
parents:
35468
diff
changeset
|
1398 v = None |
a33be093ec62
templater: look up symbols/resources as if they were separated (issue5699)
Yuya Nishihara <yuya@tcha.org>
parents:
35468
diff
changeset
|
1399 if key in self._resources: |
a33be093ec62
templater: look up symbols/resources as if they were separated (issue5699)
Yuya Nishihara <yuya@tcha.org>
parents:
35468
diff
changeset
|
1400 v = mapping.get(key) |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1401 if v is None: |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1402 v = self._resources.get(key) |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1403 if v is None: |
36444
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36443
diff
changeset
|
1404 raise ResourceUnavailable(_('template resource not available: %s') |
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36443
diff
changeset
|
1405 % key) |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1406 return v |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
1407 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1408 def _load(self, t): |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1409 '''load, parse, and cache a template''' |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1410 if t not in self._cache: |
27940
cfe7da66f555
templater: abort if infinite recursion detected while compiling
Yuya Nishihara <yuya@tcha.org>
parents:
27939
diff
changeset
|
1411 # put poison to cut recursion while compiling 't' |
28545
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
1412 self._cache[t] = (_runrecursivesymbol, t) |
27940
cfe7da66f555
templater: abort if infinite recursion detected while compiling
Yuya Nishihara <yuya@tcha.org>
parents:
27939
diff
changeset
|
1413 try: |
28956
eea98190ed73
templater: inline compiletemplate() function into engine
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
1414 x = parse(self._loader(t)) |
28957
d813132ea361
templater: load and expand aliases by template engine (API) (issue4842)
Yuya Nishihara <yuya@tcha.org>
parents:
28956
diff
changeset
|
1415 if self._aliasmap: |
d813132ea361
templater: load and expand aliases by template engine (API) (issue4842)
Yuya Nishihara <yuya@tcha.org>
parents:
28956
diff
changeset
|
1416 x = _aliasrules.expand(self._aliasmap, x) |
28956
eea98190ed73
templater: inline compiletemplate() function into engine
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
1417 self._cache[t] = compileexp(x, self, methods) |
27940
cfe7da66f555
templater: abort if infinite recursion detected while compiling
Yuya Nishihara <yuya@tcha.org>
parents:
27939
diff
changeset
|
1418 except: # re-raises |
cfe7da66f555
templater: abort if infinite recursion detected while compiling
Yuya Nishihara <yuya@tcha.org>
parents:
27939
diff
changeset
|
1419 del self._cache[t] |
cfe7da66f555
templater: abort if infinite recursion detected while compiling
Yuya Nishihara <yuya@tcha.org>
parents:
27939
diff
changeset
|
1420 raise |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1421 return self._cache[t] |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
1422 |
10853
b6f6d9fd53d6
templater: drop raw method
Matt Mackall <mpm@selenic.com>
parents:
10852
diff
changeset
|
1423 def process(self, t, mapping): |
b6f6d9fd53d6
templater: drop raw method
Matt Mackall <mpm@selenic.com>
parents:
10852
diff
changeset
|
1424 '''Perform expansion. t is name of map element to expand. |
b6f6d9fd53d6
templater: drop raw method
Matt Mackall <mpm@selenic.com>
parents:
10852
diff
changeset
|
1425 mapping contains added elements for use during expansion. Is a |
b6f6d9fd53d6
templater: drop raw method
Matt Mackall <mpm@selenic.com>
parents:
10852
diff
changeset
|
1426 generator.''' |
28545
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
1427 func, data = self._load(t) |
1d461ee26e1b
templater: lift parsed and compiled templates to generic data types
Yuya Nishihara <yuya@tcha.org>
parents:
28462
diff
changeset
|
1428 return _flatten(func(self, mapping, data)) |
8218
e61cb2813d2a
templater: separate template management and actual string processing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8198
diff
changeset
|
1429 |
8361
d8c5a7f25a40
templater: make the templating engine pluggable to some extent
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8360
diff
changeset
|
1430 engines = {'default': engine} |
d8c5a7f25a40
templater: make the templating engine pluggable to some extent
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8360
diff
changeset
|
1431 |
19125
6ba6e345961e
templater: show the style list when I try to use a wrong one
Iulian Stana <julian.stana@gmail.com>
parents:
19079
diff
changeset
|
1432 def stylelist(): |
22634
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1433 paths = templatepaths() |
20312
268a5ab5c27b
templater: selecting a style with no templates does not crash (issue4140)
Simon Heimberg <simohe@besonet.ch>
parents:
20067
diff
changeset
|
1434 if not paths: |
268a5ab5c27b
templater: selecting a style with no templates does not crash (issue4140)
Simon Heimberg <simohe@besonet.ch>
parents:
20067
diff
changeset
|
1435 return _('no templates found, try `hg debuginstall` for more info') |
27637
b502138f5faa
cleanup: remove superfluous space after space after equals (python)
timeless <timeless@mozdev.org>
parents:
27293
diff
changeset
|
1436 dirlist = os.listdir(paths[0]) |
19125
6ba6e345961e
templater: show the style list when I try to use a wrong one
Iulian Stana <julian.stana@gmail.com>
parents:
19079
diff
changeset
|
1437 stylelist = [] |
6ba6e345961e
templater: show the style list when I try to use a wrong one
Iulian Stana <julian.stana@gmail.com>
parents:
19079
diff
changeset
|
1438 for file in dirlist: |
6ba6e345961e
templater: show the style list when I try to use a wrong one
Iulian Stana <julian.stana@gmail.com>
parents:
19079
diff
changeset
|
1439 split = file.split(".") |
28403
d2e154dddb6e
templater: ignore orig/rej files
timeless <timeless@mozdev.org>
parents:
28384
diff
changeset
|
1440 if split[-1] in ('orig', 'rej'): |
d2e154dddb6e
templater: ignore orig/rej files
timeless <timeless@mozdev.org>
parents:
28384
diff
changeset
|
1441 continue |
19125
6ba6e345961e
templater: show the style list when I try to use a wrong one
Iulian Stana <julian.stana@gmail.com>
parents:
19079
diff
changeset
|
1442 if split[0] == "map-cmdline": |
6ba6e345961e
templater: show the style list when I try to use a wrong one
Iulian Stana <julian.stana@gmail.com>
parents:
19079
diff
changeset
|
1443 stylelist.append(split[1]) |
19127
d982edcfe7f0
templater: fix output instability from gsoc patches
Augie Fackler <raf@durin42.com>
parents:
19125
diff
changeset
|
1444 return ", ".join(sorted(stylelist)) |
19125
6ba6e345961e
templater: show the style list when I try to use a wrong one
Iulian Stana <julian.stana@gmail.com>
parents:
19079
diff
changeset
|
1445 |
28953
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1446 def _readmapfile(mapfile): |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1447 """Load template elements from the given map file""" |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1448 if not os.path.exists(mapfile): |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1449 raise error.Abort(_("style '%s' not found") % mapfile, |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1450 hint=_("available styles: %s") % stylelist()) |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1451 |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1452 base = os.path.dirname(mapfile) |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1453 conf = config.config(includepaths=templatepaths()) |
34714
f4aeb952ab77
templater: load template fragments from [templates] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34712
diff
changeset
|
1454 conf.read(mapfile, remap={'': 'templates'}) |
28953
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1455 |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1456 cache = {} |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1457 tmap = {} |
34715
f17a0e18c47e
templater: load aliases from [templatealias] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34714
diff
changeset
|
1458 aliases = [] |
34712
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1459 |
34714
f4aeb952ab77
templater: load template fragments from [templates] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34712
diff
changeset
|
1460 val = conf.get('templates', '__base__') |
34712
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1461 if val and val[0] not in "'\"": |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1462 # treat as a pointer to a base class for this style |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1463 path = util.normpath(os.path.join(base, val)) |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1464 |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1465 # fallback check in template paths |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1466 if not os.path.exists(path): |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1467 for p in templatepaths(): |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1468 p2 = util.normpath(os.path.join(p, val)) |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1469 if os.path.isfile(p2): |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1470 path = p2 |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1471 break |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1472 p3 = util.normpath(os.path.join(p2, "map")) |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1473 if os.path.isfile(p3): |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1474 path = p3 |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1475 break |
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1476 |
34715
f17a0e18c47e
templater: load aliases from [templatealias] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34714
diff
changeset
|
1477 cache, tmap, aliases = _readmapfile(path) |
34712
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1478 |
34714
f4aeb952ab77
templater: load template fragments from [templates] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34712
diff
changeset
|
1479 for key, val in conf['templates'].items(): |
28953
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1480 if not val: |
34714
f4aeb952ab77
templater: load template fragments from [templates] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34712
diff
changeset
|
1481 raise error.ParseError(_('missing value'), |
f4aeb952ab77
templater: load template fragments from [templates] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34712
diff
changeset
|
1482 conf.source('templates', key)) |
28953
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1483 if val[0] in "'\"": |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1484 if val[0] != val[-1]: |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1485 raise error.ParseError(_('unmatched quotes'), |
34714
f4aeb952ab77
templater: load template fragments from [templates] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34712
diff
changeset
|
1486 conf.source('templates', key)) |
28953
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1487 cache[key] = unquotestring(val) |
34712
56f085334611
templater: simplify merge of __base__ dicts by reading it first
Yuya Nishihara <yuya@tcha.org>
parents:
34693
diff
changeset
|
1488 elif key != '__base__': |
28953
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1489 val = 'default', val |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1490 if ':' in val[1]: |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1491 val = val[1].split(':', 1) |
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1492 tmap[key] = val[0], os.path.join(base, val[1]) |
34715
f17a0e18c47e
templater: load aliases from [templatealias] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34714
diff
changeset
|
1493 aliases.extend(conf['templatealias'].items()) |
f17a0e18c47e
templater: load aliases from [templatealias] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34714
diff
changeset
|
1494 return cache, tmap, aliases |
28953
7f6b8ec691e3
templater: extract function that loads template map file
Yuya Nishihara <yuya@tcha.org>
parents:
28952
diff
changeset
|
1495 |
8218
e61cb2813d2a
templater: separate template management and actual string processing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8198
diff
changeset
|
1496 class templater(object): |
e61cb2813d2a
templater: separate template management and actual string processing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8198
diff
changeset
|
1497 |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1498 def __init__(self, filters=None, defaults=None, resources=None, |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1499 cache=None, aliases=(), minchunk=1024, maxchunk=65536): |
35481
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1500 """Create template engine optionally with preloaded template fragments |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1501 |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1502 - ``filters``: a dict of functions to transform a value into another. |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1503 - ``defaults``: a dict of symbol values/functions; may be overridden |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1504 by a ``mapping`` dict. |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1505 - ``resources``: a dict of internal data (e.g. cache), inaccessible |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1506 from user template; may be overridden by a ``mapping`` dict. |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1507 - ``cache``: a dict of preloaded template fragments. |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1508 - ``aliases``: a list of alias (name, replacement) pairs. |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1509 |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1510 self.cache may be updated later to register additional template |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1511 fragments. |
964510dcdc2a
templater: rewrite docstring of templater.__init__()
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
1512 """ |
26332
66221730d372
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26331
diff
changeset
|
1513 if filters is None: |
66221730d372
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26331
diff
changeset
|
1514 filters = {} |
26333
727d57e94cda
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26332
diff
changeset
|
1515 if defaults is None: |
727d57e94cda
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26332
diff
changeset
|
1516 defaults = {} |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1517 if resources is None: |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1518 resources = {} |
26334
0a5a774f5956
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26333
diff
changeset
|
1519 if cache is None: |
0a5a774f5956
templater: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26333
diff
changeset
|
1520 cache = {} |
1975
6e1a8ea5d717
Duplicate cache when creating templater.
Shun-ichi Goto <shunichi.goto@gmail.com>
parents:
1964
diff
changeset
|
1521 self.cache = cache.copy() |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1522 self.map = {} |
8360
acc202b71619
templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8312
diff
changeset
|
1523 self.filters = templatefilters.filters.copy() |
acc202b71619
templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8312
diff
changeset
|
1524 self.filters.update(filters) |
1964
778281d46bb2
fix template bug that made hgweb break.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1955
diff
changeset
|
1525 self.defaults = defaults |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1526 self._resources = {'templ': self} |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1527 self._resources.update(resources) |
28957
d813132ea361
templater: load and expand aliases by template engine (API) (issue4842)
Yuya Nishihara <yuya@tcha.org>
parents:
28956
diff
changeset
|
1528 self._aliases = aliases |
7396
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7107
diff
changeset
|
1529 self.minchunk, self.maxchunk = minchunk, maxchunk |
13187
e3b87fb34d00
templater: clarify engine caching
Matt Mackall <mpm@selenic.com>
parents:
13176
diff
changeset
|
1530 self.ecache = {} |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1531 |
28954
f97a0bcfd7a1
templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents:
28953
diff
changeset
|
1532 @classmethod |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1533 def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None, |
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1534 cache=None, minchunk=1024, maxchunk=65536): |
28954
f97a0bcfd7a1
templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents:
28953
diff
changeset
|
1535 """Create templater from the specified map file""" |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1536 t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk) |
34715
f17a0e18c47e
templater: load aliases from [templatealias] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34714
diff
changeset
|
1537 cache, tmap, aliases = _readmapfile(mapfile) |
28954
f97a0bcfd7a1
templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents:
28953
diff
changeset
|
1538 t.cache.update(cache) |
f97a0bcfd7a1
templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents:
28953
diff
changeset
|
1539 t.map = tmap |
34715
f17a0e18c47e
templater: load aliases from [templatealias] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34714
diff
changeset
|
1540 t._aliases = aliases |
28954
f97a0bcfd7a1
templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents:
28953
diff
changeset
|
1541 return t |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1542 |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
1543 def __contains__(self, key): |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
1544 return key in self.cache or key in self.map |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
1545 |
8218
e61cb2813d2a
templater: separate template management and actual string processing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8198
diff
changeset
|
1546 def load(self, t): |
6783
6d824dc86907
templater: make a template a string-only iterator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6434
diff
changeset
|
1547 '''Get the template for the given template name. Use a local cache.''' |
16686
67964cda8701
cleanup: "not x in y" -> "x not in y"
Brodie Rao <brodie@sf.io>
parents:
14944
diff
changeset
|
1548 if t not in self.cache: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
1549 try: |
14168
135e244776f0
prevent transient leaks of file handle by using new helper functions
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13665
diff
changeset
|
1550 self.cache[t] = util.readfile(self.map[t][1]) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25654
diff
changeset
|
1551 except KeyError as inst: |
19770
0361163efbaf
templater: support using templates with non-standard names from map file
Alexander Plavin <alexander@plav.in>
parents:
19390
diff
changeset
|
1552 raise TemplateNotFound(_('"%s" not in template map') % |
0361163efbaf
templater: support using templates with non-standard names from map file
Alexander Plavin <alexander@plav.in>
parents:
19390
diff
changeset
|
1553 inst.args[0]) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25654
diff
changeset
|
1554 except IOError as inst: |
36500
43e108027b0d
py3: move between bytes and unicode when re-raising IOError
Yuya Nishihara <yuya@tcha.org>
parents:
36445
diff
changeset
|
1555 reason = (_('template file %s: %s') |
43e108027b0d
py3: move between bytes and unicode when re-raising IOError
Yuya Nishihara <yuya@tcha.org>
parents:
36445
diff
changeset
|
1556 % (self.map[t][1], util.forcebytestr(inst.args[1]))) |
43e108027b0d
py3: move between bytes and unicode when re-raising IOError
Yuya Nishihara <yuya@tcha.org>
parents:
36445
diff
changeset
|
1557 raise IOError(inst.args[0], encoding.strfromlocal(reason)) |
6783
6d824dc86907
templater: make a template a string-only iterator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6434
diff
changeset
|
1558 return self.cache[t] |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1559 |
32873
2ecce24dfcd3
templater: add simple interface for unnamed template (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32684
diff
changeset
|
1560 def render(self, mapping): |
2ecce24dfcd3
templater: add simple interface for unnamed template (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32684
diff
changeset
|
1561 """Render the default unnamed template and return result as string""" |
34346
46f45b7efa30
py3: use pycompat.strkwargs() before passing a dict as keyword argument
Pulkit Goyal <7895pulkit@gmail.com>
parents:
34330
diff
changeset
|
1562 mapping = pycompat.strkwargs(mapping) |
32873
2ecce24dfcd3
templater: add simple interface for unnamed template (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32684
diff
changeset
|
1563 return stringify(self('', **mapping)) |
2ecce24dfcd3
templater: add simple interface for unnamed template (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32684
diff
changeset
|
1564 |
10847 | 1565 def __call__(self, t, **mapping): |
33016
4e6dc34b5d7a
py3: convert kwargs keys' back to bytes using pycompat.byteskwargs()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32974
diff
changeset
|
1566 mapping = pycompat.byteskwargs(mapping) |
8361
d8c5a7f25a40
templater: make the templating engine pluggable to some extent
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8360
diff
changeset
|
1567 ttype = t in self.map and self.map[t][0] or 'default' |
13187
e3b87fb34d00
templater: clarify engine caching
Matt Mackall <mpm@selenic.com>
parents:
13176
diff
changeset
|
1568 if ttype not in self.ecache: |
28831
6b86ce3e3576
templater: give better error message for invalid engine type
Yuya Nishihara <yuya@tcha.org>
parents:
28696
diff
changeset
|
1569 try: |
6b86ce3e3576
templater: give better error message for invalid engine type
Yuya Nishihara <yuya@tcha.org>
parents:
28696
diff
changeset
|
1570 ecls = engines[ttype] |
6b86ce3e3576
templater: give better error message for invalid engine type
Yuya Nishihara <yuya@tcha.org>
parents:
28696
diff
changeset
|
1571 except KeyError: |
6b86ce3e3576
templater: give better error message for invalid engine type
Yuya Nishihara <yuya@tcha.org>
parents:
28696
diff
changeset
|
1572 raise error.Abort(_('invalid template engine: %s') % ttype) |
28957
d813132ea361
templater: load and expand aliases by template engine (API) (issue4842)
Yuya Nishihara <yuya@tcha.org>
parents:
28956
diff
changeset
|
1573 self.ecache[ttype] = ecls(self.load, self.filters, self.defaults, |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
1574 self._resources, self._aliases) |
13187
e3b87fb34d00
templater: clarify engine caching
Matt Mackall <mpm@selenic.com>
parents:
13176
diff
changeset
|
1575 proc = self.ecache[ttype] |
8361
d8c5a7f25a40
templater: make the templating engine pluggable to some extent
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8360
diff
changeset
|
1576 |
10847 | 1577 stream = proc.process(t, mapping) |
7396
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7107
diff
changeset
|
1578 if self.minchunk: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7107
diff
changeset
|
1579 stream = util.increasingchunks(stream, min=self.minchunk, |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7107
diff
changeset
|
1580 max=self.maxchunk) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7107
diff
changeset
|
1581 return stream |
7434
cf7741aa1e96
kill some trailing spaces
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7396
diff
changeset
|
1582 |
22634
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1583 def templatepaths(): |
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1584 '''return locations used for template files.''' |
22636
d844e220792a
templater: don't search randomly for templates - trust util.datapath
Mads Kiilerich <madski@unity3d.com>
parents:
22635
diff
changeset
|
1585 pathsrel = ['templates'] |
22635
660861a6fad4
templater: inline global 'path' list in templatepaths
Mads Kiilerich <madski@unity3d.com>
parents:
22634
diff
changeset
|
1586 paths = [os.path.normpath(os.path.join(util.datapath, f)) |
660861a6fad4
templater: inline global 'path' list in templatepaths
Mads Kiilerich <madski@unity3d.com>
parents:
22634
diff
changeset
|
1587 for f in pathsrel] |
660861a6fad4
templater: inline global 'path' list in templatepaths
Mads Kiilerich <madski@unity3d.com>
parents:
22634
diff
changeset
|
1588 return [p for p in paths if os.path.isdir(p)] |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
1589 |
22634
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1590 def templatepath(name): |
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1591 '''return location of template file. returns None if not found.''' |
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1592 for p in templatepaths(): |
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1593 f = os.path.join(p, name) |
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1594 if os.path.exists(f): |
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1595 return f |
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1596 return None |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
1597 |
9842
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1598 def stylemap(styles, paths=None): |
7966
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1599 """Return path to mapfile for a given style. |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1600 |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1601 Searches mapfile in the following locations: |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1602 1. templatepath/style/map |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1603 2. templatepath/map-style |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1604 3. templatepath/map |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1605 """ |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1606 |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1607 if paths is None: |
22634
e48a5d3996c2
templater: introduce templatepaths for getting paths searched for templates
Mads Kiilerich <madski@unity3d.com>
parents:
22633
diff
changeset
|
1608 paths = templatepaths() |
36550
7f6be7121b28
py3: replace type 'str' by 'bytes' in templater.py
Yuya Nishihara <yuya@tcha.org>
parents:
36546
diff
changeset
|
1609 elif isinstance(paths, bytes): |
8223
02145b700fe4
templater: fix little problem from stylemap() changes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8218
diff
changeset
|
1610 paths = [paths] |
7966
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1611 |
36550
7f6be7121b28
py3: replace type 'str' by 'bytes' in templater.py
Yuya Nishihara <yuya@tcha.org>
parents:
36546
diff
changeset
|
1612 if isinstance(styles, bytes): |
9842
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1613 styles = [styles] |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1614 |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1615 for style in styles: |
24296
b73a22d1d9bf
hgweb: prevent loading style map from directories other than specified paths
Yuya Nishihara <yuya@tcha.org>
parents:
23167
diff
changeset
|
1616 # only plain name is allowed to honor template paths |
b73a22d1d9bf
hgweb: prevent loading style map from directories other than specified paths
Yuya Nishihara <yuya@tcha.org>
parents:
23167
diff
changeset
|
1617 if (not style |
36648
6585ac350fd9
py3: make os.curdir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
36647
diff
changeset
|
1618 or style in (pycompat.oscurdir, pycompat.ospardir) |
30615
bb77654dc7ae
py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30332
diff
changeset
|
1619 or pycompat.ossep in style |
30625
bcf4a975f93d
py3: replace os.altsep with pycompat.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30615
diff
changeset
|
1620 or pycompat.osaltsep and pycompat.osaltsep in style): |
9842
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1621 continue |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1622 locations = [os.path.join(style, 'map'), 'map-' + style] |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1623 locations.append('map') |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1624 |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1625 for path in paths: |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1626 for location in locations: |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1627 mapfile = os.path.join(path, location) |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1628 if os.path.isfile(mapfile): |
d3dbdca92458
hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
9372
diff
changeset
|
1629 return style, mapfile |
7966
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1630 |
aa983c3d94a9
templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7434
diff
changeset
|
1631 raise RuntimeError("No hgweb templates found in %r" % paths) |
24601
d80819f67d59
templater: tell hggettext to collect help of template functions
Yuya Nishihara <yuya@tcha.org>
parents:
24586
diff
changeset
|
1632 |
28695
cc103bd0dbf9
registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28687
diff
changeset
|
1633 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
|
1634 """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
|
1635 """ |
cc103bd0dbf9
registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28687
diff
changeset
|
1636 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
|
1637 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
|
1638 |
24601
d80819f67d59
templater: tell hggettext to collect help of template functions
Yuya Nishihara <yuya@tcha.org>
parents:
24586
diff
changeset
|
1639 # 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
|
1640 i18nfunctions = funcs.values() |