Show More
@@ -1,145 +1,147 | |||
|
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 | |
|
42 | 42 | from pygments import highlight |
|
43 | 43 | from pygments.util import ClassNotFound |
|
44 | 44 | from pygments.lexers import guess_lexer_for_filename, TextLexer |
|
45 | 45 | from pygments.formatters import HtmlFormatter |
|
46 | 46 | |
|
47 | 47 | SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" ' |
|
48 | 48 | 'type="text/css" />') |
|
49 | 49 | |
|
50 | 50 | class StripedHtmlFormatter(HtmlFormatter): |
|
51 | 51 | def __init__(self, stripecount, *args, **kwargs): |
|
52 | 52 | super(StripedHtmlFormatter, self).__init__(*args, **kwargs) |
|
53 | 53 | self.stripecount = stripecount |
|
54 | 54 | |
|
55 | 55 | def wrap(self, source, outfile): |
|
56 | 56 | yield 0, "<div class='highlight'>" |
|
57 | 57 | yield 0, "<pre>" |
|
58 | 58 | parity = paritygen(self.stripecount) |
|
59 | 59 | |
|
60 | 60 | for n, i in source: |
|
61 | 61 | if n == 1: |
|
62 | 62 | i = "<div class='parity%s'>%s</div>" % (parity.next(), i) |
|
63 | 63 | yield n, i |
|
64 | 64 | |
|
65 | 65 | yield 0, "</pre>" |
|
66 | 66 | yield 0, "</div>" |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def pygments_format(filename, rawtext, forcetext=False, stripecount=1, |
|
70 | 70 | style='colorful'): |
|
71 | 71 | if not forcetext: |
|
72 | 72 | try: |
|
73 | 73 | lexer = guess_lexer_for_filename(filename, rawtext) |
|
74 | 74 | except ClassNotFound: |
|
75 | 75 | lexer = TextLexer() |
|
76 | 76 | else: |
|
77 | 77 | lexer = TextLexer() |
|
78 | 78 | |
|
79 | 79 | formatter = StripedHtmlFormatter(stripecount, style=style, |
|
80 | 80 | linenos='inline') |
|
81 | 81 | |
|
82 | 82 | return highlight(rawtext, lexer, formatter) |
|
83 | 83 | |
|
84 | 84 | |
|
85 | def filerevision_pygments(self, fctx): | |
|
85 | def filerevision_pygments(self, tmpl, fctx): | |
|
86 | 86 | """Reimplement hgweb.filerevision to use syntax highlighting""" |
|
87 |
f |
|
|
87 | f = fctx.path() | |
|
88 | 88 | |
|
89 | 89 | rawtext = fctx.data() |
|
90 | 90 | text = rawtext |
|
91 | 91 | |
|
92 | mt = mimetypes.guess_type(filename)[0] | |
|
92 | fl = fctx.filelog() | |
|
93 | n = fctx.filenode() | |
|
94 | ||
|
95 | mt = mimetypes.guess_type(f)[0] | |
|
93 | 96 | |
|
94 | 97 | if util.binary(text): |
|
95 | 98 | mt = mt or 'application/octet-stream' |
|
96 | 99 | text = "(binary:%s)" % mt |
|
97 | 100 | |
|
98 | 101 | # don't parse (binary:...) as anything |
|
99 | 102 | forcetext = True |
|
100 | 103 | else: |
|
101 | 104 | mt = mt or 'text/plain' |
|
102 | 105 | forcetext = False |
|
103 | 106 | |
|
104 | 107 | def lines(text): |
|
105 | 108 | for line in text.splitlines(True): |
|
106 | 109 | yield {"line": line} |
|
107 | 110 | |
|
108 | 111 | style = self.config("web", "pygments_style", "colorful") |
|
109 | 112 | |
|
110 |
text_formatted = lines(pygments_format(f |
|
|
113 | text_formatted = lines(pygments_format(f, text, | |
|
111 | 114 | forcetext=forcetext, |
|
112 | 115 | stripecount=self.stripecount, |
|
113 | 116 | style=style)) |
|
114 | 117 | |
|
115 | 118 | # override per-line template |
|
116 |
|
|
|
119 | tmpl.cache['fileline'] = '#line#' | |
|
117 | 120 | |
|
118 | 121 | # append a <link ...> to the syntax highlighting css |
|
119 |
old_header = ''.join( |
|
|
122 | old_header = ''.join(tmpl('header')) | |
|
120 | 123 | if SYNTAX_CSS not in old_header: |
|
121 | 124 | new_header = old_header + SYNTAX_CSS |
|
122 |
|
|
|
125 | tmpl.cache['header'] = new_header | |
|
123 | 126 | |
|
124 |
yield |
|
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
|
138 | fctx.filenode()), | |
|
139 | permissions=fctx.manifest().flags(filename)) | |
|
127 | yield tmpl("filerevision", | |
|
128 | file=f, | |
|
129 | path=hgweb_mod._up(f), # fixme: make public | |
|
130 | text=text_formatted, | |
|
131 | raw=rawtext, | |
|
132 | mimetype=mt, | |
|
133 | rev=fctx.rev(), | |
|
134 | node=hex(fctx.node()), | |
|
135 | author=fctx.user(), | |
|
136 | date=fctx.date(), | |
|
137 | desc=fctx.description(), | |
|
138 | parent=self.siblings(fctx.parents()), | |
|
139 | child=self.siblings(fctx.children()), | |
|
140 | rename=self.renamelink(fl, n), | |
|
141 | permissions=fctx.manifest().flags(f)) | |
|
140 | 142 | |
|
141 | 143 | |
|
142 | 144 | # monkeypatch in the new version |
|
143 | 145 | # should be safer than overriding the method in a derived class |
|
144 | 146 | # and then patching the class |
|
145 | 147 | hgweb.filerevision = filerevision_pygments |
General Comments 0
You need to be logged in to leave comments.
Login now