annotate hgext/highlight/__init__.py @ 25602:85fb416f2fa7

hgweb: provide symrev (symbolic revision) property to the templates One of the features of hgweb is that current position in repo history is remembered between separate requests. That is, links from /rev/<node_hash> lead to /file/<node_hash> or /log/<node_hash>, so it's easy to dig deep into the history. However, such links could only use node hashes and local revision numbers, so while staying at one exact revision is easy, staying on top of the changes is not, because hashes presumably can't change (local revision numbers can, but probably not in a way you'd find useful for navigating). So while you could use 'tip' or 'default' in a url, links on that page would be permanent. This is not always desired (think /rev/tip or /graph/stable or /log/@) and is sometimes just confusing (i.e. /log/<not the tip hash>, when recent history is not displayed). And if user changed url deliberately to say default instead of <some node hash>, the page ignores that fact and uses node hash in its links, which means that navigation is, in a way, broken. This new property, symrev, is used for storing current revision the way it was specified, so then templates can use it in links and thus "not dereference" the symbolic revision. It is an additional way to produce links, so not every link needs to drop {node|short} in favor of {symrev}, many will still use node hash (log and filelog entries, annotate lines, etc). Some pages (e.g. summary, tags) always use the tip changeset for their context, in such cases symrev is set to 'tip'. This is needed in case the pages want to provide archive links. highlight extension needs to be updated, since _filerevision now takes an additional positional argument (signature "web, req, tmpl" is used by most of webcommands.py functions). More references to symbolic revisions and related gripes: issue2296, issue2826, issue3594, issue3634.
author Anton Shestakov <av6@dwimlabs.net>
date Tue, 16 Jun 2015 02:07:25 +0800
parents 80c5b2666a96
children 3166bcc0c538
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8251
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
1 # highlight - syntax highlighting in hgweb, based on Pygments
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
2 #
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
3 # Copyright 2008, 2009 Patrick Mezard <pmezard@gmail.com> and others
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
4 #
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9409
diff changeset
6 # GNU General Public License version 2 or any later version.
8251
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
7 #
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
8 # The original module was split in an interface and an implementation
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
9 # file to defer pygments loading and speedup extension setup.
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
10
8932
f87884329419 extensions: fix up description lines some more
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8894
diff changeset
11 """syntax highlighting for hgweb (requires Pygments)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
12
9262
917e1d5674d6 highlight: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9210
diff changeset
13 It depends on the Pygments syntax highlighting library:
917e1d5674d6 highlight: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9210
diff changeset
14 http://pygments.org/
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
15
9210
2667ca525b59 highlight: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9064
diff changeset
16 There is a single configuration option::
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
17
9210
2667ca525b59 highlight: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9064
diff changeset
18 [web]
2667ca525b59 highlight: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9064
diff changeset
19 pygments_style = <style>
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
20
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
21 The default is 'colorful'.
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
22 """
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
23
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
24 import highlight
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
25 from mercurial.hgweb import webcommands, webutil, common
8874
74baf78202e8 highlight: was broken since 580a79dde2a3 (encoding)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8866
diff changeset
26 from mercurial import extensions, encoding
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 19872
diff changeset
27 # Note for extension authors: ONLY specify testedwith = 'internal' for
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 19872
diff changeset
28 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 19872
diff changeset
29 # be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 19872
diff changeset
30 # leave the attribute unspecified.
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16683
diff changeset
31 testedwith = 'internal'
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
32
25602
85fb416f2fa7 hgweb: provide symrev (symbolic revision) property to the templates
Anton Shestakov <av6@dwimlabs.net>
parents: 25186
diff changeset
33 def filerevision_highlight(orig, web, req, tmpl, fctx):
8874
74baf78202e8 highlight: was broken since 580a79dde2a3 (encoding)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8866
diff changeset
34 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
6987
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
35 # only pygmentize for mimetype containing 'html' so we both match
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
36 # 'text/html' and possibly 'application/xhtml+xml' in the future
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
37 # so that we don't have to touch the extension when the mimetype
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
38 # for a template changes; also hgweb optimizes the case that a
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
39 # raw file is sent using rawfile() and doesn't call us, so we
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
40 # can't clash with the file's content-type here in case we
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
41 # pygmentize a html file
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
42 if 'html' in mt:
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
43 style = web.config('web', 'pygments_style', 'colorful')
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
44 highlight.pygmentize('fileline', fctx, style, tmpl)
25602
85fb416f2fa7 hgweb: provide symrev (symbolic revision) property to the templates
Anton Shestakov <av6@dwimlabs.net>
parents: 25186
diff changeset
45 return orig(web, req, tmpl, fctx)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
46
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
47 def annotate_highlight(orig, web, req, tmpl):
8874
74baf78202e8 highlight: was broken since 580a79dde2a3 (encoding)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8866
diff changeset
48 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
6987
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
49 if 'html' in mt:
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
50 fctx = webutil.filectx(web.repo, req)
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
51 style = web.config('web', 'pygments_style', 'colorful')
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
52 highlight.pygmentize('annotateline', fctx, style, tmpl)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
53 return orig(web, req, tmpl)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
54
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
55 def generate_css(web, req, tmpl):
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
56 pg_style = web.config('web', 'pygments_style', 'colorful')
19872
681f7b9213a4 check-code: check for spaces around = for named parameters
Mads Kiilerich <madski@unity3d.com>
parents: 16743
diff changeset
57 fmter = highlight.HtmlFormatter(style=pg_style)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
58 req.respond(common.HTTP_OK, 'text/css')
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 10263
diff changeset
59 return ['/* pygments_style = %s */\n\n' % pg_style,
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 10263
diff changeset
60 fmter.get_style_defs('')]
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
61
9409
57157a224037 highlight: move code from module top-level into extsetup
Martin Geisler <mg@lazybytes.net>
parents: 9262
diff changeset
62 def extsetup():
57157a224037 highlight: move code from module top-level into extsetup
Martin Geisler <mg@lazybytes.net>
parents: 9262
diff changeset
63 # monkeypatch in the new version
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 10263
diff changeset
64 extensions.wrapfunction(webcommands, '_filerevision',
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 10263
diff changeset
65 filerevision_highlight)
9409
57157a224037 highlight: move code from module top-level into extsetup
Martin Geisler <mg@lazybytes.net>
parents: 9262
diff changeset
66 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
57157a224037 highlight: move code from module top-level into extsetup
Martin Geisler <mg@lazybytes.net>
parents: 9262
diff changeset
67 webcommands.highlightcss = generate_css
57157a224037 highlight: move code from module top-level into extsetup
Martin Geisler <mg@lazybytes.net>
parents: 9262
diff changeset
68 webcommands.__all__.append('highlightcss')