##// END OF EJS Templates
fixes #326 some html special chars where not escaped in diffs + code garden in helpers
marcink -
r1781:089c81cf beta
parent child Browse files
Show More
@@ -4,12 +4,12 b''
4 4 ~~~~~~~~~~~~~~~~~~~
5 5
6 6 Set of diffing helpers, previously part of vcs
7
8
7
8
9 9 :created_on: Dec 4, 2011
10 10 :author: marcink
11 11 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
12 :original copyright: 2007-2008 by Armin Ronacher
12 :original copyright: 2007-2008 by Armin Ronacher
13 13 :license: GPLv3, see COPYING for more details.
14 14 """
15 15 # This program is free software: you can redistribute it and/or modify
@@ -30,15 +30,15 b' import difflib'
30 30
31 31 from itertools import tee, imap
32 32
33 from mercurial.match import match
34
35 33 from vcs.exceptions import VCSError
36 34 from vcs.nodes import FileNode
35 import markupsafe
36
37 37
38 38 def get_gitdiff(filenode_old, filenode_new, ignore_whitespace=True, context=3):
39 39 """
40 40 Returns git style diff between given ``filenode_old`` and ``filenode_new``.
41
41
42 42 :param ignore_whitespace: ignore whitespaces in diff
43 43 """
44 44
@@ -95,7 +95,7 b' class DiffProcessor(object):'
95 95 self.differ = self._highlight_line_udiff
96 96
97 97 def escaper(self, string):
98 return string.replace('<', '&lt;').replace('>', '&gt;')
98 return markupsafe.escape(string)
99 99
100 100 def copy_iterator(self):
101 101 """
@@ -153,15 +153,15 b' class DiffProcessor(object):'
153 153
154 154 raise Exception('wrong size of diff %s' % size)
155 155
156 def _highlight_line_difflib(self, line, next):
156 def _highlight_line_difflib(self, line, next_):
157 157 """
158 158 Highlight inline changes in both lines.
159 159 """
160 160
161 161 if line['action'] == 'del':
162 old, new = line, next
162 old, new = line, next_
163 163 else:
164 old, new = next, line
164 old, new = next_, line
165 165
166 166 oldwords = re.split(r'(\W)', old['line'])
167 167 newwords = re.split(r'(\W)', new['line'])
@@ -183,17 +183,17 b' class DiffProcessor(object):'
183 183 old['line'] = "".join(oldfragments)
184 184 new['line'] = "".join(newfragments)
185 185
186 def _highlight_line_udiff(self, line, next):
186 def _highlight_line_udiff(self, line, next_):
187 187 """
188 188 Highlight inline changes in both lines.
189 189 """
190 190 start = 0
191 limit = min(len(line['line']), len(next['line']))
192 while start < limit and line['line'][start] == next['line'][start]:
191 limit = min(len(line['line']), len(next_['line']))
192 while start < limit and line['line'][start] == next_['line'][start]:
193 193 start += 1
194 194 end = -1
195 195 limit -= start
196 while -end <= limit and line['line'][end] == next['line'][end]:
196 while -end <= limit and line['line'][end] == next_['line'][end]:
197 197 end -= 1
198 198 end += 1
199 199 if start or end:
@@ -211,7 +211,7 b' class DiffProcessor(object):'
211 211 l['line'][last:]
212 212 )
213 213 do(line)
214 do(next)
214 do(next_)
215 215
216 216 def _parse_udiff(self):
217 217 """
@@ -302,7 +302,7 b' class DiffProcessor(object):'
302 302 pass
303 303
304 304 # highlight inline changes
305 for file in files:
305 for _ in files:
306 306 for chunk in chunks:
307 307 lineiter = iter(chunk)
308 308 #first = True
@@ -214,13 +214,16 b' def pygmentize(filenode, **kwargs):'
214 214 return literal(code_highlight(filenode.content,
215 215 filenode.lexer, CodeHtmlFormatter(**kwargs)))
216 216
217
217 218 def pygmentize_annotation(repo_name, filenode, **kwargs):
218 """pygmentize function for annotation
219 """
220 pygmentize function for annotation
219 221
220 222 :param filenode:
221 223 """
222 224
223 225 color_dict = {}
226
224 227 def gen_color(n=10000):
225 228 """generator for getting n of evenly distributed colors using
226 229 hsv color and golden ratio. It always return same order of colors
@@ -229,19 +232,26 b' def pygmentize_annotation(repo_name, fil'
229 232 """
230 233
231 234 def hsv_to_rgb(h, s, v):
232 if s == 0.0: return v, v, v
233 i = int(h * 6.0) # XXX assume int() truncates!
235 if s == 0.0:
236 return v, v, v
237 i = int(h * 6.0) # XXX assume int() truncates!
234 238 f = (h * 6.0) - i
235 239 p = v * (1.0 - s)
236 240 q = v * (1.0 - s * f)
237 241 t = v * (1.0 - s * (1.0 - f))
238 242 i = i % 6
239 if i == 0: return v, t, p
240 if i == 1: return q, v, p
241 if i == 2: return p, v, t
242 if i == 3: return p, q, v
243 if i == 4: return t, p, v
244 if i == 5: return v, p, q
243 if i == 0:
244 return v, t, p
245 if i == 1:
246 return q, v, p
247 if i == 2:
248 return p, v, t
249 if i == 3:
250 return p, q, v
251 if i == 4:
252 return t, p, v
253 if i == 5:
254 return v, p, q
245 255
246 256 golden_ratio = 0.618033988749895
247 257 h = 0.22717784590367374
@@ -251,12 +261,12 b' def pygmentize_annotation(repo_name, fil'
251 261 h %= 1
252 262 HSV_tuple = [h, 0.95, 0.95]
253 263 RGB_tuple = hsv_to_rgb(*HSV_tuple)
254 yield map(lambda x:str(int(x * 256)), RGB_tuple)
264 yield map(lambda x: str(int(x * 256)), RGB_tuple)
255 265
256 266 cgenerator = gen_color()
257 267
258 268 def get_color_string(cs):
259 if color_dict.has_key(cs):
269 if cs in color_dict:
260 270 col = color_dict[cs]
261 271 else:
262 272 col = color_dict[cs] = cgenerator.next()
@@ -291,6 +301,7 b' def pygmentize_annotation(repo_name, fil'
291 301
292 302 return literal(annotate_highlight(filenode, url_func(repo_name), **kwargs))
293 303
304
294 305 def is_following_repo(repo_name, user_id):
295 306 from rhodecode.model.scm import ScmModel
296 307 return ScmModel().is_following_repo(repo_name, user_id)
@@ -304,7 +315,7 b' from vcs.utils import author_name, autho'
304 315 from rhodecode.lib import credentials_filter, age as _age
305 316 from rhodecode.model.db import User
306 317
307 age = lambda x:_age(x)
318 age = lambda x: _age(x)
308 319 capitalize = lambda x: x.capitalize()
309 320 email = author_email
310 321 short_id = lambda x: x[:12]
@@ -325,10 +336,11 b' def email_or_none(author):'
325 336 # No valid email, not a valid user in the system, none!
326 337 return None
327 338
339
328 340 def person(author):
329 341 # attr to return from fetched user
330 342 person_getter = lambda usr: usr.username
331
343
332 344 # Valid email in the attribute passed, see if they're in the system
333 345 _email = email(author)
334 346 if _email != '':
General Comments 0
You need to be logged in to leave comments. Login now