comparison mercurial/hgweb/hgwebdir_mod.py @ 5602:d676d0f35bd8

hgwebdir: split out templater creation
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Mon, 03 Dec 2007 18:58:18 +0100
parents 8279cb841467
children 74f65f44a9aa
comparison
equal deleted inserted replaced
5601:8279cb841467 5602:d676d0f35bd8
68 req = wsgirequest(env, respond) 68 req = wsgirequest(env, respond)
69 self.run_wsgi(req) 69 self.run_wsgi(req)
70 return req 70 return req
71 71
72 def run_wsgi(self, req): 72 def run_wsgi(self, req):
73 def header(**map):
74 header_file = cStringIO.StringIO(
75 ''.join(tmpl("header", encoding=util._encoding, **map)))
76 msg = mimetools.Message(header_file, 0)
77 req.header(msg.items())
78 yield header_file.read()
79
80 def footer(**map):
81 yield tmpl("footer", **map)
82
83 def motd(**map):
84 if self.motd is not None:
85 yield self.motd
86 else:
87 yield config('web', 'motd', '')
88
89 def config(section, name, default=None, untrusted=True):
90 return self.parentui.config(section, name, default, untrusted)
91
92 url = req.env.get('SCRIPT_NAME', '')
93 if not url.endswith('/'):
94 url += '/'
95
96 staticurl = config('web', 'staticurl') or url + 'static/'
97 if not staticurl.endswith('/'):
98 staticurl += '/'
99
100 style = self.style
101 if style is None:
102 style = config('web', 'style', '')
103 if req.form.has_key('style'):
104 style = req.form['style'][0]
105 if self.stripecount is None:
106 self.stripecount = int(config('web', 'stripes', 1))
107 mapfile = style_map(templater.templatepath(), style)
108 tmpl = templater.templater(mapfile, templater.common_filters,
109 defaults={"header": header,
110 "footer": footer,
111 "motd": motd,
112 "url": url,
113 "staticurl": staticurl})
114 73
115 try: 74 try:
116 try: 75 try:
117 virtual = req.env.get("PATH_INFO", "").strip('/') 76 virtual = req.env.get("PATH_INFO", "").strip('/')
118 if virtual.startswith('static/'): 77 if virtual.startswith('static/'):
135 raise ErrorResponse(500, str(inst)) 94 raise ErrorResponse(500, str(inst))
136 95
137 # browse subdirectories 96 # browse subdirectories
138 subdir = virtual + '/' 97 subdir = virtual + '/'
139 if [r for r in repos if r.startswith(subdir)]: 98 if [r for r in repos if r.startswith(subdir)]:
99 tmpl = self.templater(req)
140 self.makeindex(req, tmpl, subdir) 100 self.makeindex(req, tmpl, subdir)
141 return 101 return
142 102
143 up = virtual.rfind('/') 103 up = virtual.rfind('/')
144 if up < 0: 104 if up < 0:
145 break 105 break
146 virtual = virtual[:up] 106 virtual = virtual[:up]
147 107
108 tmpl = self.templater(req)
148 req.respond(404, tmpl("notfound", repo=virtual)) 109 req.respond(404, tmpl("notfound", repo=virtual))
149 else: 110 else:
150 if req.form.has_key('static'): 111 if req.form.has_key('static'):
151 static = os.path.join(templater.templatepath(), "static") 112 static = os.path.join(templater.templatepath(), "static")
152 fname = req.form['static'][0] 113 fname = req.form['static'][0]
153 req.write(staticfile(static, fname, req)) 114 req.write(staticfile(static, fname, req))
154 else: 115 else:
116 tmpl = self.templater(req)
155 self.makeindex(req, tmpl) 117 self.makeindex(req, tmpl)
156 except ErrorResponse, err: 118 except ErrorResponse, err:
157 req.respond(err.code, tmpl('error', error=err.message or '')) 119 req.respond(err.code, tmpl('error', error=err.message or ''))
158 finally: 120 finally:
159 tmpl = None 121 tmpl = None
257 and "-" or "", column)) 219 and "-" or "", column))
258 for column in sortable] 220 for column in sortable]
259 req.write(tmpl("index", entries=entries, subdir=subdir, 221 req.write(tmpl("index", entries=entries, subdir=subdir,
260 sortcolumn=sortcolumn, descending=descending, 222 sortcolumn=sortcolumn, descending=descending,
261 **dict(sort))) 223 **dict(sort)))
224
225 def templater(self, req):
226
227 def header(**map):
228 header_file = cStringIO.StringIO(
229 ''.join(tmpl("header", encoding=util._encoding, **map)))
230 msg = mimetools.Message(header_file, 0)
231 req.header(msg.items())
232 yield header_file.read()
233
234 def footer(**map):
235 yield tmpl("footer", **map)
236
237 def motd(**map):
238 if self.motd is not None:
239 yield self.motd
240 else:
241 yield config('web', 'motd', '')
242
243 def config(section, name, default=None, untrusted=True):
244 return self.parentui.config(section, name, default, untrusted)
245
246 url = req.env.get('SCRIPT_NAME', '')
247 if not url.endswith('/'):
248 url += '/'
249
250 staticurl = config('web', 'staticurl') or url + 'static/'
251 if not staticurl.endswith('/'):
252 staticurl += '/'
253
254 style = self.style
255 if style is None:
256 style = config('web', 'style', '')
257 if req.form.has_key('style'):
258 style = req.form['style'][0]
259 if self.stripecount is None:
260 self.stripecount = int(config('web', 'stripes', 1))
261 mapfile = style_map(templater.templatepath(), style)
262 tmpl = templater.templater(mapfile, templater.common_filters,
263 defaults={"header": header,
264 "footer": footer,
265 "motd": motd,
266 "url": url,
267 "staticurl": staticurl})
268 return tmpl