comparison mercurial/templater.py @ 1904:a7e416bf3c1d

improve templating. allow {foo} as well as #foo#. add new functions for changeset authors.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Mon, 27 Feb 2006 11:33:09 -0800
parents 1cc5f25653a3
children 0c760737b996
comparison
equal deleted inserted replaced
1903:e4abeafd6eb1 1904:a7e416bf3c1d
65 tmpl = self.cache[t] 65 tmpl = self.cache[t]
66 except KeyError: 66 except KeyError:
67 tmpl = self.cache[t] = file(self.map[t]).read() 67 tmpl = self.cache[t] = file(self.map[t]).read()
68 return self.template(tmpl, self.filters, **m) 68 return self.template(tmpl, self.filters, **m)
69 69
70 template_re = re.compile(r"#([a-zA-Z_][a-zA-Z0-9_]*)" 70 template_re = re.compile(r"[#{]([a-zA-Z_][a-zA-Z0-9_]*)"
71 r"((%[a-zA-Z_][a-zA-Z0-9_]*)*)" 71 r"((%[a-zA-Z_][a-zA-Z0-9_]*)*)"
72 r"((\|[a-zA-Z_][a-zA-Z0-9_]*)*)#") 72 r"((\|[a-zA-Z_][a-zA-Z0-9_]*)*)[#}]")
73 73
74 def template(self, tmpl, filters={}, **map): 74 def template(self, tmpl, filters={}, **map):
75 lm = map.copy() 75 lm = map.copy()
76 while tmpl: 76 while tmpl:
77 m = self.template_re.search(tmpl) 77 m = self.template_re.search(tmpl)
134 return text.replace('\n', '<br/>\n') 134 return text.replace('\n', '<br/>\n')
135 135
136 def obfuscate(text): 136 def obfuscate(text):
137 return ''.join(['&#%d;' % ord(c) for c in text]) 137 return ''.join(['&#%d;' % ord(c) for c in text])
138 138
139 def domain(author):
140 f = author.find('@')
141 if f == -1: return ''
142 author = author[f+1:]
143 f = author.find('>')
144 if f >= 0: author = author[:f]
145 return author
146
147 def person(author):
148 f = author.find('<')
149 if f == -1: return util.shortuser(author)
150 return author[:f].rstrip()
151
139 common_filters = { 152 common_filters = {
140 "escape": lambda x: cgi.escape(x, True), 153 "addbreaks": nl2br,
141 "urlescape": urllib.quote,
142 "strip": lambda x: x.strip(),
143 "age": age, 154 "age": age,
144 "date": lambda x: util.datestr(x), 155 "date": lambda x: util.datestr(x),
145 "addbreaks": nl2br, 156 "escape": lambda x: cgi.escape(x, True),
157 "firstline": (lambda x: x.splitlines(1)[0]),
158 "domain": domain,
146 "obfuscate": obfuscate, 159 "obfuscate": obfuscate,
160 "permissions": (lambda x: x and "-rwxr-xr-x" or "-rw-r--r--"),
161 "person": person,
162 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"),
147 "short": (lambda x: x[:12]), 163 "short": (lambda x: x[:12]),
148 "firstline": (lambda x: x.splitlines(1)[0]), 164 "strip": lambda x: x.strip(),
149 "permissions": (lambda x: x and "-rwxr-xr-x" or "-rw-r--r--"), 165 "urlescape": urllib.quote,
150 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"), 166 "user": util.shortuser,
151 } 167 }
152 168
153 def templatepath(name=None): 169 def templatepath(name=None):
154 for f in 'templates', '../templates': 170 for f in 'templates', '../templates':
155 fl = f.split('/') 171 fl = f.split('/')