Mercurial > hg
comparison hgext/highlight.py @ 5533:6cf7d7fe7d3d
highlight: clean up coding style a little
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Mon, 12 Nov 2007 10:49:26 -0800 |
parents | 40a06e39f010 |
children | 88ca3e0fb6e5 |
comparison
equal
deleted
inserted
replaced
5532:40a06e39f010 | 5533:6cf7d7fe7d3d |
---|---|
42 from pygments import highlight | 42 from pygments import highlight |
43 from pygments.util import ClassNotFound | 43 from pygments.util import ClassNotFound |
44 from pygments.lexers import guess_lexer_for_filename, TextLexer | 44 from pygments.lexers import guess_lexer_for_filename, TextLexer |
45 from pygments.formatters import HtmlFormatter | 45 from pygments.formatters import HtmlFormatter |
46 | 46 |
47 SYNTAX_CSS = '\n<link rel="stylesheet" href="#staticurl#highlight.css" type="text/css" />' | 47 SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" ' |
48 'type="text/css" />') | |
48 | 49 |
49 class StripedHtmlFormatter(HtmlFormatter): | 50 class StripedHtmlFormatter(HtmlFormatter): |
50 | |
51 def __init__(self, stripecount, *args, **kwargs): | 51 def __init__(self, stripecount, *args, **kwargs): |
52 super(StripedHtmlFormatter, self).__init__(*args, **kwargs) | 52 super(StripedHtmlFormatter, self).__init__(*args, **kwargs) |
53 self.stripecount = stripecount | 53 self.stripecount = stripecount |
54 | 54 |
55 def wrap(self, source, outfile): | 55 def wrap(self, source, outfile): |
56 yield 0, "<div class='highlight'>" | 56 yield 0, "<div class='highlight'>" |
57 yield 0, "<pre>" | 57 yield 0, "<pre>" |
58 parity = paritygen(self.stripecount) | 58 parity = paritygen(self.stripecount) |
59 | 59 |
60 for n, i in source: | 60 for n, i in source: |
61 if n == 1: | 61 if n == 1: |
62 i = "<div class='parity%s'>%s</div>" % \ | 62 i = "<div class='parity%s'>%s</div>" % (parity.next(), i) |
63 (parity.next(), i) | |
64 yield n, i | 63 yield n, i |
65 | 64 |
66 yield 0, "</pre>" | 65 yield 0, "</pre>" |
67 yield 0, "</div>" | 66 yield 0, "</div>" |
68 | 67 |
69 | 68 |
70 def pygments_format(filename, rawtext, | 69 def pygments_format(filename, rawtext, forcetext=False, stripecount=1, |
71 forcetext=False, | |
72 stripecount=1, | |
73 style='colorful'): | 70 style='colorful'): |
74 | |
75 if not forcetext: | 71 if not forcetext: |
76 try: | 72 try: |
77 lexer = guess_lexer_for_filename(filename, rawtext) | 73 lexer = guess_lexer_for_filename(filename, rawtext) |
78 except ClassNotFound: | 74 except ClassNotFound: |
79 lexer = TextLexer() | 75 lexer = TextLexer() |
80 else: | 76 else: |
81 lexer = TextLexer() | 77 lexer = TextLexer() |
82 | 78 |
83 formatter = StripedHtmlFormatter(stripecount, | 79 formatter = StripedHtmlFormatter(stripecount, style=style, |
84 style=style, | |
85 linenos='inline') | 80 linenos='inline') |
86 | 81 |
87 return highlight(rawtext, lexer, formatter) | 82 return highlight(rawtext, lexer, formatter) |
88 | 83 |
89 | 84 |
90 """ | |
91 This reimplements hgweb.filerevision to use syntax highlighting | |
92 """ | |
93 def filerevision_pygments(self, fctx): | 85 def filerevision_pygments(self, fctx): |
86 """Reimplement hgweb.filerevision to use syntax highlighting""" | |
94 filename = fctx.path() | 87 filename = fctx.path() |
95 | 88 |
96 rawtext = fctx.data() | 89 rawtext = fctx.data() |
97 text = rawtext | 90 text = rawtext |
98 | 91 |
106 forcetext = True | 99 forcetext = True |
107 else: | 100 else: |
108 mt = mt or 'text/plain' | 101 mt = mt or 'text/plain' |
109 forcetext = False | 102 forcetext = False |
110 | 103 |
111 | |
112 def lines(text): | 104 def lines(text): |
113 for line in text.splitlines(True): | 105 for line in text.splitlines(True): |
114 yield {"line": line} | 106 yield {"line": line} |
115 | 107 |
116 style = self.config("web", "pygments_style", "colorful") | 108 style = self.config("web", "pygments_style", "colorful") |
118 text_formatted = lines(pygments_format(filename, text, | 110 text_formatted = lines(pygments_format(filename, text, |
119 forcetext=forcetext, | 111 forcetext=forcetext, |
120 stripecount=self.stripecount, | 112 stripecount=self.stripecount, |
121 style=style)) | 113 style=style)) |
122 | 114 |
123 # override per-line template | 115 # override per-line template |
124 self.t.cache['fileline'] = '#line#' | 116 self.t.cache['fileline'] = '#line#' |
125 | 117 |
126 # append a <link ...> to the syntax highlighting css | 118 # append a <link ...> to the syntax highlighting css |
127 old_header = ''.join(self.t('header')) | 119 old_header = ''.join(self.t('header')) |
128 if SYNTAX_CSS not in old_header: | 120 if SYNTAX_CSS not in old_header: |
129 new_header = old_header + SYNTAX_CSS | 121 new_header = old_header + SYNTAX_CSS |
130 self.t.cache['header'] = new_header | 122 self.t.cache['header'] = new_header |
131 | 123 |
132 | |
133 yield self.t("filerevision", | 124 yield self.t("filerevision", |
134 file=filename, | 125 file=filename, |
135 path=hgweb_mod._up(filename), # fixme: make public | 126 path=hgweb_mod._up(filename), # fixme: make public |
136 text=text_formatted, | 127 text=text_formatted, |
137 raw=rawtext, | 128 raw=rawtext, |
145 child=self.siblings(fctx.children()), | 136 child=self.siblings(fctx.children()), |
146 rename=self.renamelink(fctx.filelog(), | 137 rename=self.renamelink(fctx.filelog(), |
147 fctx.filenode()), | 138 fctx.filenode()), |
148 permissions=fctx.manifest().flags(filename)) | 139 permissions=fctx.manifest().flags(filename)) |
149 | 140 |
141 | |
150 # monkeypatch in the new version | 142 # monkeypatch in the new version |
151 # should be safer than overriding the method in a derived class | 143 # should be safer than overriding the method in a derived class |
152 # and then patching the class | 144 # and then patching the class |
153 hgweb.filerevision = filerevision_pygments | 145 hgweb.filerevision = filerevision_pygments |