##// END OF EJS Templates
highlight: clean up coding style a little
Bryan O'Sullivan -
r5533:6cf7d7fe default
parent child Browse files
Show More
@@ -44,14 +44,14 b' 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>"
@@ -59,19 +59,15 b' class StripedHtmlFormatter(HtmlFormatter'
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)
@@ -79,18 +75,15 b' def pygments_format(filename, rawtext,'
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()
@@ -108,7 +101,6 b' def filerevision_pygments(self, fctx):'
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}
@@ -120,7 +112,7 b' def filerevision_pygments(self, fctx):'
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
@@ -129,7 +121,6 b' def filerevision_pygments(self, fctx):'
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
@@ -147,6 +138,7 b' def filerevision_pygments(self, fctx):'
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
General Comments 0
You need to be logged in to leave comments. Login now