Debian Codegraph @

hgext/highlight/__init__.py

raw
1 # highlight - syntax highlighting in hgweb, based on Pygments
2 #
3 # Copyright 2008, 2009 Patrick Mezard <[email protected]> and others
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7 #
8 # The original module was split in an interface and an implementation
9 # file to defer pygments loading and speedup extension setup.
10
11 """syntax highlighting for hgweb (requires Pygments)
12
13 It depends on the Pygments syntax highlighting library:
14 http://pygments.org/
15
16 There are the following configuration options::
17
18 [web]
19 pygments_style = <style> (default: colorful)
20 highlightfiles = <fileset> (default: size('<5M'))
21 highlightonlymatchfilename = <bool> (default False)
22
23 ``highlightonlymatchfilename`` will only highlight files if their type could
24 be identified by their filename. When this is not enabled (the default),
25 Pygments will try very hard to identify the file type from content and any
26 match (even matches with a low confidence score) will be used.
27 """
28
29 from __future__ import annotations
30
31 from . import highlight
32 from mercurial.hgweb import (
33 webcommands,
34 webutil,
35 )
36
37 from mercurial import (
38 extensions,
39 pycompat,
40 )
41
42 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
44 # be specifying the version(s) of Mercurial they are tested with, or
45 # leave the attribute unspecified.
46 testedwith = b'ships-with-hg-core'
47
48
49 def pygmentize(web, field, fctx, tmpl):
50 style = web.config(b'web', b'pygments_style', b'colorful')
51 expr = web.config(b'web', b'highlightfiles', b"size('<5M')")
52 filenameonly = web.configbool(b'web', b'highlightonlymatchfilename', False)
53
54 ctx = fctx.changectx()
55 m = ctx.matchfileset(fctx.repo().root, expr)
56 if m(fctx.path()):
57 highlight.pygmentize(
58 field, fctx, style, tmpl, guessfilenameonly=filenameonly
59 )
60
61
62 def filerevision_highlight(orig, web, fctx):
63 mt = web.res.headers[b'Content-Type']
64 # only pygmentize for mimetype containing 'html' so we both match
65 # 'text/html' and possibly 'application/xhtml+xml' in the future
66 # so that we don't have to touch the extension when the mimetype
67 # for a template changes; also hgweb optimizes the case that a
68 # raw file is sent using rawfile() and doesn't call us, so we
69 # can't clash with the file's content-type here in case we
70 # pygmentize a html file
71 if b'html' in mt:
72 pygmentize(web, b'fileline', fctx, web.tmpl)
73
74 return orig(web, fctx)
75
76
77 def annotate_highlight(orig, web):
78 mt = web.res.headers[b'Content-Type']
79 if b'html' in mt:
80 fctx = webutil.filectx(web.repo, web.req)
81 pygmentize(web, b'annotateline', fctx, web.tmpl)
82
83 return orig(web)
84
85
86 def generate_css(web):
87 pg_style = web.config(b'web', b'pygments_style', b'colorful')
88 fmter = highlight.HtmlFormatter(style=pycompat.sysstr(pg_style))
89 web.res.headers[b'Content-Type'] = b'text/css'
90 style_defs = fmter.get_style_defs(pycompat.sysstr(b''))
91 web.res.setbodybytes(
92 b''.join(
93 [
94 b'/* pygments_style = %s */\n\n' % pg_style,
95 pycompat.bytestr(style_defs),
96 ]
97 )
98 )
99 return web.res.sendresponse()
100
101
102 def extsetup(ui):
103 # monkeypatch in the new version
104 extensions.wrapfunction(
105 webcommands, '_filerevision', filerevision_highlight
106 )
107 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
108 webcommands.highlightcss = generate_css
109 webcommands.__all__.append('highlightcss')