~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/highlight.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-30 10:27:53 UTC
  • mto: (389.2.2 pep8-2009-10)
  • mto: This revision was merged to the branch mainline in revision 392.
  • Revision ID: mnordhoff@mattnordhoff.com-20090430102753-k0fe9v60gw0at3py
Whitespace tweak to profile.py

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 cgi
20
 
 
21
19
from pygments import highlight as _highlight_func
22
20
from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
23
21
from pygments.formatters import HtmlFormatter
25
23
 
26
24
DEFAULT_PYGMENT_STYLE = 'colorful'
27
25
 
28
 
# Trying to highlight very large files using pygments was killing
29
 
# loggerhead on launchpad.net, because pygments isn't very fast.
30
 
# So we only highlight files if they're 512K or smaller.
31
 
MAX_HIGHLIGHT_SIZE = 512000;
32
26
 
33
 
def highlight(path, text, encoding, style=DEFAULT_PYGMENT_STYLE):
 
27
def highlight(path, text, style=DEFAULT_PYGMENT_STYLE):
34
28
    """
35
29
    Returns a list of highlighted (i.e. HTML formatted) strings.
36
30
    """
37
31
 
38
 
    if len(text) > MAX_HIGHLIGHT_SIZE:
39
 
        return map(cgi.escape, text.split('\n'))
40
 
 
41
32
    formatter = HtmlFormatter(style=style, nowrap=True, classprefix='pyg-')
42
33
 
 
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
 
43
41
    try:
44
42
        lexer = guess_lexer_for_filename(path, text[:1024], encoding=encoding)
45
43
    except (ClassNotFound, ValueError):