hgext/highlight/__init__.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Wed, 24 Jun 2009 12:25:56 +0200
changeset 8932 f87884329419
parent 8894 868670dbc237
child 8949 8d985f0e54ae
permissions -rw-r--r--
extensions: fix up description lines some more
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
7fc30044b514 highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7216
diff changeset
     6
# GNU General Public License version 2, incorporated herein by reference.
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
8666
88edf62870f4 highlight: consistently capitalize Pygments
Martin Geisler <mg@lazybytes.net>
parents: 8251
diff changeset
    13
It depends on the Pygments syntax highlighting library:
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    14
http://pygments.org/
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    15
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    16
There is a single configuration option:
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    17
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    18
[web]
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    19
pygments_style = <style>
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
-- Adam Hupp <adam@hupp.org>
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    24
"""
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    25
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    26
import highlight
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    27
from mercurial.hgweb import webcommands, webutil, common
8874
74baf78202e8 highlight: was broken since 580a79dde2a3 (encoding)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8866
diff changeset
    28
from mercurial import extensions, encoding
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    29
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
    30
def filerevision_highlight(orig, web, tmpl, fctx):
8874
74baf78202e8 highlight: was broken since 580a79dde2a3 (encoding)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8866
diff changeset
    31
    mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
6987
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    32
    # 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
    33
    # '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
    34
    # 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
    35
    # 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
    36
    # 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
    37
    # 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
    38
    # pygmentize a html file
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    39
    if 'html' in mt:
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    40
        style = web.config('web', 'pygments_style', 'colorful')
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    41
        highlight.pygmentize('fileline', fctx, style, tmpl)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
    42
    return orig(web, tmpl, fctx)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    43
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
    44
def annotate_highlight(orig, web, req, tmpl):
8874
74baf78202e8 highlight: was broken since 580a79dde2a3 (encoding)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8866
diff changeset
    45
    mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
6987
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    46
    if 'html' in mt:
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    47
        fctx = webutil.filectx(web.repo, req)
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    48
        style = web.config('web', 'pygments_style', 'colorful')
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    49
        highlight.pygmentize('annotateline', fctx, style, tmpl)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
    50
    return orig(web, req, tmpl)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    51
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    52
def generate_css(web, req, tmpl):
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    53
    pg_style = web.config('web', 'pygments_style', 'colorful')
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    54
    fmter = highlight.HtmlFormatter(style = pg_style)
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    55
    req.respond(common.HTTP_OK, 'text/css')
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    56
    return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')]
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    57
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    58
# monkeypatch in the new version
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
    59
extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight)
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
    60
extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    61
webcommands.highlightcss = generate_css
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    62
webcommands.__all__.append('highlightcss')