hgext/highlight/__init__.py
author Joerg Sonnenberger <joerg@bec.de>
Thu, 29 Oct 2020 13:29:05 +0100
changeset 45795 c4ab93849383
parent 44009 e685fac56693
child 48875 6000f5b25c9b
permissions -rw-r--r--
relnotes: mention improved memory use and underlaying API changes Differential Revision: https://phab.mercurial-scm.org/D9258
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
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
26680
7a3f6490ef97 highlight: add option to prevent content-only based fallback
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26679
diff changeset
    16
There are the following configuration options::
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]
26249
3166bcc0c538 highlight: add highlightfiles config option which takes a fileset (issue3005)
Anton Shestakov <av6@dwimlabs.net>
parents: 25602
diff changeset
    19
  pygments_style = <style> (default: colorful)
3166bcc0c538 highlight: add highlightfiles config option which takes a fileset (issue3005)
Anton Shestakov <av6@dwimlabs.net>
parents: 25602
diff changeset
    20
  highlightfiles = <fileset> (default: size('<5M'))
26680
7a3f6490ef97 highlight: add option to prevent content-only based fallback
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26679
diff changeset
    21
  highlightonlymatchfilename = <bool> (default False)
7a3f6490ef97 highlight: add option to prevent content-only based fallback
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26679
diff changeset
    22
7a3f6490ef97 highlight: add option to prevent content-only based fallback
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26679
diff changeset
    23
``highlightonlymatchfilename`` will only highlight files if their type could
7a3f6490ef97 highlight: add option to prevent content-only based fallback
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26679
diff changeset
    24
be identified by their filename. When this is not enabled (the default),
7a3f6490ef97 highlight: add option to prevent content-only based fallback
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26679
diff changeset
    25
Pygments will try very hard to identify the file type from content and any
7a3f6490ef97 highlight: add option to prevent content-only based fallback
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26679
diff changeset
    26
match (even matches with a low confidence score) will be used.
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    27
"""
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    28
29485
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    29
from __future__ import absolute_import
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    30
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    31
from . import highlight
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    32
from mercurial.hgweb import (
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    33
    webcommands,
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    34
    webutil,
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    35
)
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    36
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    37
from mercurial import (
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    38
    extensions,
42923
a7abc6081bc5 highlight: fix encoding issues to enable Py3 compatibility
Connor Sheehan <sheehan@mozilla.com>
parents: 41068
diff changeset
    39
    pycompat,
29485
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    40
)
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26680
diff changeset
    41
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29485
diff changeset
    42
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 19872
diff changeset
    43
# 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
    44
# 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
    45
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    46
testedwith = b'ships-with-hg-core'
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    47
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    48
26679
0d93df4d1e44 highlight: inline checkfctx()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26678
diff changeset
    49
def pygmentize(web, field, fctx, tmpl):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    50
    style = web.config(b'web', b'pygments_style', b'colorful')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    51
    expr = web.config(b'web', b'highlightfiles', b"size('<5M')")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    52
    filenameonly = web.configbool(b'web', b'highlightonlymatchfilename', False)
26679
0d93df4d1e44 highlight: inline checkfctx()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26678
diff changeset
    53
26249
3166bcc0c538 highlight: add highlightfiles config option which takes a fileset (issue3005)
Anton Shestakov <av6@dwimlabs.net>
parents: 25602
diff changeset
    54
    ctx = fctx.changectx()
44009
e685fac56693 match: resolve filesets against the passed `cwd`, not the current one
Matt Harbison <matt_harbison@yahoo.com>
parents: 43077
diff changeset
    55
    m = ctx.matchfileset(fctx.repo().root, expr)
38688
2570dca0f21c highlight: use matcher API to test if file should be pygmentized
Yuya Nishihara <yuya@tcha.org>
parents: 37018
diff changeset
    56
    if m(fctx.path()):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    57
        highlight.pygmentize(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    58
            field, fctx, style, tmpl, guessfilenameonly=filenameonly
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    59
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    60
26678
613d850cce53 highlight: consolidate duplicate code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26295
diff changeset
    61
36887
4daa22071d5d hgweb: stop passing req and tmpl into @webcommand functions (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36886
diff changeset
    62
def filerevision_highlight(orig, web, fctx):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    63
    mt = web.res.headers[b'Content-Type']
6987
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
    64
    # 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
    65
    # '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
    66
    # 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
    67
    # 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
    68
    # 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
    69
    # 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
    70
    # pygmentize a html file
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
    if b'html' in mt:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
        pygmentize(web, b'fileline', fctx, web.tmpl)
26678
613d850cce53 highlight: consolidate duplicate code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26295
diff changeset
    73
36887
4daa22071d5d hgweb: stop passing req and tmpl into @webcommand functions (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36886
diff changeset
    74
    return orig(web, fctx)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    75
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    76
36887
4daa22071d5d hgweb: stop passing req and tmpl into @webcommand functions (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36886
diff changeset
    77
def annotate_highlight(orig, web):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    78
    mt = web.res.headers[b'Content-Type']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
    if b'html' in mt:
36886
563fd95a6efb hgweb: pass modern request type into various webutil functions (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
    80
        fctx = webutil.filectx(web.repo, web.req)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    81
        pygmentize(web, b'annotateline', fctx, web.tmpl)
26678
613d850cce53 highlight: consolidate duplicate code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26295
diff changeset
    82
36887
4daa22071d5d hgweb: stop passing req and tmpl into @webcommand functions (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36886
diff changeset
    83
    return orig(web)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
    84
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    85
36887
4daa22071d5d hgweb: stop passing req and tmpl into @webcommand functions (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36886
diff changeset
    86
def generate_css(web):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    87
    pg_style = web.config(b'web', b'pygments_style', b'colorful')
42923
a7abc6081bc5 highlight: fix encoding issues to enable Py3 compatibility
Connor Sheehan <sheehan@mozilla.com>
parents: 41068
diff changeset
    88
    fmter = highlight.HtmlFormatter(style=pycompat.sysstr(pg_style))
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    89
    web.res.headers[b'Content-Type'] = b'text/css'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    90
    style_defs = fmter.get_style_defs(pycompat.sysstr(b''))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    91
    web.res.setbodybytes(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    92
        b''.join(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    93
            [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    94
                b'/* pygments_style = %s */\n\n' % pg_style,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    95
                pycompat.bytestr(style_defs),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    96
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    97
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
    98
    )
36880
67fb0dca29bc hgweb: always return iterable from @webcommand functions (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36871
diff changeset
    99
    return web.res.sendresponse()
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
   100
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
   101
41068
28a4fb793ba1 extensions: deprecate extsetup without a `ui` argument (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 38688
diff changeset
   102
def extsetup(ui):
9409
57157a224037 highlight: move code from module top-level into extsetup
Martin Geisler <mg@lazybytes.net>
parents: 9262
diff changeset
   103
    # monkeypatch in the new version
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
   104
    extensions.wrapfunction(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   105
        webcommands, b'_filerevision', filerevision_highlight
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42923
diff changeset
   106
    )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
    extensions.wrapfunction(webcommands, b'annotate', annotate_highlight)
9409
57157a224037 highlight: move code from module top-level into extsetup
Martin Geisler <mg@lazybytes.net>
parents: 9262
diff changeset
   108
    webcommands.highlightcss = generate_css
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   109
    webcommands.__all__.append(b'highlightcss')