Mercurial > public > src > rhodecode
annotate pylons_app/lib/helpers.py @ 102:2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 25 Apr 2010 18:42:58 +0200 |
parents | 01d0f363f36d |
children | 4cea52709743 |
rev | line source |
---|---|
0 | 1 """Helper functions |
2 | |
3 Consists of functions to typically be used within templates, but also | |
4 available to Controllers. This module is available to both as 'h'. | |
5 """ | |
43 | 6 from pylons import url |
97
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
7 from pylons.i18n.translation import _, ungettext |
0 | 8 from webhelpers.html import (literal, HTML, escape) |
98
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
9 from webhelpers.html.builder import make_tag |
0 | 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, | |
94
0bb9391bc287
webhelpers update
Marcin Kuzminski <marcin@python-works.com>
parents:
45
diff
changeset
|
17 submit, text, password, textarea, title, |
0bb9391bc287
webhelpers update
Marcin Kuzminski <marcin@python-works.com>
parents:
45
diff
changeset
|
18 ul, xml_declaration) |
0 | 19 from webhelpers.text import (chop_at, collapse, convert_accented_entities, |
94
0bb9391bc287
webhelpers update
Marcin Kuzminski <marcin@python-works.com>
parents:
45
diff
changeset
|
20 convert_misc_entities, lchop, plural, rchop, |
0bb9391bc287
webhelpers update
Marcin Kuzminski <marcin@python-works.com>
parents:
45
diff
changeset
|
21 remove_formatting, replace_whitespace, urlify) |
0 | 22 |
23 from webhelpers.pylonslib import Flash as _Flash | |
24 from webhelpers.pylonslib.secure_form import secure_form | |
25 | |
98
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
26 from pygments import highlight |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
27 from pygments.formatters import HtmlFormatter |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
28 from pygments.lexers import guess_lexer |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
29 from pygments.lexers import get_lexer_by_name |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
30 |
0 | 31 #Custom helper here :) |
32 class _Link(object): | |
33 ''' | |
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 | |
36 @param url: the url for link | |
37 ''' | |
38 | |
43 | 39 def __call__(self, label='', *url_, **urlargs): |
0 | 40 if label is None or '': |
41 label = url | |
43 | 42 link_fn = link_to(label, url(*url_, **urlargs)) |
0 | 43 return link_fn |
44 | |
45 | |
46 class _GetError(object): | |
47 | |
48 def __call__(self, field_name, form_errors): | |
49 tmpl = """<span class="error_msg">%s</span>""" | |
50 if form_errors and form_errors.has_key(field_name): | |
51 return literal(tmpl % form_errors.get(field_name)) | |
52 | |
98
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
53 class _FileSizeFormat(object): |
97
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
54 """ |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
55 Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
56 102 bytes, etc). |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
57 """ |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
58 def __call__(self, bytes): |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
59 try: |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
60 bytes = float(bytes) |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
61 except TypeError: |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
62 return u"0 bytes" |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
63 |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
64 if bytes < 1024: |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
65 return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes} |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
66 if bytes < 1024 * 1024: |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
67 return _("%.1f KB") % (bytes / 1024) |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
68 if bytes < 1024 * 1024 * 1024: |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
69 return _("%.1f MB") % (bytes / (1024 * 1024)) |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
70 return _("%.1f GB") % (bytes / (1024 * 1024 * 1024)) |
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
71 |
102
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
72 class _FilesBreadCrumbs(object): |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
73 |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
74 def __call__(self, repo_name, rev, paths): |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
75 url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, f_path=''))] |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
76 paths_l = paths.split('/') |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
77 |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
78 for cnt, p in enumerate(paths_l, 1): |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
79 if p != '': |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
80 url_l.append(link_to(p, url('files_home', repo_name=repo_name, f_path='/'.join(paths_l[:cnt])))) |
97
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
81 |
102
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
82 return literal(' / '.join(url_l)) |
98
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
83 |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
84 def pygmentize(code, **kwargs): |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
85 ''' |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
86 Filter for chunks of html to replace code tags with pygmented code |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
87 ''' |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
88 return literal(highlight(code, guess_lexer(code), HtmlFormatter(**kwargs))) |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
89 |
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
90 |
102
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
91 files_breadcrumbs = _FilesBreadCrumbs() |
97
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
92 filesizeformat = _FileSizeFormat() |
0 | 93 link = _Link() |
94 flash = _Flash() | |
95 get_error = _GetError() |