author | Matt Mackall <mpm@selenic.com> |
Mon, 24 Mar 2008 11:12:57 -0500 | |
changeset 6378 | f0f830114a9b |
parent 6337 | d2713d902524 |
child 6434 | 62e0bb41e682 |
permissions | -rw-r--r-- |
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 |
# |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
7 |
|
3891 | 8 |
from i18n import _ |
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
9 |
import re, sys, os |
6337
d2713d902524
give better error message on non-existent mapfile (issue813)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5976
diff
changeset
|
10 |
from mercurial import util |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
11 |
|
1902
1cc5f25653a3
make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1901
diff
changeset
|
12 |
def parsestring(s, quoted=True): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
13 |
'''parse a string using simple c-like syntax. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
14 |
string must be in quotes if quoted is True.''' |
1902
1cc5f25653a3
make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1901
diff
changeset
|
15 |
if quoted: |
3639
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
16 |
if len(s) < 2 or s[0] != s[-1]: |
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
17 |
raise SyntaxError(_('unmatched quotes')) |
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
18 |
return s[1:-1].decode('string_escape') |
3632
231393b7316f
templater: use str.decode in parse_string
Matt Mackall <mpm@selenic.com>
parents:
3462
diff
changeset
|
19 |
|
231393b7316f
templater: use str.decode in parse_string
Matt Mackall <mpm@selenic.com>
parents:
3462
diff
changeset
|
20 |
return s.decode('string_escape') |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
21 |
|
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
22 |
class templater(object): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
23 |
'''template expansion engine. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
24 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
25 |
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
|
26 |
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
|
27 |
is treated as name of template file. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
28 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
29 |
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
|
30 |
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
|
31 |
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
|
32 |
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
|
33 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
34 |
expansion also allows formatting and filtering. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
35 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
36 |
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
|
37 |
{key%format}. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
38 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
39 |
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
|
40 |
{key|filter1|filter2|...}.''' |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
41 |
|
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
42 |
template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))" |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
43 |
r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
44 |
|
1964
778281d46bb2
fix template bug that made hgweb break.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1955
diff
changeset
|
45 |
def __init__(self, mapfile, filters={}, defaults={}, cache={}): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
46 |
'''set up template engine. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
47 |
mapfile is name of file to read map definitions from. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
48 |
filters is dict of functions. each transforms a value into another. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
49 |
defaults is dict of default map definitions.''' |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
50 |
self.mapfile = mapfile or 'template' |
1975
6e1a8ea5d717
Duplicate cache when creating templater.
Shun-ichi Goto <shunichi.goto@gmail.com>
parents:
1964
diff
changeset
|
51 |
self.cache = cache.copy() |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
52 |
self.map = {} |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
53 |
self.base = (mapfile and os.path.dirname(mapfile)) or '' |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
54 |
self.filters = filters |
1964
778281d46bb2
fix template bug that made hgweb break.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1955
diff
changeset
|
55 |
self.defaults = defaults |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
56 |
|
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
57 |
if not mapfile: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
58 |
return |
6337
d2713d902524
give better error message on non-existent mapfile (issue813)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5976
diff
changeset
|
59 |
if not os.path.exists(mapfile): |
d2713d902524
give better error message on non-existent mapfile (issue813)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5976
diff
changeset
|
60 |
raise util.Abort(_('style not found: %s') % mapfile) |
d2713d902524
give better error message on non-existent mapfile (issue813)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5976
diff
changeset
|
61 |
|
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
62 |
i = 0 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
63 |
for l in file(mapfile): |
1914
a5bf0030df5f
make --style=compact look for map-cmdline.compact.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1913
diff
changeset
|
64 |
l = l.strip() |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
65 |
i += 1 |
1914
a5bf0030df5f
make --style=compact look for map-cmdline.compact.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1913
diff
changeset
|
66 |
if not l or l[0] in '#;': continue |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
67 |
m = re.match(r'([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.+)$', l) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
68 |
if m: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
69 |
key, val = m.groups() |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
70 |
if val[0] in "'\"": |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
71 |
try: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
72 |
self.cache[key] = parsestring(val) |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
73 |
except SyntaxError, inst: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
74 |
raise SyntaxError('%s:%s: %s' % |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
75 |
(mapfile, i, inst.args[0])) |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
76 |
else: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
77 |
self.map[key] = os.path.join(self.base, val) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
78 |
else: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
79 |
raise SyntaxError(_("%s:%s: parse error") % (mapfile, i)) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
80 |
|
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
81 |
def __contains__(self, key): |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
82 |
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
|
83 |
|
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
84 |
def __call__(self, t, **map): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
85 |
'''perform expansion. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
86 |
t is name of map element to expand. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
87 |
map is added elements to use during expansion.''' |
5915
d0576d065993
Prefer i in d over d.has_key(i)
Christian Ebert <blacktrash@gmx.net>
parents:
5269
diff
changeset
|
88 |
if not t in self.cache: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
89 |
try: |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
90 |
self.cache[t] = file(self.map[t]).read() |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
91 |
except IOError, inst: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
92 |
raise IOError(inst.args[0], _('template file %s: %s') % |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
93 |
(self.map[t], inst.args[1])) |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
94 |
tmpl = self.cache[t] |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
95 |
|
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
96 |
while tmpl: |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
97 |
m = self.template_re.search(tmpl) |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
98 |
if not m: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
99 |
yield tmpl |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
100 |
break |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
101 |
|
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
102 |
start, end = m.span(0) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
103 |
key, format, fl = m.groups() |
3636
dfbbb33f3fa3
templater: simplify template function
Matt Mackall <mpm@selenic.com>
parents:
3635
diff
changeset
|
104 |
|
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
105 |
if start: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
106 |
yield tmpl[:start] |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
107 |
tmpl = tmpl[end:] |
3636
dfbbb33f3fa3
templater: simplify template function
Matt Mackall <mpm@selenic.com>
parents:
3635
diff
changeset
|
108 |
|
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
109 |
if key in map: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
110 |
v = map[key] |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
111 |
else: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
112 |
v = self.defaults.get(key, "") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
113 |
if callable(v): |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
114 |
v = v(**map) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
115 |
if format: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
116 |
if not hasattr(v, '__iter__'): |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
117 |
raise SyntaxError(_("Error expanding '%s%s'") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
118 |
% (key, format)) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
119 |
lm = map.copy() |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
120 |
for i in v: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
121 |
lm.update(i) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
122 |
yield self(format, **lm) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
123 |
else: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
124 |
if fl: |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
125 |
for f in fl.split("|")[1:]: |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
126 |
v = self.filters[f](v) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
127 |
yield v |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
128 |
|
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
129 |
def templatepath(name=None): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
130 |
'''return location of template file or directory (if no name). |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
131 |
returns None if not found.''' |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
132 |
|
2001
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
133 |
# executable version (py2exe) doesn't support __file__ |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
134 |
if hasattr(sys, 'frozen'): |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
135 |
module = sys.executable |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
136 |
else: |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
137 |
module = __file__ |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
138 |
for f in 'templates', '../templates': |
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
139 |
fl = f.split('/') |
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
140 |
if name: fl.append(name) |
2001
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
141 |
p = os.path.join(os.path.dirname(module), *fl) |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
142 |
if (name and os.path.exists(p)) or os.path.isdir(p): |
1897
58b6784cf9f1
move hgweb.templatepath into templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1896
diff
changeset
|
143 |
return os.path.normpath(p) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
144 |
|
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
145 |
def stringify(thing): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
146 |
'''turn nested template iterator into string.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
147 |
if hasattr(thing, '__iter__'): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
148 |
return "".join([stringify(t) for t in thing if t is not None]) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
149 |
return str(thing) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
150 |