Mercurial > public > src > rhodecode
comparison pylons_app/lib/helpers.py @ 250:be4621c6de58
Pygments code coloring rewrite, annotation was moved to vcs whitch had much better lib for that. Fixed code recognition based on mimetypes of filenodes, for better coloring.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 04 Jun 2010 00:23:55 +0200 |
parents | ea893ffb7f00 |
children | 6ada8c223374 |
comparison
equal
deleted
inserted
replaced
249:bad9ccac26b7 | 250:be4621c6de58 |
---|---|
1 """Helper functions | 1 """Helper functions |
2 | 2 |
3 Consists of functions to typically be used within templates, but also | 3 Consists of functions to typically be used within templates, but also |
4 available to Controllers. This module is available to both as 'h'. | 4 available to Controllers. This module is available to both as 'h'. |
5 """ | 5 """ |
6 from pygments.formatters import HtmlFormatter | |
7 from pygments import highlight as code_highlight | |
6 from pylons import url, app_globals as g | 8 from pylons import url, app_globals as g |
7 from pylons.i18n.translation import _, ungettext | 9 from pylons.i18n.translation import _, ungettext |
8 from webhelpers.html import (literal, HTML, escape) | 10 from vcs.utils.annotate import annotate_highlight |
11 from webhelpers.html import literal, HTML, escape | |
9 from webhelpers.html.builder import make_tag | 12 from webhelpers.html.builder import make_tag |
10 from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate | 13 from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \ |
11 , mail_to, strip_links, strip_tags, tag_re) | 14 end_form, file, form, hidden, image, javascript_link, link_to, link_to_if, \ |
12 from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes, | 15 link_to_unless, ol, required_legend, select, stylesheet_link, submit, text, \ |
13 end_form, file, form, hidden, image, | 16 password, textarea, title, ul, xml_declaration |
14 javascript_link, link_to, link_to_if, | 17 from webhelpers.html.tools import auto_link, button_to, highlight, js_obfuscate, \ |
15 link_to_unless, ol, required_legend, | 18 mail_to, strip_links, strip_tags, tag_re |
16 select, stylesheet_link, | 19 from webhelpers.number import format_byte_size, format_bit_size |
17 submit, text, password, textarea, title, | |
18 ul, xml_declaration) | |
19 from webhelpers.text import (chop_at, collapse, convert_accented_entities, | |
20 convert_misc_entities, lchop, plural, rchop, | |
21 remove_formatting, replace_whitespace, urlify) | |
22 from webhelpers.number import (format_byte_size, format_bit_size) | |
23 from webhelpers.pylonslib import Flash as _Flash | 20 from webhelpers.pylonslib import Flash as _Flash |
24 from webhelpers.pylonslib.secure_form import secure_form | 21 from webhelpers.pylonslib.secure_form import secure_form |
22 from webhelpers.text import chop_at, collapse, convert_accented_entities, \ | |
23 convert_misc_entities, lchop, plural, rchop, remove_formatting, \ | |
24 replace_whitespace, urlify | |
25 | 25 |
26 from pygments import highlight | |
27 from pygments.formatters import HtmlFormatter | |
28 from pygments.lexers import guess_lexer | |
29 from pygments.lexers import get_lexer_by_name | |
30 | 26 |
31 #Custom helper here :) | 27 #Custom helper here :) |
32 class _Link(object): | 28 class _Link(object): |
33 ''' | 29 ''' |
34 Make a url based on label and url with help of url_for | 30 Make a url based on label and url with help of url_for |
60 if p != '': | 56 if p != '': |
61 url_l.append(link_to(p, url('files_home', repo_name=repo_name, revision=rev, f_path='/'.join(paths_l[:cnt])))) | 57 url_l.append(link_to(p, url('files_home', repo_name=repo_name, revision=rev, f_path='/'.join(paths_l[:cnt])))) |
62 | 58 |
63 return literal(' / '.join(url_l)) | 59 return literal(' / '.join(url_l)) |
64 | 60 |
65 def pygmentize(code, **kwargs): | 61 def pygmentize(filenode, **kwargs): |
66 """ | 62 """ |
67 Filter for chunks of html to replace code tags with pygmented code | 63 pygmentize function using pygments |
64 @param filenode: | |
68 """ | 65 """ |
69 code = code.splitlines() | 66 return literal(code_highlight(filenode.content, filenode.lexer, HtmlFormatter(**kwargs))) |
70 _html, _html2 = [], [] | |
71 _html.append("""<table class="code-highlighttable">""") | |
72 _html.append("""<tr>""") | |
73 _html.append("""<td class="linenos">""") | |
74 _html.append("""<div class="linenodiv">""") | |
75 _html.append("""<pre>""") | |
76 for cnt, code in enumerate(code, 1): | |
77 #generete lines nos | |
78 _html.append("""<a id="A%s" href="#A%s">%s</a>\n""" \ | |
79 % (cnt, cnt, cnt)) | |
80 #propagate second list with code | |
81 _html2.append("""%s""" % (highlight(code, get_lexer_by_name('python'), | |
82 HtmlFormatter(nowrap=True)))) | |
83 _html.append("""</pre>""") | |
84 _html.append("""</div>""") | |
85 _html.append("""</td>""") | |
86 _html.append("""<td class="code">""") | |
87 _html.append("""<div class="code-highlight">""") | |
88 _html.append("""<pre>""") | |
89 _html.extend(_html2) | |
90 _html.append("""</pre>""") | |
91 _html.append("""</div>""") | |
92 _html.append("""</td>""") | |
93 _html.append("""</tr>""") | |
94 _html.append("""</table>""") | |
95 return literal(''.join(_html)) | |
96 #return literal(highlight(code, get_lexer_by_name('python'), HtmlFormatter(**kwargs))) | |
97 | 67 |
98 def pygmentize_annotation(annotate_list, repo_name): | 68 def pygmentize_annotation(filenode, **kwargs): |
99 """ | 69 """ |
100 Generate a dict of | 70 pygmentize function for annotation |
101 @param annotate_lists: | 71 @param filenode: |
102 """ | 72 """ |
103 import random | 73 |
104 color_dict = g.changeset_annotation_colors | 74 color_dict = g.changeset_annotation_colors |
105 def gen_color(): | 75 def gen_color(): |
106 return [str(random.randrange(0, 255)) for _ in xrange(3)] | 76 import random |
77 return [str(random.randrange(10, 235)) for _ in xrange(3)] | |
107 def get_color_string(cs): | 78 def get_color_string(cs): |
108 if color_dict.has_key(cs): | 79 if color_dict.has_key(cs): |
109 col = color_dict[cs] | 80 col = color_dict[cs] |
110 else: | 81 else: |
111 color_dict[cs] = gen_color() | 82 color_dict[cs] = gen_color() |
112 col = color_dict[cs] | 83 col = color_dict[cs] |
113 return "color: rgb(%s) ! important;" % (','.join(col)) | 84 return "color: rgb(%s) ! important;" % (','.join(col)) |
114 _html, _html2, _html3 = [], [], [] | 85 |
115 _html.append("""<table class="code-highlighttable">""") | 86 def url_func(changeset): |
116 _html.append("""<tr>""") | 87 return '%s\n' % (link_to(changeset.raw_id, |
117 _html.append("""<td class="linenos">""") | 88 url('changeset_home', repo_name='test', revision=changeset.raw_id), |
118 _html.append("""<div class="linenodiv">""") | 89 title=_('author') + ':%s rev:%s %s' % (changeset.author, changeset.revision, |
119 _html.append("""<pre>""") | 90 changeset.message,), |
120 for line in annotate_list: | 91 style=get_color_string(changeset.raw_id))) |
121 #lines | 92 |
122 _html.append("""<a id="A%s" href="#S%s">%s</a>\n""" \ | 93 return literal(annotate_highlight(filenode, url_func, **kwargs)) |
123 % (line[0], line[0], line[0])) | |
124 #annotation tags | |
125 _html2.append("""%s\n""" % link_to(line[1].raw_id, | |
126 url('changeset_home', repo_name=repo_name, revision=line[1].raw_id), | |
127 title=_('author') + ':%s rev:%s %s' % (line[1].author, line[1].revision, | |
128 line[1].message,), | |
129 style=get_color_string(line[1].raw_id))) | |
130 #code formated with pygments | |
131 _html3.append("""%s""" % (highlight(line[2], get_lexer_by_name('python') | |
132 , HtmlFormatter(nowrap=True)))) | |
133 _html.append("""</pre>""") | |
134 _html.append("""</div>""") | |
135 _html.append("""</td>""") | |
136 _html.append("""<td class="linenos">""") | |
137 _html.append("""<div class="linenodiv">""") | |
138 _html.append("""<pre>""") | |
139 _html.extend(_html2) | |
140 _html.append("""</pre>""") | |
141 _html.append("""</div>""") | |
142 _html.append("""</td>""") | |
143 _html.append("""<td class="code">""") | |
144 _html.append("""<div class="code-highlight">""") | |
145 _html.append("""<pre>""") | |
146 _html.extend(_html3) | |
147 _html.append("""</pre>""") | |
148 _html.append("""</div>""") | |
149 _html.append("""</td>""") | |
150 _html.append("""</tr>""") | |
151 _html.append("""</table>""") | |
152 return literal(''.join(_html)) | |
153 | |
154 | 94 |
155 files_breadcrumbs = _FilesBreadCrumbs() | 95 files_breadcrumbs = _FilesBreadCrumbs() |
156 link = _Link() | 96 link = _Link() |
157 flash = _Flash() | 97 flash = _Flash() |
158 get_error = _GetError() | 98 get_error = _GetError() |