equal
deleted
inserted
replaced
61 if hasattr(thing, "__iter__"): |
61 if hasattr(thing, "__iter__"): |
62 for part in thing: |
62 for part in thing: |
63 write(part) |
63 write(part) |
64 else: |
64 else: |
65 sys.stdout.write(str(thing)) |
65 sys.stdout.write(str(thing)) |
66 |
|
67 def template(tmpl, filters = {}, **map): |
|
68 while tmpl: |
|
69 m = re.search(r"#([a-zA-Z0-9]+)((\|[a-zA-Z0-9]+)*)#", tmpl) |
|
70 if m: |
|
71 yield tmpl[:m.start(0)] |
|
72 v = map.get(m.group(1), "") |
|
73 v = callable(v) and v(**map) or v |
|
74 |
|
75 fl = m.group(2) |
|
76 if fl: |
|
77 for f in fl.split("|")[1:]: |
|
78 v = filters[f](v) |
|
79 |
|
80 yield v |
|
81 tmpl = tmpl[m.end(0):] |
|
82 else: |
|
83 yield tmpl |
|
84 return |
|
85 |
66 |
86 class templater: |
67 class templater: |
87 def __init__(self, mapfile, filters = {}, defaults = {}): |
68 def __init__(self, mapfile, filters = {}, defaults = {}): |
88 self.cache = {} |
69 self.cache = {} |
89 self.map = {} |
70 self.map = {} |
107 m.update(map) |
88 m.update(map) |
108 try: |
89 try: |
109 tmpl = self.cache[t] |
90 tmpl = self.cache[t] |
110 except KeyError: |
91 except KeyError: |
111 tmpl = self.cache[t] = file(self.map[t]).read() |
92 tmpl = self.cache[t] = file(self.map[t]).read() |
112 return template(tmpl, self.filters, **m) |
93 return self.template(tmpl, self.filters, **m) |
|
94 |
|
95 def template(self, tmpl, filters = {}, **map): |
|
96 while tmpl: |
|
97 m = re.search(r"#([a-zA-Z0-9]+)((%[a-zA-Z0-9]+)*)((\|[a-zA-Z0-9]+)*)#", tmpl) |
|
98 if m: |
|
99 yield tmpl[:m.start(0)] |
|
100 v = map.get(m.group(1), "") |
|
101 v = callable(v) and v(**map) or v |
|
102 |
|
103 format = m.group(2) |
|
104 fl = m.group(4) |
|
105 |
|
106 if format: |
|
107 q = v.__iter__ |
|
108 for i in q(): |
|
109 lm = map.copy() |
|
110 lm.update(i) |
|
111 yield self(format[1:], **lm) |
|
112 |
|
113 v = "" |
|
114 |
|
115 elif fl: |
|
116 for f in fl.split("|")[1:]: |
|
117 v = filters[f](v) |
|
118 |
|
119 yield v |
|
120 tmpl = tmpl[m.end(0):] |
|
121 else: |
|
122 yield tmpl |
|
123 return |
113 |
124 |
114 def rfc822date(x): |
125 def rfc822date(x): |
115 return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(x)) |
126 return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(x)) |
116 |
127 |
117 class hgweb: |
128 class hgweb: |
558 i.reverse() |
569 i.reverse() |
559 |
570 |
560 def entries(**map): |
571 def entries(**map): |
561 parity = 0 |
572 parity = 0 |
562 for k,n in i: |
573 for k,n in i: |
563 yield self.t("tagentry", |
574 yield {"parity": parity, |
564 parity = parity, |
575 "tag": k, |
565 tag = k, |
576 "node": hex(n)} |
566 node = hex(n)) |
|
567 parity = 1 - parity |
577 parity = 1 - parity |
568 |
578 |
569 yield self.t("tags", |
579 yield self.t("tags", |
570 manifest = hex(mf), |
580 manifest = hex(mf), |
571 entries = entries) |
581 entries = entries) |