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