Use heuristics to decide what to colorize

In practise pygments seems to have a very hard time processing large files and
files with long lines, so try to avoid using it in those cases.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
This commit is contained in:
Alberto Bertogli 2012-11-14 00:48:46 +00:00
parent ba3b2132f5
commit 62da3ebc08
4 changed files with 25 additions and 5 deletions

@ -158,7 +158,7 @@ def with_utils(f):
""" """
utilities = { utilities = {
'shorten': utils.shorten, 'shorten': utils.shorten,
'has_colorizer': utils.has_colorizer, 'can_colorize': utils.can_colorize,
'colorize_diff': utils.colorize_diff, 'colorize_diff': utils.colorize_diff,
'colorize_blob': utils.colorize_blob, 'colorize_blob': utils.colorize_blob,
'abort': bottle.abort, 'abort': bottle.abort,

@ -18,8 +18,28 @@ def shorten(s, width = 60):
return s return s
return s[:57] + "..." return s[:57] + "..."
def has_colorizer(): def can_colorize(s):
return pygments is not None """True if we can colorize the string, False otherwise."""
if pygments is None:
return False
# Pygments can take a huge amount of time with long files, or with very
# long lines; these are heuristics to try to avoid those situations.
if len(s) > (512 * 1024):
return False
# If any of the first 5 lines is over 300 characters long, don't colorize.
start = 0
for i in range(5):
pos = s.find('\n', start)
if pos == -1:
break
if pos - start > 300:
return False
start = pos + 1
return True
def colorize_diff(s): def colorize_diff(s):
lexer = lexers.DiffLexer(encoding = 'utf-8') lexer = lexers.DiffLexer(encoding = 'utf-8')

@ -36,7 +36,7 @@
<a href="">{{!fname.html}}</a> <a href="">{{!fname.html}}</a>
</h3> </h3>
% if has_colorizer(): % if can_colorize(blob):
{{!colorize_blob(fname.unicode, blob)}} {{!colorize_blob(fname.unicode, blob)}}
% else: % else:
<pre class="blob-body"> <pre class="blob-body">

@ -55,7 +55,7 @@
<hr/> <hr/>
% if has_colorizer(): % if can_colorize(c.diff.body):
{{!colorize_diff(c.diff.body)}} {{!colorize_diff(c.diff.body)}}
% else: % else:
<pre class="diff-body"> <pre class="diff-body">