~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/highlight.py

  • Committer: Jelmer Vernooij
  • Date: 2011-03-25 13:09:10 UTC
  • mfrom: (442.2.1 trunk)
  • Revision ID: jelmer@samba.org-20110325130910-awpn311q8f6yxvf4
Merge fix for foreign revision info displaying.

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
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
44
    try:
39
49
        except (ClassNotFound, ValueError):
40
50
            lexer = TextLexer(encoding=encoding)
41
51
 
42
 
    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)
43
54
 
44
55
    return hl_lines