Mercurial > public > mercurial-scm > hg
comparison mercurial/hgweb/webutil.py @ 24177:f53b7174facf
hgweb: extract changeset template mapping generation to own function
Similar in spirit to 513d47905114, I want to write an extension to
make available extra template keywords so hgweb templates can include
extra data.
To do this today requires monkeypatching the templater, which I think is
the wrong place to perform this modification.
This patch extracts the creation of the templater arguments to a
standalone function - one that can be monkeypatched by extensions.
I would very much like for extensions to be able to inject extra
templater parameters into *any* template. However, I'm not sure the best
way to facilitate this, as hgweb commands invoke the templater before
returning and we want the extensions to have access to rich data
structures like the context instances. We need cooperation inside hgweb
command functions. The use case screams for something like internal-only
"hooks." This is exactly what my (rejected) "events" patch series
provided. Perhaps that feature should be reconsidered...
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 02 Mar 2015 15:07:18 -0800 |
parents | 46d6cdfce4bf |
children | 6ddc86eedc3b |
comparison
equal
deleted
inserted
replaced
24176:67952dc7a88f | 24177:f53b7174facf |
---|---|
8 | 8 |
9 import os, copy | 9 import os, copy |
10 from mercurial import match, patch, error, ui, util, pathutil, context | 10 from mercurial import match, patch, error, ui, util, pathutil, context |
11 from mercurial.i18n import _ | 11 from mercurial.i18n import _ |
12 from mercurial.node import hex, nullid | 12 from mercurial.node import hex, nullid |
13 from common import ErrorResponse | 13 from common import ErrorResponse, paritygen |
14 from common import HTTP_NOT_FOUND | 14 from common import HTTP_NOT_FOUND |
15 import difflib | 15 import difflib |
16 | 16 |
17 def up(p): | 17 def up(p): |
18 if p[0] != "/": | 18 if p[0] != "/": |
276 "tags": nodetagsdict(repo, n), | 276 "tags": nodetagsdict(repo, n), |
277 "bookmarks": nodebookmarksdict(repo, n), | 277 "bookmarks": nodebookmarksdict(repo, n), |
278 "inbranch": nodeinbranch(repo, ctx), | 278 "inbranch": nodeinbranch(repo, ctx), |
279 "branches": nodebranchdict(repo, ctx) | 279 "branches": nodebranchdict(repo, ctx) |
280 } | 280 } |
281 | |
282 def changesetentry(web, req, tmpl, ctx): | |
283 '''Obtain a dictionary to be used to render the "changeset" template.''' | |
284 | |
285 showtags = showtag(web.repo, tmpl, 'changesettag', ctx.node()) | |
286 showbookmarks = showbookmark(web.repo, tmpl, 'changesetbookmark', | |
287 ctx.node()) | |
288 showbranch = nodebranchnodefault(ctx) | |
289 | |
290 files = [] | |
291 parity = paritygen(web.stripecount) | |
292 for blockno, f in enumerate(ctx.files()): | |
293 template = f in ctx and 'filenodelink' or 'filenolink' | |
294 files.append(tmpl(template, | |
295 node=ctx.hex(), file=f, blockno=blockno + 1, | |
296 parity=parity.next())) | |
297 | |
298 basectx = basechangectx(web.repo, req) | |
299 if basectx is None: | |
300 basectx = ctx.p1() | |
301 | |
302 style = web.config('web', 'style', 'paper') | |
303 if 'style' in req.form: | |
304 style = req.form['style'][0] | |
305 | |
306 parity = paritygen(web.stripecount) | |
307 diff = diffs(web.repo, tmpl, ctx, basectx, None, parity, style) | |
308 | |
309 parity = paritygen(web.stripecount) | |
310 diffstatsgen = diffstatgen(ctx, basectx) | |
311 diffstats = diffstat(tmpl, ctx, diffstatsgen, parity) | |
312 | |
313 return dict( | |
314 diff=diff, | |
315 rev=ctx.rev(), | |
316 node=ctx.hex(), | |
317 parent=tuple(parents(ctx)), | |
318 child=children(ctx), | |
319 basenode=basectx.hex(), | |
320 changesettag=showtags, | |
321 changesetbookmark=showbookmarks, | |
322 changesetbranch=showbranch, | |
323 author=ctx.user(), | |
324 desc=ctx.description(), | |
325 extra=ctx.extra(), | |
326 date=ctx.date(), | |
327 files=files, | |
328 diffsummary=lambda **x: diffsummary(diffstatsgen), | |
329 diffstat=diffstats, | |
330 archives=web.archivelist(ctx.hex()), | |
331 tags=nodetagsdict(web.repo, ctx.node()), | |
332 bookmarks=nodebookmarksdict(web.repo, ctx.node()), | |
333 branch=nodebranchnodefault(ctx), | |
334 inbranch=nodeinbranch(web.repo, ctx), | |
335 branches=nodebranchdict(web.repo, ctx)) | |
281 | 336 |
282 def listfilediffs(tmpl, files, node, max): | 337 def listfilediffs(tmpl, files, node, max): |
283 for f in files[:max]: | 338 for f in files[:max]: |
284 yield tmpl('filedifflink', node=hex(node), file=f) | 339 yield tmpl('filedifflink', node=hex(node), file=f) |
285 if len(files) > max: | 340 if len(files) > max: |