Mercurial > hg
comparison mercurial/templater.py @ 1899:888d298ddb91
many small changes to templater.
get string code to parse escapes. uses eval now, should parse strings
itself soon.
let caller check if fragment is defined using "in".
make templatepath take optional file name.
author | Vadim Gelfer <vadim.gelfer@gmail.com> |
---|---|
date | Sun, 26 Feb 2006 20:53:37 -0800 |
parents | 58b6784cf9f1 |
children | f2815605186e |
comparison
equal
deleted
inserted
replaced
1898:e517189f168d | 1899:888d298ddb91 |
---|---|
8 self.base = os.path.dirname(mapfile) | 8 self.base = os.path.dirname(mapfile) |
9 self.filters = filters | 9 self.filters = filters |
10 self.defaults = defaults | 10 self.defaults = defaults |
11 | 11 |
12 for l in file(mapfile): | 12 for l in file(mapfile): |
13 m = re.match(r'(\S+)\s*=\s*"(.*)"$', l) | 13 m = re.match(r'(\S+)\s*=\s*(".*"|\'.*\')$', l) |
14 if m: | 14 if m: |
15 self.cache[m.group(1)] = m.group(2) | 15 self.cache[m.group(1)] = eval(m.group(2)) |
16 else: | 16 else: |
17 m = re.match(r'(\S+)\s*=\s*(\S+)', l) | 17 m = re.match(r'(\S+)\s*=\s*(\S+)', l) |
18 if m: | 18 if m: |
19 self.map[m.group(1)] = os.path.join(self.base, m.group(2)) | 19 self.map[m.group(1)] = os.path.join(self.base, m.group(2)) |
20 else: | 20 else: |
21 raise LookupError(_("unknown map entry '%s'") % l) | 21 raise LookupError(_("unknown map entry '%s'") % l) |
22 | |
23 def __contains__(self, key): | |
24 return key in self.cache | |
22 | 25 |
23 def __call__(self, t, **map): | 26 def __call__(self, t, **map): |
24 m = self.defaults.copy() | 27 m = self.defaults.copy() |
25 m.update(map) | 28 m.update(map) |
26 try: | 29 try: |
29 tmpl = self.cache[t] = file(self.map[t]).read() | 32 tmpl = self.cache[t] = file(self.map[t]).read() |
30 return self.template(tmpl, self.filters, **m) | 33 return self.template(tmpl, self.filters, **m) |
31 | 34 |
32 def template(self, tmpl, filters={}, **map): | 35 def template(self, tmpl, filters={}, **map): |
33 while tmpl: | 36 while tmpl: |
34 m = re.search(r"#([a-zA-Z0-9]+)((%[a-zA-Z0-9]+)*)((\|[a-zA-Z0-9]+)*)#", tmpl) | 37 m = re.search(r"#([a-zA-Z_][a-zA-Z0-9_]*)" |
38 r"((%[a-zA-Z_][a-zA-Z0-9_]*)*)" | |
39 r"((\|[a-zA-Z_][a-zA-Z0-9_]*)*)#", tmpl) | |
35 if m: | 40 if m: |
36 yield tmpl[:m.start(0)] | 41 yield tmpl[:m.start(0)] |
37 v = map.get(m.group(1), "") | 42 v = map.get(m.group(1), "") |
38 v = callable(v) and v(**map) or v | 43 v = callable(v) and v(**map) or v |
39 | 44 |
104 "firstline": (lambda x: x.splitlines(1)[0]), | 109 "firstline": (lambda x: x.splitlines(1)[0]), |
105 "permissions": (lambda x: x and "-rwxr-xr-x" or "-rw-r--r--"), | 110 "permissions": (lambda x: x and "-rwxr-xr-x" or "-rw-r--r--"), |
106 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"), | 111 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"), |
107 } | 112 } |
108 | 113 |
109 def templatepath(): | 114 def templatepath(name=None): |
110 for f in "templates", "../templates": | 115 for f in 'templates', '../templates': |
111 p = os.path.join(os.path.dirname(__file__), f) | 116 fl = f.split('/') |
112 if os.path.isdir(p): | 117 if name: fl.append(name) |
118 p = os.path.join(os.path.dirname(__file__), *fl) | |
119 if (name and os.path.exists(p)) or os.path.isdir(p): | |
113 return os.path.normpath(p) | 120 return os.path.normpath(p) |