author | Patrick Mezard <pmezard@gmail.com> |
Fri, 04 Apr 2008 23:09:54 +0200 | |
changeset 6473 | 9c897ffd3637 |
parent 5976 | 9f1e6ab76069 |
child 6337 | d2713d902524 |
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 |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
10 |
|
1902
1cc5f25653a3
make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1901
diff
changeset
|
11 |
def parsestring(s, quoted=True): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
12 |
'''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
|
13 |
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
|
14 |
if quoted: |
3639
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
15 |
if len(s) < 2 or s[0] != s[-1]: |
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
16 |
raise SyntaxError(_('unmatched quotes')) |
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
17 |
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
|
18 |
|
231393b7316f
templater: use str.decode in parse_string
Matt Mackall <mpm@selenic.com>
parents:
3462
diff
changeset
|
19 |
return s.decode('string_escape') |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
20 |
|
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
21 |
class templater(object): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
22 |
'''template expansion engine. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
23 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
24 |
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
|
25 |
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
|
26 |
is treated as name of template file. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
27 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
28 |
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
|
29 |
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
|
30 |
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
|
31 |
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
|
32 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
33 |
expansion also allows formatting and filtering. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
34 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
35 |
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
|
36 |
{key%format}. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
37 |
|
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
38 |
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
|
39 |
{key|filter1|filter2|...}.''' |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
40 |
|
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
41 |
template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))" |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
42 |
r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
43 |
|
1964
778281d46bb2
fix template bug that made hgweb break.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1955
diff
changeset
|
44 |
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
|
45 |
'''set up template engine. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
self.mapfile = mapfile or 'template' |
1975
6e1a8ea5d717
Duplicate cache when creating templater.
Shun-ichi Goto <shunichi.goto@gmail.com>
parents:
1964
diff
changeset
|
50 |
self.cache = cache.copy() |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
51 |
self.map = {} |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
52 |
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
|
53 |
self.filters = filters |
1964
778281d46bb2
fix template bug that made hgweb break.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1955
diff
changeset
|
54 |
self.defaults = defaults |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
55 |
|
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
56 |
if not mapfile: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
57 |
return |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
58 |
i = 0 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
59 |
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
|
60 |
l = l.strip() |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
61 |
i += 1 |
1914
a5bf0030df5f
make --style=compact look for map-cmdline.compact.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1913
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
if m: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
65 |
key, val = m.groups() |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
66 |
if val[0] in "'\"": |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
67 |
try: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
68 |
self.cache[key] = parsestring(val) |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
69 |
except SyntaxError, inst: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
70 |
raise SyntaxError('%s:%s: %s' % |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
71 |
(mapfile, i, inst.args[0])) |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
72 |
else: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
73 |
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
|
74 |
else: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
75 |
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
|
76 |
|
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
77 |
def __contains__(self, key): |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
78 |
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
|
79 |
|
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
80 |
def __call__(self, t, **map): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
81 |
'''perform expansion. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
try: |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
86 |
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
|
87 |
except IOError, inst: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
88 |
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
|
89 |
(self.map[t], inst.args[1])) |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
90 |
tmpl = self.cache[t] |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
91 |
|
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
92 |
while tmpl: |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
93 |
m = self.template_re.search(tmpl) |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
94 |
if not m: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
95 |
yield tmpl |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
96 |
break |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
97 |
|
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
98 |
start, end = m.span(0) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
99 |
key, format, fl = m.groups() |
3636
dfbbb33f3fa3
templater: simplify template function
Matt Mackall <mpm@selenic.com>
parents:
3635
diff
changeset
|
100 |
|
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
101 |
if start: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
102 |
yield tmpl[:start] |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
103 |
tmpl = tmpl[end:] |
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 key in map: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
106 |
v = map[key] |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
107 |
else: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
108 |
v = self.defaults.get(key, "") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
109 |
if callable(v): |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
110 |
v = v(**map) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
111 |
if format: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
112 |
if not hasattr(v, '__iter__'): |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
113 |
raise SyntaxError(_("Error expanding '%s%s'") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
114 |
% (key, format)) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
115 |
lm = map.copy() |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
116 |
for i in v: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
117 |
lm.update(i) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
118 |
yield self(format, **lm) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
119 |
else: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
120 |
if fl: |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
121 |
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
|
122 |
v = self.filters[f](v) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
123 |
yield v |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
124 |
|
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
125 |
def templatepath(name=None): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
126 |
'''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
|
127 |
returns None if not found.''' |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
128 |
|
2001
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
129 |
# 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
|
130 |
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
|
131 |
module = sys.executable |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
132 |
else: |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
133 |
module = __file__ |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
134 |
for f in 'templates', '../templates': |
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
135 |
fl = f.split('/') |
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
136 |
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
|
137 |
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
|
138 |
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
|
139 |
return os.path.normpath(p) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
140 |
|
5976
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
141 |
def stringify(thing): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
142 |
'''turn nested template iterator into string.''' |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
143 |
if hasattr(thing, '__iter__'): |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
144 |
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
|
145 |
return str(thing) |
9f1e6ab76069
templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
5975
diff
changeset
|
146 |