Mercurial > kallithea
annotate pylons_app/lib/helpers.py @ 260:6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Switched message slug into webhelpers function
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 06 Jun 2010 21:54:54 +0200 |
parents | be4621c6de58 |
children | 0e5455fda8fd |
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 """ | |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
6 from pygments.formatters import HtmlFormatter |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
7 from pygments import highlight as code_highlight |
165
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
8 from pylons import url, app_globals as g |
97
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
9 from pylons.i18n.translation import _, ungettext |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
10 from vcs.utils.annotate import annotate_highlight |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
11 from webhelpers.html import literal, HTML, escape |
98
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
12 from webhelpers.html.builder import make_tag |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
13 from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
14 end_form, file, form, hidden, image, javascript_link, link_to, link_to_if, \ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
15 link_to_unless, ol, required_legend, select, stylesheet_link, submit, text, \ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
16 password, textarea, title, ul, xml_declaration |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
17 from webhelpers.html.tools import auto_link, button_to, highlight, js_obfuscate, \ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
18 mail_to, strip_links, strip_tags, tag_re |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
19 from webhelpers.number import format_byte_size, format_bit_size |
0 | 20 from webhelpers.pylonslib import Flash as _Flash |
21 from webhelpers.pylonslib.secure_form import secure_form | |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
22 from webhelpers.text import chop_at, collapse, convert_accented_entities, \ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
23 convert_misc_entities, lchop, plural, rchop, remove_formatting, \ |
260
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
24 replace_whitespace, urlify, truncate |
0 | 25 |
98
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
26 |
0 | 27 #Custom helper here :) |
28 class _Link(object): | |
29 ''' | |
30 Make a url based on label and url with help of url_for | |
31 @param label:name of link if not defined url is used | |
32 @param url: the url for link | |
33 ''' | |
34 | |
43 | 35 def __call__(self, label='', *url_, **urlargs): |
0 | 36 if label is None or '': |
37 label = url | |
43 | 38 link_fn = link_to(label, url(*url_, **urlargs)) |
0 | 39 return link_fn |
40 | |
41 | |
42 class _GetError(object): | |
43 | |
44 def __call__(self, field_name, form_errors): | |
45 tmpl = """<span class="error_msg">%s</span>""" | |
46 if form_errors and form_errors.has_key(field_name): | |
47 return literal(tmpl % form_errors.get(field_name)) | |
48 | |
102
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
49 class _FilesBreadCrumbs(object): |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
50 |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
51 def __call__(self, repo_name, rev, paths): |
104
4cea52709743
fixed file browser breadcrumbs with revision
Marcin Kuzminski <marcin@python-works.com>
parents:
102
diff
changeset
|
52 url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, revision=rev, f_path=''))] |
102
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
53 paths_l = paths.split('/') |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
54 |
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
55 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
|
56 if p != '': |
104
4cea52709743
fixed file browser breadcrumbs with revision
Marcin Kuzminski <marcin@python-works.com>
parents:
102
diff
changeset
|
57 url_l.append(link_to(p, url('files_home', repo_name=repo_name, revision=rev, f_path='/'.join(paths_l[:cnt])))) |
97
be0096a02772
added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents:
94
diff
changeset
|
58 |
102
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
59 return literal(' / '.join(url_l)) |
98
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
60 |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
61 def pygmentize(filenode, **kwargs): |
165
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
62 """ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
63 pygmentize function using pygments |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
64 @param filenode: |
165
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
65 """ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
66 return literal(code_highlight(filenode.content, filenode.lexer, HtmlFormatter(**kwargs))) |
165
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
67 |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
68 def pygmentize_annotation(filenode, **kwargs): |
165
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
69 """ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
70 pygmentize function for annotation |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
71 @param filenode: |
165
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
72 """ |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
73 |
165
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
74 color_dict = g.changeset_annotation_colors |
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
75 def gen_color(): |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
76 import random |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
77 return [str(random.randrange(10, 235)) for _ in xrange(3)] |
165
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
78 def get_color_string(cs): |
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
79 if color_dict.has_key(cs): |
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
80 col = color_dict[cs] |
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
81 else: |
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
82 color_dict[cs] = gen_color() |
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
83 col = color_dict[cs] |
ea893ffb7f00
implemented pygmentize codes into webhelpers. Together with color_dict caching into pylons globals
Marcin Kuzminski <marcin@python-works.com>
parents:
104
diff
changeset
|
84 return "color: rgb(%s) ! important;" % (','.join(col)) |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
85 |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
86 def url_func(changeset): |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
87 return '%s\n' % (link_to(changeset.raw_id, |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
88 url('changeset_home', repo_name='test', revision=changeset.raw_id), |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
89 title=_('author') + ':%s rev:%s %s' % (changeset.author, changeset.revision, |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
90 changeset.message,), |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
91 style=get_color_string(changeset.raw_id))) |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
92 |
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.
Marcin Kuzminski <marcin@python-works.com>
parents:
165
diff
changeset
|
93 return literal(annotate_highlight(filenode, url_func, **kwargs)) |
98
01d0f363f36d
added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents:
97
diff
changeset
|
94 |
260
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
95 def recursive_replace(str, replace=' '): |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
96 """ |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
97 Recursive replace of given sign to just one instance |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
98 @param str: given string |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
99 @param replace:char to find and replace multiple instances |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
100 |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
101 Examples:: |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
102 >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-') |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
103 'Mighty-Mighty-Bo-sstones' |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
104 """ |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
105 |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
106 if str.find(replace * 2) == -1: |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
107 return str |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
108 else: |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
109 str = str.replace(replace * 2, replace) |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
110 return recursive_replace(str, replace) |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
111 |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
112 def repo_name_slug(value): |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
113 """ |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
114 Return slug of name of repository |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
115 """ |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
116 slug = urlify(value) |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
117 for c in """=[]\;',/~!@#$%^&*()+{}|:""": |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
118 slug = slug.replace(c, '-') |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
119 print slug |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
120 slug = recursive_replace(slug, '-') |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
121 print slug |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
122 return slug |
6ada8c223374
made global funcion to clean repo names, and remove all special chars from the name.
Marcin Kuzminski <marcin@python-works.com>
parents:
250
diff
changeset
|
123 |
102
2dc0c8e4f384
Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents:
98
diff
changeset
|
124 files_breadcrumbs = _FilesBreadCrumbs() |
0 | 125 link = _Link() |
126 flash = _Flash() | |
127 get_error = _GetError() |