Show More
@@ -1,238 +1,266 | |||
|
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 pygments.formatters import HtmlFormatter |
|
7 | 7 | from pygments import highlight as code_highlight |
|
8 | 8 | from pylons import url, app_globals as g |
|
9 | 9 | from pylons.i18n.translation import _, ungettext |
|
10 | 10 | from vcs.utils.annotate import annotate_highlight |
|
11 | 11 | from webhelpers.html import literal, HTML, escape |
|
12 | 12 | from webhelpers.html.tools import * |
|
13 | 13 | from webhelpers.html.builder import make_tag |
|
14 | 14 | from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \ |
|
15 | 15 | end_form, file, form, hidden, image, javascript_link, link_to, link_to_if, \ |
|
16 | 16 | link_to_unless, ol, required_legend, select, stylesheet_link, submit, text, \ |
|
17 | 17 | password, textarea, title, ul, xml_declaration, radio |
|
18 | 18 | from webhelpers.html.tools import auto_link, button_to, highlight, js_obfuscate, \ |
|
19 | 19 | mail_to, strip_links, strip_tags, tag_re |
|
20 | 20 | from webhelpers.number import format_byte_size, format_bit_size |
|
21 | 21 | from webhelpers.pylonslib import Flash as _Flash |
|
22 | 22 | from webhelpers.pylonslib.secure_form import secure_form |
|
23 | 23 | from webhelpers.text import chop_at, collapse, convert_accented_entities, \ |
|
24 | 24 | convert_misc_entities, lchop, plural, rchop, remove_formatting, \ |
|
25 | 25 | replace_whitespace, urlify, truncate, wrap_paragraphs |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | #Custom helpers here :) |
|
29 | 29 | class _Link(object): |
|
30 | 30 | ''' |
|
31 | 31 | Make a url based on label and url with help of url_for |
|
32 | 32 | @param label:name of link if not defined url is used |
|
33 | 33 | @param url: the url for link |
|
34 | 34 | ''' |
|
35 | 35 | |
|
36 | 36 | def __call__(self, label='', *url_, **urlargs): |
|
37 | 37 | if label is None or '': |
|
38 | 38 | label = url |
|
39 | 39 | link_fn = link_to(label, url(*url_, **urlargs)) |
|
40 | 40 | return link_fn |
|
41 | 41 | |
|
42 | 42 | link = _Link() |
|
43 | 43 | |
|
44 | 44 | class _GetError(object): |
|
45 | 45 | |
|
46 | 46 | def __call__(self, field_name, form_errors): |
|
47 | 47 | tmpl = """<span class="error_msg">%s</span>""" |
|
48 | 48 | if form_errors and form_errors.has_key(field_name): |
|
49 | 49 | return literal(tmpl % form_errors.get(field_name)) |
|
50 | 50 | |
|
51 | 51 | get_error = _GetError() |
|
52 | 52 | |
|
53 | 53 | def recursive_replace(str, replace=' '): |
|
54 | 54 | """ |
|
55 | 55 | Recursive replace of given sign to just one instance |
|
56 | 56 | @param str: given string |
|
57 | 57 | @param replace:char to find and replace multiple instances |
|
58 | 58 | |
|
59 | 59 | Examples:: |
|
60 | 60 | >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-') |
|
61 | 61 | 'Mighty-Mighty-Bo-sstones' |
|
62 | 62 | """ |
|
63 | 63 | |
|
64 | 64 | if str.find(replace * 2) == -1: |
|
65 | 65 | return str |
|
66 | 66 | else: |
|
67 | 67 | str = str.replace(replace * 2, replace) |
|
68 | 68 | return recursive_replace(str, replace) |
|
69 | 69 | |
|
70 | 70 | class _ToolTip(object): |
|
71 | 71 | |
|
72 | 72 | def __call__(self, tooltip_title, trim_at=50): |
|
73 | 73 | """ |
|
74 | 74 | Special function just to wrap our text into nice formatted autowrapped |
|
75 | 75 | text |
|
76 | 76 | @param tooltip_title: |
|
77 | 77 | """ |
|
78 | 78 | |
|
79 | 79 | return literal(wrap_paragraphs(tooltip_title, trim_at)\ |
|
80 | 80 | .replace('\n', '<br/>')) |
|
81 | 81 | |
|
82 | 82 | def activate(self): |
|
83 | 83 | """ |
|
84 | 84 | Adds tooltip mechanism to the given Html all tooltips have to have |
|
85 | 85 | set class tooltip and set attribute tooltip_title. |
|
86 | 86 | Then a tooltip will be generated based on that |
|
87 | 87 | All with yui js tooltip |
|
88 | 88 | """ |
|
89 | 89 | |
|
90 | 90 | js = ''' |
|
91 | 91 | YAHOO.util.Event.onDOMReady(function(){ |
|
92 | 92 | function toolTipsId(){ |
|
93 | 93 | var ids = []; |
|
94 | 94 | var tts = YAHOO.util.Dom.getElementsByClassName('tooltip'); |
|
95 | 95 | |
|
96 | 96 | for (var i = 0; i < tts.length; i++) { |
|
97 | 97 | //if element doesn not have and id autgenerate one for tooltip |
|
98 | 98 | |
|
99 | 99 | if (!tts[i].id){ |
|
100 | 100 | tts[i].id='tt'+i*100; |
|
101 | 101 | } |
|
102 | 102 | ids.push(tts[i].id); |
|
103 | 103 | } |
|
104 | 104 | return ids |
|
105 | 105 | }; |
|
106 | 106 | var myToolTips = new YAHOO.widget.Tooltip("tooltip", { |
|
107 | 107 | context: toolTipsId(), |
|
108 | 108 | monitorresize:false, |
|
109 | 109 | xyoffset :[0,0], |
|
110 | 110 | autodismissdelay:300000, |
|
111 | 111 | hidedelay:5, |
|
112 | 112 | showdelay:20, |
|
113 | 113 | }); |
|
114 | 114 | |
|
115 | 115 | //Mouse Over event disabled for new repositories since they dont |
|
116 | 116 | //have last commit message |
|
117 | 117 | myToolTips.contextMouseOverEvent.subscribe( |
|
118 | 118 | function(type, args) { |
|
119 | 119 | var context = args[0]; |
|
120 | 120 | var txt = context.getAttribute('tooltip_title'); |
|
121 | 121 | if(txt){ |
|
122 | this.cfg.config.x.value = 0; | |
|
123 | this.cfg.config.y.value = 0; | |
|
124 | ||
|
122 | 125 | return true; |
|
123 | 126 | } |
|
124 | 127 | else{ |
|
125 | 128 | return false; |
|
126 | 129 | } |
|
127 | 130 | }); |
|
128 | 131 | |
|
132 | ||
|
129 | 133 | // Set the text for the tooltip just before we display it. Lazy method |
|
130 | 134 | myToolTips.contextTriggerEvent.subscribe( |
|
131 | 135 | function(type, args) { |
|
132 | 136 | var context = args[0]; |
|
133 | 137 | var txt = context.getAttribute('tooltip_title'); |
|
134 |
this.cfg.setProperty("text", txt); |
|
|
138 | this.cfg.setProperty("text", txt); | |
|
139 | //autocenter | |
|
140 | var w = this.element.clientWidth; | |
|
141 | var h = this.element.clientHeight; | |
|
142 | var cur_x = this.pageX - (w / 2); | |
|
143 | var cur_y = this.pageY - h - 10; | |
|
144 | ||
|
145 | this.cfg.setProperty("xy",[cur_x,cur_y]); | |
|
146 | ||
|
135 | 147 | }); |
|
148 | //Mouse out | |
|
149 | myToolTips.contextMouseOutEvent.subscribe( | |
|
150 | function(type, args) { | |
|
151 | var context = args[0]; | |
|
152 | ||
|
153 | //console.log(this.cfg.config.x.value); | |
|
154 | //console.log(this.cfg.config.y.value); | |
|
155 | //console.log(this.cfg.config.xy.value); | |
|
156 | //console.log(this.cfg.config); | |
|
157 | //this.cfg.config.xy = [0,0]; | |
|
158 | //this.cfg.config.xyoffset = [0,0]; | |
|
159 | ||
|
160 | ||
|
161 | ||
|
162 | }); | |
|
163 | ||
|
136 | 164 | }); |
|
137 | 165 | ''' |
|
138 | 166 | return literal(js) |
|
139 | 167 | |
|
140 | 168 | tooltip = _ToolTip() |
|
141 | 169 | |
|
142 | 170 | class _FilesBreadCrumbs(object): |
|
143 | 171 | |
|
144 | 172 | def __call__(self, repo_name, rev, paths): |
|
145 | 173 | url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, revision=rev, f_path=''))] |
|
146 | 174 | paths_l = paths.split(' / ') |
|
147 | 175 | |
|
148 | 176 | for cnt, p in enumerate(paths_l, 1): |
|
149 | 177 | if p != '': |
|
150 | 178 | url_l.append(link_to(p, url('files_home', repo_name=repo_name, revision=rev, f_path=' / '.join(paths_l[:cnt])))) |
|
151 | 179 | |
|
152 | 180 | return literal(' / '.join(url_l)) |
|
153 | 181 | |
|
154 | 182 | files_breadcrumbs = _FilesBreadCrumbs() |
|
155 | 183 | |
|
156 | 184 | def pygmentize(filenode, **kwargs): |
|
157 | 185 | """ |
|
158 | 186 | pygmentize function using pygments |
|
159 | 187 | @param filenode: |
|
160 | 188 | """ |
|
161 | 189 | return literal(code_highlight(filenode.content, filenode.lexer, HtmlFormatter(**kwargs))) |
|
162 | 190 | |
|
163 | 191 | def pygmentize_annotation(filenode, **kwargs): |
|
164 | 192 | """ |
|
165 | 193 | pygmentize function for annotation |
|
166 | 194 | @param filenode: |
|
167 | 195 | """ |
|
168 | 196 | |
|
169 | 197 | color_dict = g.changeset_annotation_colors |
|
170 | 198 | def gen_color(): |
|
171 | 199 | import random |
|
172 | 200 | return [str(random.randrange(10, 235)) for _ in xrange(3)] |
|
173 | 201 | def get_color_string(cs): |
|
174 | 202 | if color_dict.has_key(cs): |
|
175 | 203 | col = color_dict[cs] |
|
176 | 204 | else: |
|
177 | 205 | color_dict[cs] = gen_color() |
|
178 | 206 | col = color_dict[cs] |
|
179 | 207 | return "color: rgb(%s) ! important;" % (', '.join(col)) |
|
180 | 208 | |
|
181 | 209 | def url_func(changeset): |
|
182 | 210 | tooltip_html = "<div style='font-size:0.8em'><b>Author:</b> %s<br/><b>Date:</b> %s</b><br/><b>Message:</b> %s<br/></div>" |
|
183 | 211 | |
|
184 | 212 | tooltip_html = tooltip_html % (changeset.author, |
|
185 | 213 | changeset.date, |
|
186 | 214 | tooltip(changeset.message)) |
|
187 | 215 | lnk_format = 'r%s:%s' % (changeset.revision, |
|
188 | 216 | changeset.raw_id) |
|
189 | 217 | uri = link_to( |
|
190 | 218 | lnk_format, |
|
191 | 219 | url('changeset_home', repo_name='test', |
|
192 | 220 | revision=changeset.raw_id), |
|
193 | 221 | style=get_color_string(changeset.raw_id), |
|
194 | 222 | class_='tooltip', |
|
195 | 223 | tooltip_title=tooltip_html |
|
196 | 224 | ) |
|
197 | 225 | |
|
198 | 226 | uri += '\n' |
|
199 | 227 | return uri |
|
200 | 228 | return literal(annotate_highlight(filenode, url_func, **kwargs)) |
|
201 | 229 | |
|
202 | 230 | def repo_name_slug(value): |
|
203 | 231 | """ |
|
204 | 232 | Return slug of name of repository |
|
205 | 233 | """ |
|
206 | 234 | slug = urlify(value) |
|
207 | 235 | for c in """=[]\;'"<>,/~!@#$%^&*()+{}|:""": |
|
208 | 236 | slug = slug.replace(c, '-') |
|
209 | 237 | slug = recursive_replace(slug, '-') |
|
210 | 238 | return slug |
|
211 | 239 | |
|
212 | 240 | flash = _Flash() |
|
213 | 241 | |
|
214 | 242 | |
|
215 | 243 | #=============================================================================== |
|
216 | 244 | # MERCURIAL FILTERS available via h. |
|
217 | 245 | #=============================================================================== |
|
218 | 246 | |
|
219 | 247 | |
|
220 | 248 | from mercurial import util |
|
221 | 249 | from mercurial.templatefilters import age as _age, person as _person |
|
222 | 250 | |
|
223 | 251 | age = lambda x:_age(x) |
|
224 | 252 | capitalize = lambda x: x.capitalize() |
|
225 | 253 | date = lambda x: util.datestr(x) |
|
226 | 254 | email = util.email |
|
227 | 255 | person = lambda x: _person(x) |
|
228 | 256 | hgdate = lambda x: "%d %d" % x |
|
229 | 257 | isodate = lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2') |
|
230 | 258 | isodatesec = lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2') |
|
231 | 259 | localdate = lambda x: (x[0], util.makedate()[1]) |
|
232 | 260 | rfc822date = lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2") |
|
233 | 261 | rfc3339date = lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2") |
|
234 | 262 | time_ago = lambda x: util.datestr(_age(x), "%a, %d %b %Y %H:%M:%S %1%2") |
|
235 | 263 | |
|
236 | 264 | |
|
237 | 265 | |
|
238 | 266 |
General Comments 0
You need to be logged in to leave comments.
Login now