Show More
@@ -1,5 +1,6 b'' | |||||
|
1 | import re | |||
1 | from demandload import demandload |
|
2 | from demandload import demandload | |
2 |
demandload(globals(), "cgi os |
|
3 | demandload(globals(), "cgi os time urllib util") | |
3 |
|
4 | |||
4 | class templater(object): |
|
5 | class templater(object): | |
5 | def __init__(self, mapfile, filters={}, defaults={}): |
|
6 | def __init__(self, mapfile, filters={}, defaults={}): | |
@@ -32,13 +33,18 b' class templater(object):' | |||||
32 | tmpl = self.cache[t] = file(self.map[t]).read() |
|
33 | tmpl = self.cache[t] = file(self.map[t]).read() | |
33 | return self.template(tmpl, self.filters, **m) |
|
34 | return self.template(tmpl, self.filters, **m) | |
34 |
|
35 | |||
|
36 | template_re = re.compile(r"#([a-zA-Z_][a-zA-Z0-9_]*)" | |||
|
37 | r"((%[a-zA-Z_][a-zA-Z0-9_]*)*)" | |||
|
38 | r"((\|[a-zA-Z_][a-zA-Z0-9_]*)*)#") | |||
|
39 | ||||
35 | def template(self, tmpl, filters={}, **map): |
|
40 | def template(self, tmpl, filters={}, **map): | |
|
41 | lm = map.copy() | |||
36 | while tmpl: |
|
42 | while tmpl: | |
37 | m = re.search(r"#([a-zA-Z_][a-zA-Z0-9_]*)" |
|
43 | m = self.template_re.search(tmpl) | |
38 | r"((%[a-zA-Z_][a-zA-Z0-9_]*)*)" |
|
|||
39 | r"((\|[a-zA-Z_][a-zA-Z0-9_]*)*)#", tmpl) |
|
|||
40 | if m: |
|
44 | if m: | |
41 |
|
|
45 | start = m.start(0) | |
|
46 | if start: | |||
|
47 | yield tmpl[:start] | |||
42 | v = map.get(m.group(1), "") |
|
48 | v = map.get(m.group(1), "") | |
43 | v = callable(v) and v(**map) or v |
|
49 | v = callable(v) and v(**map) or v | |
44 |
|
50 | |||
@@ -48,7 +54,6 b' class templater(object):' | |||||
48 | if format: |
|
54 | if format: | |
49 | q = v.__iter__ |
|
55 | q = v.__iter__ | |
50 | for i in q(): |
|
56 | for i in q(): | |
51 | lm = map.copy() |
|
|||
52 | lm.update(i) |
|
57 | lm.update(i) | |
53 | yield self(format[1:], **lm) |
|
58 | yield self(format[1:], **lm) | |
54 |
|
59 | |||
@@ -62,7 +67,17 b' class templater(object):' | |||||
62 | tmpl = tmpl[m.end(0):] |
|
67 | tmpl = tmpl[m.end(0):] | |
63 | else: |
|
68 | else: | |
64 | yield tmpl |
|
69 | yield tmpl | |
65 |
|
|
70 | break | |
|
71 | ||||
|
72 | agescales = [("second", 1), | |||
|
73 | ("minute", 60), | |||
|
74 | ("hour", 3600), | |||
|
75 | ("day", 3600 * 24), | |||
|
76 | ("week", 3600 * 24 * 7), | |||
|
77 | ("month", 3600 * 24 * 30), | |||
|
78 | ("year", 3600 * 24 * 365)] | |||
|
79 | ||||
|
80 | agescales.reverse() | |||
66 |
|
81 | |||
67 | def age(x): |
|
82 | def age(x): | |
68 | def plural(t, c): |
|
83 | def plural(t, c): | |
@@ -76,17 +91,7 b' def age(x):' | |||||
76 | then = x[0] |
|
91 | then = x[0] | |
77 | delta = max(1, int(now - then)) |
|
92 | delta = max(1, int(now - then)) | |
78 |
|
93 | |||
79 | scales = [["second", 1], |
|
94 | for t, s in agescales: | |
80 | ["minute", 60], |
|
|||
81 | ["hour", 3600], |
|
|||
82 | ["day", 3600 * 24], |
|
|||
83 | ["week", 3600 * 24 * 7], |
|
|||
84 | ["month", 3600 * 24 * 30], |
|
|||
85 | ["year", 3600 * 24 * 365]] |
|
|||
86 |
|
||||
87 | scales.reverse() |
|
|||
88 |
|
||||
89 | for t, s in scales: |
|
|||
90 | n = delta / s |
|
95 | n = delta / s | |
91 | if n >= 2 or s == 1: |
|
96 | if n >= 2 or s == 1: | |
92 | return fmt(t, n) |
|
97 | return fmt(t, n) |
General Comments 0
You need to be logged in to leave comments.
Login now