equal
deleted
inserted
replaced
3 # Copyright 2005-2008 Olivia Mackall <olivia@selenic.com> |
3 # Copyright 2005-2008 Olivia Mackall <olivia@selenic.com> |
4 # |
4 # |
5 # This software may be used and distributed according to the terms of the |
5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. |
6 # GNU General Public License version 2 or any later version. |
7 |
7 |
8 from __future__ import absolute_import |
|
9 |
8 |
10 import os |
9 import os |
11 import re |
10 import re |
12 import time |
11 import time |
13 |
12 |
139 a = b[: len(a)] |
138 a = b[: len(a)] |
140 elif len(b) > len(a): |
139 elif len(b) > len(a): |
141 b = b[: len(a)] |
140 b = b[: len(a)] |
142 if a == b: |
141 if a == b: |
143 return a |
142 return a |
144 for i in pycompat.xrange(len(a)): |
143 for i in range(len(a)): |
145 if a[i] != b[i]: |
144 if a[i] != b[i]: |
146 return a[:i] |
145 return a[:i] |
147 return a |
146 return a |
148 |
147 |
149 try: |
148 try: |
267 |
266 |
268 |
267 |
269 @templatefilter(b'firstline', intype=bytes) |
268 @templatefilter(b'firstline', intype=bytes) |
270 def firstline(text): |
269 def firstline(text): |
271 """Any text. Returns the first line of text.""" |
270 """Any text. Returns the first line of text.""" |
272 try: |
271 return stringutil.firstline(text) |
273 return text.splitlines(True)[0].rstrip(b'\r\n') |
|
274 except IndexError: |
|
275 return b'' |
|
276 |
272 |
277 |
273 |
278 @templatefilter(b'hex', intype=bytes) |
274 @templatefilter(b'hex', intype=bytes) |
279 def hexfilter(text): |
275 def hexfilter(text): |
280 """Any text. Convert a binary Mercurial node identifier into |
276 """Any text. Convert a binary Mercurial node identifier into |
313 lines = text.splitlines() |
309 lines = text.splitlines() |
314 num_lines = len(lines) |
310 num_lines = len(lines) |
315 endswithnewline = text[-1:] == b'\n' |
311 endswithnewline = text[-1:] == b'\n' |
316 |
312 |
317 def indenter(): |
313 def indenter(): |
318 for i in pycompat.xrange(num_lines): |
314 for i in range(num_lines): |
319 l = lines[i] |
315 l = lines[i] |
320 if l.strip(): |
316 if l.strip(): |
321 yield prefix if i else firstline |
317 yield prefix if i else firstline |
322 yield l |
318 yield l |
323 if i < num_lines - 1 or endswithnewline: |
319 if i < num_lines - 1 or endswithnewline: |
333 return b'null' |
329 return b'null' |
334 elif obj is False: |
330 elif obj is False: |
335 return b'false' |
331 return b'false' |
336 elif obj is True: |
332 elif obj is True: |
337 return b'true' |
333 return b'true' |
338 elif isinstance(obj, (int, pycompat.long, float)): |
334 elif isinstance(obj, (int, int, float)): |
339 return pycompat.bytestr(obj) |
335 return pycompat.bytestr(obj) |
340 elif isinstance(obj, bytes): |
336 elif isinstance(obj, bytes): |
341 return b'"%s"' % encoding.jsonescape(obj, paranoid=paranoid) |
337 return b'"%s"' % encoding.jsonescape(obj, paranoid=paranoid) |
342 elif isinstance(obj, type(u'')): |
338 elif isinstance(obj, type(u'')): |
343 raise error.ProgrammingError( |
339 raise error.ProgrammingError( |
345 ) |
341 ) |
346 elif util.safehasattr(obj, b'keys'): |
342 elif util.safehasattr(obj, b'keys'): |
347 out = [ |
343 out = [ |
348 b'"%s": %s' |
344 b'"%s": %s' |
349 % (encoding.jsonescape(k, paranoid=paranoid), json(v, paranoid)) |
345 % (encoding.jsonescape(k, paranoid=paranoid), json(v, paranoid)) |
350 for k, v in sorted(pycompat.iteritems(obj)) |
346 for k, v in sorted(obj.items()) |
351 ] |
347 ] |
352 return b'{' + b', '.join(out) + b'}' |
348 return b'{' + b', '.join(out) + b'}' |
353 elif util.safehasattr(obj, b'__iter__'): |
349 elif util.safehasattr(obj, b'__iter__'): |
354 out = [json(i, paranoid) for i in obj] |
350 out = [json(i, paranoid) for i in obj] |
355 return b'[' + b', '.join(out) + b']' |
351 return b'[' + b', '.join(out) + b']' |
371 @templatefilter(b'obfuscate', intype=bytes) |
367 @templatefilter(b'obfuscate', intype=bytes) |
372 def obfuscate(text): |
368 def obfuscate(text): |
373 """Any text. Returns the input text rendered as a sequence of |
369 """Any text. Returns the input text rendered as a sequence of |
374 XML entities. |
370 XML entities. |
375 """ |
371 """ |
376 text = pycompat.unicode( |
372 text = str(text, pycompat.sysstr(encoding.encoding), r'replace') |
377 text, pycompat.sysstr(encoding.encoding), r'replace' |
|
378 ) |
|
379 return b''.join([b'&#%d;' % ord(c) for c in text]) |
373 return b''.join([b'&#%d;' % ord(c) for c in text]) |
380 |
374 |
381 |
375 |
382 @templatefilter(b'permissions', intype=bytes) |
376 @templatefilter(b'permissions', intype=bytes) |
383 def permissions(flags): |
377 def permissions(flags): |
547 return text |
541 return text |
548 |
542 |
549 |
543 |
550 def loadfilter(ui, extname, registrarobj): |
544 def loadfilter(ui, extname, registrarobj): |
551 """Load template filter from specified registrarobj""" |
545 """Load template filter from specified registrarobj""" |
552 for name, func in pycompat.iteritems(registrarobj._table): |
546 for name, func in registrarobj._table.items(): |
553 filters[name] = func |
547 filters[name] = func |
554 |
548 |
555 |
549 |
556 # tell hggettext to extract docstrings from these functions: |
550 # tell hggettext to extract docstrings from these functions: |
557 i18nfunctions = filters.values() |
551 i18nfunctions = filters.values() |