Show More
@@ -1,95 +1,95 | |||
|
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 | 6 | from pylons import url |
|
7 | 7 | from pylons.i18n.translation import _, ungettext |
|
8 | 8 | from webhelpers.html import (literal, HTML, escape) |
|
9 | 9 | from webhelpers.html.builder import make_tag |
|
10 | 10 | from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate |
|
11 | 11 | , mail_to, strip_links, strip_tags, tag_re) |
|
12 | 12 | from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes, |
|
13 | 13 | end_form, file, form, hidden, image, |
|
14 | 14 | javascript_link, link_to, link_to_if, |
|
15 | 15 | link_to_unless, ol, required_legend, |
|
16 | 16 | select, stylesheet_link, |
|
17 | 17 | submit, text, password, textarea, title, |
|
18 | 18 | ul, xml_declaration) |
|
19 | 19 | from webhelpers.text import (chop_at, collapse, convert_accented_entities, |
|
20 | 20 | convert_misc_entities, lchop, plural, rchop, |
|
21 | 21 | remove_formatting, replace_whitespace, urlify) |
|
22 | 22 | |
|
23 | 23 | from webhelpers.pylonslib import Flash as _Flash |
|
24 | 24 | from webhelpers.pylonslib.secure_form import secure_form |
|
25 | 25 | |
|
26 | 26 | from pygments import highlight |
|
27 | 27 | from pygments.formatters import HtmlFormatter |
|
28 | 28 | from pygments.lexers import guess_lexer |
|
29 | 29 | from pygments.lexers import get_lexer_by_name |
|
30 | 30 | |
|
31 | 31 | #Custom helper here :) |
|
32 | 32 | class _Link(object): |
|
33 | 33 | ''' |
|
34 | 34 | Make a url based on label and url with help of url_for |
|
35 | 35 | @param label:name of link if not defined url is used |
|
36 | 36 | @param url: the url for link |
|
37 | 37 | ''' |
|
38 | 38 | |
|
39 | 39 | def __call__(self, label='', *url_, **urlargs): |
|
40 | 40 | if label is None or '': |
|
41 | 41 | label = url |
|
42 | 42 | link_fn = link_to(label, url(*url_, **urlargs)) |
|
43 | 43 | return link_fn |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | class _GetError(object): |
|
47 | 47 | |
|
48 | 48 | def __call__(self, field_name, form_errors): |
|
49 | 49 | tmpl = """<span class="error_msg">%s</span>""" |
|
50 | 50 | if form_errors and form_errors.has_key(field_name): |
|
51 | 51 | return literal(tmpl % form_errors.get(field_name)) |
|
52 | 52 | |
|
53 | 53 | class _FileSizeFormat(object): |
|
54 | 54 | """ |
|
55 | 55 | Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, |
|
56 | 56 | 102 bytes, etc). |
|
57 | 57 | """ |
|
58 | 58 | def __call__(self, bytes): |
|
59 | 59 | try: |
|
60 | 60 | bytes = float(bytes) |
|
61 | 61 | except TypeError: |
|
62 | 62 | return u"0 bytes" |
|
63 | 63 | |
|
64 | 64 | if bytes < 1024: |
|
65 | 65 | return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes} |
|
66 | 66 | if bytes < 1024 * 1024: |
|
67 | 67 | return _("%.1f KB") % (bytes / 1024) |
|
68 | 68 | if bytes < 1024 * 1024 * 1024: |
|
69 | 69 | return _("%.1f MB") % (bytes / (1024 * 1024)) |
|
70 | 70 | return _("%.1f GB") % (bytes / (1024 * 1024 * 1024)) |
|
71 | 71 | |
|
72 | 72 | class _FilesBreadCrumbs(object): |
|
73 | 73 | |
|
74 | 74 | def __call__(self, repo_name, rev, paths): |
|
75 | url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, f_path=''))] | |
|
75 | url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, revision=rev, f_path=''))] | |
|
76 | 76 | paths_l = paths.split('/') |
|
77 | 77 | |
|
78 | 78 | for cnt, p in enumerate(paths_l, 1): |
|
79 | 79 | if p != '': |
|
80 | url_l.append(link_to(p, url('files_home', repo_name=repo_name, f_path='/'.join(paths_l[:cnt])))) | |
|
80 | url_l.append(link_to(p, url('files_home', repo_name=repo_name, revision=rev, f_path='/'.join(paths_l[:cnt])))) | |
|
81 | 81 | |
|
82 | 82 | return literal(' / '.join(url_l)) |
|
83 | 83 | |
|
84 | 84 | def pygmentize(code, **kwargs): |
|
85 | 85 | ''' |
|
86 | 86 | Filter for chunks of html to replace code tags with pygmented code |
|
87 | 87 | ''' |
|
88 | 88 | return literal(highlight(code, guess_lexer(code), HtmlFormatter(**kwargs))) |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | files_breadcrumbs = _FilesBreadCrumbs() |
|
92 | 92 | filesizeformat = _FileSizeFormat() |
|
93 | 93 | link = _Link() |
|
94 | 94 | flash = _Flash() |
|
95 | 95 | get_error = _GetError() |
General Comments 0
You need to be logged in to leave comments.
Login now