##// END OF EJS Templates
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.
marcink -
r250:be4621c6 default
parent child Browse files
Show More
@@ -1,158 +1,98 b''
1 1 """Helper functions
2 2
3 3 Consists of functions to typically be used within templates, but also
4 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 8 from pylons import url, app_globals as g
7 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 12 from webhelpers.html.builder import make_tag
10 from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate
11 , mail_to, strip_links, strip_tags, tag_re)
12 from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes,
13 end_form, file, form, hidden, image,
14 javascript_link, link_to, link_to_if,
15 link_to_unless, ol, required_legend,
16 select, stylesheet_link,
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)
13 from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \
14 end_form, file, form, hidden, image, javascript_link, link_to, link_to_if, \
15 link_to_unless, ol, required_legend, select, stylesheet_link, submit, text, \
16 password, textarea, title, ul, xml_declaration
17 from webhelpers.html.tools import auto_link, button_to, highlight, js_obfuscate, \
18 mail_to, strip_links, strip_tags, tag_re
19 from webhelpers.number import format_byte_size, format_bit_size
23 20 from webhelpers.pylonslib import Flash as _Flash
24 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 27 #Custom helper here :)
32 28 class _Link(object):
33 29 '''
34 30 Make a url based on label and url with help of url_for
35 31 @param label:name of link if not defined url is used
36 32 @param url: the url for link
37 33 '''
38 34
39 35 def __call__(self, label='', *url_, **urlargs):
40 36 if label is None or '':
41 37 label = url
42 38 link_fn = link_to(label, url(*url_, **urlargs))
43 39 return link_fn
44 40
45 41
46 42 class _GetError(object):
47 43
48 44 def __call__(self, field_name, form_errors):
49 45 tmpl = """<span class="error_msg">%s</span>"""
50 46 if form_errors and form_errors.has_key(field_name):
51 47 return literal(tmpl % form_errors.get(field_name))
52 48
53 49 class _FilesBreadCrumbs(object):
54 50
55 51 def __call__(self, repo_name, rev, paths):
56 52 url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, revision=rev, f_path=''))]
57 53 paths_l = paths.split('/')
58 54
59 55 for cnt, p in enumerate(paths_l, 1):
60 56 if p != '':
61 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 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()
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)))
66 return literal(code_highlight(filenode.content, filenode.lexer, 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
101 @param annotate_lists:
70 pygmentize function for annotation
71 @param filenode:
102 72 """
103 import random
73
104 74 color_dict = g.changeset_annotation_colors
105 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 78 def get_color_string(cs):
108 79 if color_dict.has_key(cs):
109 80 col = color_dict[cs]
110 81 else:
111 82 color_dict[cs] = gen_color()
112 83 col = color_dict[cs]
113 84 return "color: rgb(%s) ! important;" % (','.join(col))
114 _html, _html2, _html3 = [], [], []
115 _html.append("""<table class="code-highlighttable">""")
116 _html.append("""<tr>""")
117 _html.append("""<td class="linenos">""")
118 _html.append("""<div class="linenodiv">""")
119 _html.append("""<pre>""")
120 for line in annotate_list:
121 #lines
122 _html.append("""<a id="A%s" href="#S%s">%s</a>\n""" \
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 85
86 def url_func(changeset):
87 return '%s\n' % (link_to(changeset.raw_id,
88 url('changeset_home', repo_name='test', revision=changeset.raw_id),
89 title=_('author') + ':%s rev:%s %s' % (changeset.author, changeset.revision,
90 changeset.message,),
91 style=get_color_string(changeset.raw_id)))
92
93 return literal(annotate_highlight(filenode, url_func, **kwargs))
154 94
155 95 files_breadcrumbs = _FilesBreadCrumbs()
156 96 link = _Link()
157 97 flash = _Flash()
158 98 get_error = _GetError()
@@ -1,44 +1,44 b''
1 1 <%inherit file="/base/base.html"/>
2 2
3 3 <%def name="title()">
4 4 ${_('File annotate')}
5 5 </%def>
6 6 <%def name="breadcrumbs()">
7 7 ${h.link_to(u'Home',h.url('/'))}
8 8 /
9 9 ${h.link_to(c.repo_name,h.url('files_home',repo_name=c.repo_name))}
10 10 /
11 11 ${_('files')}
12 12 </%def>
13 13 <%def name="page_nav()">
14 14 ${self.menu('files')}
15 15 </%def>
16 16 <%def name="css()">
17 17 <link rel="stylesheet" href="/css/monoblue_custom.css" type="text/css" />
18 18 <link rel="stylesheet" href="/css/pygments.css" type="text/css" />
19 19 </%def>
20 20 <%def name="main()">
21 21 <h2 class="no-link no-border">${_('Annotate')}</h2>
22 22 <div id="files_data">
23 23 <h2>${_('Location')}: ${h.files_breadcrumbs(c.repo_name,c.cur_rev,c.file.path)}</h2>
24 24 <dl class="overview">
25 25 <dt>${_('Revision')}</dt>
26 26 <dd>r${c.file.last_changeset.revision}:${c.file.last_changeset._short}</dd>
27 27 <dt>${_('Size')}</dt>
28 28 <dd>${h.format_byte_size(c.file.size,binary=True)}</dd>
29 29 <dt>${_('Options')}</dt>
30 30 <dd>${h.link_to(_('source'),
31 31 h.url('files_home',repo_name=c.repo_name,revision=c.cur_rev,f_path=c.f_path))} / ${h.link_to(_('raw'),
32 32 h.url('files_raw_home',repo_name=c.repo_name,revision=c.cur_rev,f_path=c.f_path))}</dd>
33 33 </dl>
34 34 <div id="body" class="codeblock">
35 35 <div class="code-header">
36 36 <div class="revision">${c.file.name}@r${c.file.last_changeset.revision}:${c.file.last_changeset._short}</div>
37 37 <div class="commit" style="font-size:70%">"${c.file_msg}"</div>
38 38 </div>
39 39 <div class="code-body">
40 ${h.pygmentize_annotation(c.annotate,c.repo_name)}
40 ${h.pygmentize_annotation(c.file,linenos=True,anchorlinenos=True,lineanchors='S',cssclass="code-highlight")}
41 41 </div>
42 42 </div>
43 43 </div>
44 44 </%def> No newline at end of file
@@ -1,28 +1,28 b''
1 1 <dl class="overview">
2 2 <dt>${_('Revision')}</dt>
3 3 <dd>r${c.files_list.last_changeset.revision}:${c.files_list.last_changeset._short}</dd>
4 4 <dt>${_('Size')}</dt>
5 5 <dd>${h.format_byte_size(c.files_list.size,binary=True)}</dd>
6 6 <dt>${_('Options')}</dt>
7 7 <dd>${h.link_to(_('annotate'),
8 8 h.url('files_annotate_home',repo_name=c.repo_name,revision=c.cur_rev,f_path=c.f_path))} / ${h.link_to(_('raw'),
9 9 h.url('files_raw_home',repo_name=c.repo_name,revision=c.cur_rev,f_path=c.f_path))}</dd>
10 10 <dt>${_('History')}</dt>
11 11 <dd>
12 12 ${h.form(h.url('files_diff_home',repo_name=c.repo_name,f_path=c.f_path),method='GET')}
13 13 ${h.hidden('diff2',c.files_list.last_changeset._short)}
14 14 ${h.select('diff1','',c.file_history)}
15 15 ${h.submit('diff','diff')}
16 16 ${h.end_form()}
17 17 </dd>
18 18
19 19 </dl>
20 20 <div id="body" class="codeblock">
21 21 <div class="code-header">
22 22 <div class="revision">${c.files_list.name}@r${c.files_list.last_changeset.revision}:${c.files_list.last_changeset._short}</div>
23 23 <div class="commit" style="font-size:70%">"${c.file_msg}"</div>
24 24 </div>
25 25 <div class="code-body">
26 ${h.pygmentize(c.files_list.content,linenos=True,anchorlinenos=True,cssclass="code-highlight")}
26 ${h.pygmentize(c.files_list,linenos=True,anchorlinenos=True,lineanchors='S',cssclass="code-highlight")}
27 27 </div>
28 28 </div> No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now