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