##// END OF EJS Templates
highlight: bail out if file is binary
Brendan Cully -
r6194:fe54e750 default
parent child Browse files
Show More
@@ -1,97 +1,100 b''
1 1 """
2 2 This is Mercurial extension for syntax highlighting in the file
3 3 revision view of hgweb.
4 4
5 5 It depends on the pygments syntax highlighting library:
6 6 http://pygments.org/
7 7
8 8 To enable the extension add this to hgrc:
9 9
10 10 [extensions]
11 11 hgext.highlight =
12 12
13 13 There is a single configuration option:
14 14
15 15 [web]
16 16 pygments_style = <style>
17 17
18 18 The default is 'colorful'. If this is changed the corresponding CSS
19 19 file should be re-generated by running
20 20
21 21 # pygmentize -f html -S <newstyle>
22 22
23 23
24 24 -- Adam Hupp <adam@hupp.org>
25 25
26 26
27 27 """
28 28
29 29 from mercurial import demandimport
30 30 demandimport.ignore.extend(['pkgutil',
31 31 'pkg_resources',
32 32 '__main__',])
33 33
34 34 import mimetypes
35 35
36 36 from mercurial.hgweb import hgweb_mod
37 37 from mercurial.hgweb.hgweb_mod import hgweb
38 38 from mercurial import util
39 39 from mercurial.hgweb.common import paritygen
40 40 from mercurial.node import hex
41 41 from mercurial.templatefilters import filters
42 42
43 43 from pygments import highlight
44 44 from pygments.util import ClassNotFound
45 45 from pygments.lexers import guess_lexer_for_filename, TextLexer
46 46 from pygments.formatters import HtmlFormatter
47 47
48 48 SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" '
49 49 'type="text/css" />')
50 50
51 51 def pygmentize(self, tmpl, fctx, field):
52 52 # append a <link ...> to the syntax highlighting css
53 53 old_header = ''.join(tmpl('header'))
54 54 if SYNTAX_CSS not in old_header:
55 55 new_header = old_header + SYNTAX_CSS
56 56 tmpl.cache['header'] = new_header
57 57
58 text = fctx.data()
59 if util.binary(text):
60 return
61
58 62 style = self.config("web", "pygments_style", "colorful")
59 63 # To get multi-line strings right, we can't format line-by-line
60 text = fctx.data()
61 64 try:
62 65 lexer = guess_lexer_for_filename(fctx.path(), text,
63 66 encoding=util._encoding)
64 67 except ClassNotFound:
65 68 lexer = TextLexer(encoding=util._encoding)
66 69
67 70 formatter = HtmlFormatter(style=style, encoding=util._encoding)
68 71
69 72 colorized = highlight(text, lexer, formatter)
70 73 # strip wrapping div
71 74 colorized = colorized[:colorized.find('\n</pre>')]
72 75 colorized = colorized[colorized.find('<span'):]
73 76 coloriter = (l for l in colorized.splitlines())
74 77
75 78 filters['colorize'] = lambda x: coloriter.next()
76 79
77 80 oldl = tmpl.cache[field]
78 81 newl = oldl.replace('line|escape', 'line|colorize')
79 82 tmpl.cache[field] = newl
80 83
81 84 def filerevision_highlight(self, tmpl, fctx):
82 85 pygmentize(self, tmpl, fctx, 'fileline')
83 86
84 87 return realrevision(self, tmpl, fctx)
85 88
86 89 def fileannotate_highlight(self, tmpl, fctx):
87 90 pygmentize(self, tmpl, fctx, 'annotateline')
88 91
89 92 return realannotate(self, tmpl, fctx)
90 93
91 94 # monkeypatch in the new version
92 95 # should be safer than overriding the method in a derived class
93 96 # and then patching the class
94 97 realrevision = hgweb.filerevision
95 98 hgweb.filerevision = filerevision_highlight
96 99 realannotate = hgweb.fileannotate
97 100 hgweb.fileannotate = fileannotate_highlight
General Comments 0
You need to be logged in to leave comments. Login now