annotate mercurial/templater.py @ 1906:9dec2479622d

add iso date template filter.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Mon, 27 Feb 2006 12:50:49 -0800
parents 0c760737b996
children 37b9f80a5fbb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1900
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
1 import re
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
2 from demandload import demandload
1901
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
3 from i18n import gettext as _
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
4 demandload(globals(), "cStringIO cgi os time urllib util")
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
5
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
6 esctable = {
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
7 '\\': '\\',
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
8 'r': '\r',
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
9 't': '\t',
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
10 'n': '\n',
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
11 'v': '\v',
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
12 }
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
13
1902
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
14 def parsestring(s, quoted=True):
1901
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
15 fp = cStringIO.StringIO()
1902
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
16 if quoted:
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
17 first = s[0]
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
18 if len(s) < 2: raise SyntaxError(_('string too short'))
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
19 if first not in "'\"": raise SyntaxError(_('invalid quote'))
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
20 if s[-1] != first: raise SyntaxError(_('unmatched quotes'))
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
21 s = s[1:-1]
1901
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
22 escape = False
1902
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
23 for c in s:
1901
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
24 if escape:
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
25 fp.write(esctable.get(c, c))
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
26 escape = False
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
27 elif c == '\\': escape = True
1902
1cc5f25653a3 make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1901
diff changeset
28 elif quoted and c == first: raise SyntaxError(_('string ends early'))
1901
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
29 else: fp.write(c)
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
30 if escape: raise SyntaxError(_('unterminated escape'))
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
31 return fp.getvalue()
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
32
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
33 class templater(object):
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
34 def __init__(self, mapfile, filters={}, defaults={}):
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
35 self.mapfile = mapfile or 'template'
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
36 self.cache = {}
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
37 self.map = {}
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
38 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
39 self.filters = filters
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
40 self.defaults = defaults
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
41
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
42 if not mapfile:
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
43 return
1901
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
44 i = 0
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
45 for l in file(mapfile):
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
46 l = l.rstrip('\r\n')
1901
c64bef3d7043 use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1900
diff changeset
47 i += 1
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
48 if l.startswith('#') or not l.strip(): continue
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
49 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
50 if m:
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
51 key, val = m.groups()
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
52 if val[0] in "'\"":
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
53 try:
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
54 self.cache[key] = parsestring(val)
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
55 except SyntaxError, inst:
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
56 raise SyntaxError('%s:%s: %s' %
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
57 (mapfile, i, inst.args[0]))
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
58 else:
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
59 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
60 else:
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
61 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
62
1899
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
63 def __contains__(self, key):
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
64 return key in self.cache
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
65
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
66 def __call__(self, t, **map):
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
67 m = self.defaults.copy()
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
68 m.update(map)
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
69 try:
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
70 tmpl = self.cache[t]
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
71 except KeyError:
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
72 try:
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
73 tmpl = self.cache[t] = file(self.map[t]).read()
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
74 except IOError, inst:
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
75 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
76 (self.map[t], inst.args[1]))
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
77 return self.template(tmpl, self.filters, **m)
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
78
1904
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
79 template_re = re.compile(r"[#{]([a-zA-Z_][a-zA-Z0-9_]*)"
1900
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
80 r"((%[a-zA-Z_][a-zA-Z0-9_]*)*)"
1904
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
81 r"((\|[a-zA-Z_][a-zA-Z0-9_]*)*)[#}]")
1900
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
82
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
83 def template(self, tmpl, filters={}, **map):
1900
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
84 lm = map.copy()
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
85 while tmpl:
1900
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
86 m = self.template_re.search(tmpl)
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
87 if m:
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
88 start, end = m.span(0)
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
89 s, e = tmpl[start], tmpl[end - 1]
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
90 key = m.group(1)
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
91 if ((s == '#' and e != '#') or (s == '{' and e != '}')):
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
92 raise SyntaxError(_("'%s'/'%s' mismatch expanding '%s'") %
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
93 (s, e, key))
1900
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
94 if start:
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
95 yield tmpl[:start]
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
96 v = map.get(key, "")
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
97 v = callable(v) and v(**map) or v
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
98
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
99 format = m.group(2)
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
100 fl = m.group(4)
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
101
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
102 if format:
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
103 q = v.__iter__
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
104 for i in q():
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
105 lm.update(i)
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
106 yield self(format[1:], **lm)
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
107
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
108 v = ""
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
109
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
110 elif fl:
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
111 for f in fl.split("|")[1:]:
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
112 v = filters[f](v)
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
113
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
114 yield v
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
115 tmpl = tmpl[end:]
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
116 else:
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
117 yield tmpl
1900
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
118 break
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
119
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
120 agescales = [("second", 1),
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
121 ("minute", 60),
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
122 ("hour", 3600),
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
123 ("day", 3600 * 24),
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
124 ("week", 3600 * 24 * 7),
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
125 ("month", 3600 * 24 * 30),
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
126 ("year", 3600 * 24 * 365)]
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
127
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
128 agescales.reverse()
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
129
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
130 def age(x):
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
131 def plural(t, c):
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
132 if c == 1:
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
133 return t
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
134 return t + "s"
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
135 def fmt(t, c):
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
136 return "%d %s" % (c, plural(t, c))
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
137
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
138 now = time.time()
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
139 then = x[0]
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
140 delta = max(1, int(now - then))
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
141
1900
f2815605186e move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1899
diff changeset
142 for t, s in agescales:
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
143 n = delta / s
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
144 if n >= 2 or s == 1:
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
145 return fmt(t, n)
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
146
1906
9dec2479622d add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1905
diff changeset
147 def isodate(date):
9dec2479622d add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1905
diff changeset
148 return util.datestr(date, format='%Y-%m-%d %H:%M')
9dec2479622d add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1905
diff changeset
149
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
150 def nl2br(text):
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
151 return text.replace('\n', '<br/>\n')
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
152
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
153 def obfuscate(text):
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
154 return ''.join(['&#%d;' % ord(c) for c in text])
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
155
1904
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
156 def domain(author):
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
157 f = author.find('@')
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
158 if f == -1: return ''
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
159 author = author[f+1:]
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
160 f = author.find('>')
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
161 if f >= 0: author = author[:f]
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
162 return author
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
163
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
164 def person(author):
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
165 f = author.find('<')
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
166 if f == -1: return util.shortuser(author)
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
167 return author[:f].rstrip()
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
168
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
169 common_filters = {
1904
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
170 "addbreaks": nl2br,
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
171 "age": age,
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
172 "date": lambda x: util.datestr(x),
1905
0c760737b996 improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1904
diff changeset
173 "domain": domain,
1904
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
174 "escape": lambda x: cgi.escape(x, True),
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
175 "firstline": (lambda x: x.splitlines(1)[0]),
1906
9dec2479622d add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1905
diff changeset
176 "isodate": isodate,
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
177 "obfuscate": obfuscate,
1904
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
178 "permissions": (lambda x: x and "-rwxr-xr-x" or "-rw-r--r--"),
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
179 "person": person,
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
180 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"),
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
181 "short": (lambda x: x[:12]),
1904
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
182 "strip": lambda x: x.strip(),
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
183 "urlescape": urllib.quote,
a7e416bf3c1d improve templating.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1902
diff changeset
184 "user": util.shortuser,
1896
f8f818a04f5b move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
185 }
1897
58b6784cf9f1 move hgweb.templatepath into templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1896
diff changeset
186
1899
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
187 def templatepath(name=None):
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
188 for f in 'templates', '../templates':
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
189 fl = f.split('/')
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
190 if name: fl.append(name)
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
191 p = os.path.join(os.path.dirname(__file__), *fl)
888d298ddb91 many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1897
diff changeset
192 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
193 return os.path.normpath(p)