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