Mercurial > hg > mercurial-crew-with-dirclash
annotate mercurial/templater.py @ 4779:37e11c768db9
merge with crew-stable
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Tue, 03 Jul 2007 03:14:18 -0300 |
parents | e21a0e12ff10 |
children | 46c5e1ee8aaa |
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 # |
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 |
3893 | 8 from i18n import _ |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
9 from node import * |
3886
abaee83ce0a6
Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
3654
diff
changeset
|
10 import cgi, re, sys, os, time, urllib, util, textwrap |
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: |
3642
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3641
diff
changeset
|
16 if len(s) < 2 or s[0] != s[-1]: |
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3641
diff
changeset
|
17 raise SyntaxError(_('unmatched quotes')) |
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3641
diff
changeset
|
18 return s[1:-1].decode('string_escape') |
3635
231393b7316f
templater: use str.decode in parse_string
Matt Mackall <mpm@selenic.com>
parents:
3457
diff
changeset
|
19 |
231393b7316f
templater: use str.decode in parse_string
Matt Mackall <mpm@selenic.com>
parents:
3457
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 |
4335
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 |
3641
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
42 template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))" |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
43 r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
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 |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
59 i = 0 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
60 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
|
61 l = l.strip() |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
62 i += 1 |
1914
a5bf0030df5f
make --style=compact look for map-cmdline.compact.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1913
diff
changeset
|
63 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
|
64 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
|
65 if m: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
66 key, val = m.groups() |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
67 if val[0] in "'\"": |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
68 try: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
69 self.cache[key] = parsestring(val) |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
70 except SyntaxError, inst: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
71 raise SyntaxError('%s:%s: %s' % |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
72 (mapfile, i, inst.args[0])) |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
73 else: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
74 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
|
75 else: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
76 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
|
77 |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
78 def __contains__(self, key): |
3640
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3639
diff
changeset
|
79 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
|
80 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
81 def __call__(self, t, **map): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
82 '''perform expansion. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
83 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
|
84 map is added elements to use during expansion.''' |
3640
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3639
diff
changeset
|
85 if not self.cache.has_key(t): |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
86 try: |
3640
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3639
diff
changeset
|
87 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
|
88 except IOError, inst: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
89 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
|
90 (self.map[t], inst.args[1])) |
3641
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
91 tmpl = self.cache[t] |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
92 |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
93 while tmpl: |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
94 m = self.template_re.search(tmpl) |
3641
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
95 if not m: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
96 yield tmpl |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
97 break |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 |
3641
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
99 start, end = m.span(0) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
100 key, format, fl = m.groups() |
3639
dfbbb33f3fa3
templater: simplify template function
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
101 |
3641
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
102 if start: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
103 yield tmpl[:start] |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
104 tmpl = tmpl[end:] |
3639
dfbbb33f3fa3
templater: simplify template function
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
105 |
3641
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
106 if key in map: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
107 v = map[key] |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
108 else: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
109 v = self.defaults.get(key, "") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
110 if callable(v): |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
111 v = v(**map) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
112 if format: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
113 if not hasattr(v, '__iter__'): |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
114 raise SyntaxError(_("Error expanding '%s%s'") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
115 % (key, format)) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
116 lm = map.copy() |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
117 for i in v: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
118 lm.update(i) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
119 yield self(format, **lm) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
120 else: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
121 if fl: |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
122 for f in fl.split("|")[1:]: |
3640
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3639
diff
changeset
|
123 v = self.filters[f](v) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
124 yield v |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
125 |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
126 agescales = [("second", 1), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
127 ("minute", 60), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
128 ("hour", 3600), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
129 ("day", 3600 * 24), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
130 ("week", 3600 * 24 * 7), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
131 ("month", 3600 * 24 * 30), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
132 ("year", 3600 * 24 * 365)] |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
133 |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
134 agescales.reverse() |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
135 |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
136 def age(date): |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
137 '''turn a (timestamp, tzoff) tuple into an age string.''' |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
138 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
139 def plural(t, c): |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
140 if c == 1: |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
141 return t |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
142 return t + "s" |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
143 def fmt(t, c): |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
144 return "%d %s" % (c, plural(t, c)) |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
145 |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
146 now = time.time() |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
147 then = date[0] |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
148 delta = max(1, int(now - then)) |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
149 |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
150 for t, s in agescales: |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
151 n = delta / s |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
152 if n >= 2 or s == 1: |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
153 return fmt(t, n) |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
154 |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
155 def stringify(thing): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
156 '''turn nested template iterator into string.''' |
3644
54d27caf6a78
templater: speed up changeset writes and stringify
Matt Mackall <mpm@selenic.com>
parents:
3643
diff
changeset
|
157 if hasattr(thing, '__iter__'): |
3654 | 158 return "".join([stringify(t) for t in thing if t is not None]) |
3644
54d27caf6a78
templater: speed up changeset writes and stringify
Matt Mackall <mpm@selenic.com>
parents:
3643
diff
changeset
|
159 return str(thing) |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
160 |
2470
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
161 para_re = None |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
162 space_re = None |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
163 |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
164 def fill(text, width): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
165 '''fill many paragraphs.''' |
2470
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
166 global para_re, space_re |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
167 if para_re is None: |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
168 para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
169 space_re = re.compile(r' +') |
2535
b8ccf6386db7
Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents:
2470
diff
changeset
|
170 |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
171 def findparas(): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
172 start = 0 |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
173 while True: |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
174 m = para_re.search(text, start) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
175 if not m: |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
176 w = len(text) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
177 while w > start and text[w-1].isspace(): w -= 1 |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
178 yield text[start:w], text[w:] |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
179 break |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
180 yield text[start:m.start(0)], m.group(1) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
181 start = m.end(1) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
182 |
3636
bf3dec184c78
templater: remove cStringIO for fill
Matt Mackall <mpm@selenic.com>
parents:
3635
diff
changeset
|
183 return "".join([space_re.sub(' ', textwrap.fill(para, width)) + rest |
bf3dec184c78
templater: remove cStringIO for fill
Matt Mackall <mpm@selenic.com>
parents:
3635
diff
changeset
|
184 for para, rest in findparas()]) |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
185 |
2559
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
186 def firstline(text): |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
187 '''return the first line of text''' |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
188 try: |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
189 return text.splitlines(1)[0].rstrip('\r\n') |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
190 except IndexError: |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
191 return '' |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
192 |
1906
9dec2479622d
add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1905
diff
changeset
|
193 def isodate(date): |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
194 '''turn a (timestamp, tzoff) tuple into an iso 8631 date and time.''' |
1906
9dec2479622d
add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1905
diff
changeset
|
195 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
|
196 |
2519
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
197 def hgdate(date): |
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
198 '''turn a (timestamp, tzoff) tuple into an hg cset timestamp.''' |
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
199 return "%d %d" % date |
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
200 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
201 def nl2br(text): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
202 '''replace raw newlines with xhtml line breaks.''' |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
203 return text.replace('\n', '<br/>\n') |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
204 |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
205 def obfuscate(text): |
3904
0dd07dd63db3
templater.py: fix obfuscate
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3654
diff
changeset
|
206 text = unicode(text, util._encoding, 'replace') |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
207 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
|
208 |
1904 | 209 def domain(author): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
210 '''get domain of author, or empty string if none.''' |
1904 | 211 f = author.find('@') |
212 if f == -1: return '' | |
213 author = author[f+1:] | |
214 f = author.find('>') | |
215 if f >= 0: author = author[:f] | |
216 return author | |
217 | |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
218 def email(author): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
219 '''get email of author.''' |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
220 r = author.find('>') |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
221 if r == -1: r = None |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
222 return author[author.find('<')+1:r] |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
223 |
1904 | 224 def person(author): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
225 '''get name of author, or else username.''' |
1904 | 226 f = author.find('<') |
227 if f == -1: return util.shortuser(author) | |
228 return author[:f].rstrip() | |
229 | |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
230 def shortdate(date): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
231 '''turn (timestamp, tzoff) tuple into iso 8631 date.''' |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
232 return util.datestr(date, format='%Y-%m-%d', timezone=False) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
233 |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
234 def indent(text, prefix): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
235 '''indent each non-empty line of text after first with prefix.''' |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
236 lines = text.splitlines() |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
237 num_lines = len(lines) |
3638
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
238 def indenter(): |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
239 for i in xrange(num_lines): |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
240 l = lines[i] |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
241 if i and l.strip(): |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
242 yield prefix |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
243 yield l |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
244 if i < num_lines - 1 or text.endswith('\n'): |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
245 yield '\n' |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
246 return "".join(indenter()) |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
247 |
4778
e21a0e12ff10
hgweb: use lrwxrwxrwx as the permissions of a symlink
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4336
diff
changeset
|
248 def permissions(flags): |
e21a0e12ff10
hgweb: use lrwxrwxrwx as the permissions of a symlink
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4336
diff
changeset
|
249 if "l" in flags: |
e21a0e12ff10
hgweb: use lrwxrwxrwx as the permissions of a symlink
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4336
diff
changeset
|
250 return "lrwxrwxrwx" |
e21a0e12ff10
hgweb: use lrwxrwxrwx as the permissions of a symlink
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4336
diff
changeset
|
251 if "x" in flags: |
e21a0e12ff10
hgweb: use lrwxrwxrwx as the permissions of a symlink
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4336
diff
changeset
|
252 return "-rwxr-xr-x" |
e21a0e12ff10
hgweb: use lrwxrwxrwx as the permissions of a symlink
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4336
diff
changeset
|
253 return "-rw-r--r--" |
e21a0e12ff10
hgweb: use lrwxrwxrwx as the permissions of a symlink
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4336
diff
changeset
|
254 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
255 common_filters = { |
1904 | 256 "addbreaks": nl2br, |
2191
c2e43535d4d1
make templater bit more flexible and efficient for external users.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2189
diff
changeset
|
257 "basename": os.path.basename, |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
258 "age": age, |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
259 "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
|
260 "domain": domain, |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
261 "email": email, |
1904 | 262 "escape": lambda x: cgi.escape(x, True), |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
263 "fill68": lambda x: fill(x, width=68), |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
264 "fill76": lambda x: fill(x, width=76), |
2559
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
265 "firstline": firstline, |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
266 "tabindent": lambda x: indent(x, '\t'), |
2519
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
267 "hgdate": hgdate, |
1906
9dec2479622d
add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1905
diff
changeset
|
268 "isodate": isodate, |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
269 "obfuscate": obfuscate, |
4778
e21a0e12ff10
hgweb: use lrwxrwxrwx as the permissions of a symlink
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4336
diff
changeset
|
270 "permissions": permissions, |
1904 | 271 "person": person, |
272 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"), | |
1912
b288b4bb8448
hide some functions behind lambdas, so demandload is useful.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1909
diff
changeset
|
273 "short": lambda x: x[:12], |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
274 "shortdate": shortdate, |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
275 "stringify": stringify, |
1904 | 276 "strip": lambda x: x.strip(), |
1912
b288b4bb8448
hide some functions behind lambdas, so demandload is useful.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1909
diff
changeset
|
277 "urlescape": lambda x: urllib.quote(x), |
b288b4bb8448
hide some functions behind lambdas, so demandload is useful.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1909
diff
changeset
|
278 "user": lambda x: util.shortuser(x), |
3457
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3456
diff
changeset
|
279 "stringescape": lambda x: x.encode('string_escape'), |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
280 } |
1897
58b6784cf9f1
move hgweb.templatepath into templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1896
diff
changeset
|
281 |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
282 def templatepath(name=None): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
283 '''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
|
284 returns None if not found.''' |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
285 |
2001
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
286 # 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
|
287 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
|
288 module = sys.executable |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
289 else: |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
290 module = __file__ |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
291 for f in 'templates', '../templates': |
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
292 fl = f.split('/') |
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
293 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
|
294 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
|
295 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
|
296 return os.path.normpath(p) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
297 |