~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/highlight.py

  • Committer: Danilo Segan
  • Date: 2011-08-17 12:53:09 UTC
  • mfrom: (454.1.1 bug-718566)
  • Revision ID: danilo@canonical.com-20110817125309-2r1fgcrqt5kr5h43
Remove FileChangeCache to resolve bug #718566 [r=jam].

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
#
18
18
 
 
19
import bzrlib.osutils
 
20
import cgi
 
21
 
19
22
from pygments import highlight as _highlight_func
20
23
from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
21
24
from pygments.formatters import HtmlFormatter
23
26
 
24
27
DEFAULT_PYGMENT_STYLE = 'colorful'
25
28
 
 
29
# Trying to highlight very large files using pygments was killing
 
30
# loggerhead on launchpad.net, because pygments isn't very fast.
 
31
# So we only highlight files if they're 512K or smaller.
 
32
MAX_HIGHLIGHT_SIZE = 512000;
26
33
 
27
 
def highlight(path, text, style=DEFAULT_PYGMENT_STYLE):
 
34
def highlight(path, text, encoding, style=DEFAULT_PYGMENT_STYLE):
28
35
    """
29
36
    Returns a list of highlighted (i.e. HTML formatted) strings.
30
37
    """
31
38
 
 
39
    if len(text) > MAX_HIGHLIGHT_SIZE:
 
40
        return map(cgi.escape,  bzrlib.osutils.split_lines(text))
 
41
 
32
42
    formatter = HtmlFormatter(style=style, nowrap=True, classprefix='pyg-')
33
43
 
34
 
    encoding = 'utf-8'
35
 
    try:
36
 
        text = text.decode(encoding)
37
 
    except UnicodeDecodeError:
38
 
        encoding = 'iso-8859-15'
39
 
        text = text.decode(encoding)
40
 
 
41
44
    try:
42
45
        lexer = guess_lexer_for_filename(path, text[:1024], encoding=encoding)
43
46
    except (ClassNotFound, ValueError):
46
49
        except (ClassNotFound, ValueError):
47
50
            lexer = TextLexer(encoding=encoding)
48
51
 
49
 
    hl_lines = _highlight_func(text, lexer, formatter).split('\n')
 
52
    hl_lines = _highlight_func(text, lexer, formatter)
 
53
    hl_lines = bzrlib.osutils.split_lines(hl_lines)
50
54
 
51
55
    return hl_lines