~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/highlight.py

  • Committer: Ian Clatworthy
  • Date: 2010-04-23 00:02:29 UTC
  • mfrom: (407.1.2 loggerhead)
  • Revision ID: ian.clatworthy@canonical.com-20100423000229-ysxj7lzkx6oklt35
Skip syntax highlighting on files over 512K

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
 
19
21
from pygments import highlight as _highlight_func
20
22
from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
21
23
from pygments.formatters import HtmlFormatter
23
25
 
24
26
DEFAULT_PYGMENT_STYLE = 'colorful'
25
27
 
 
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;
26
32
 
27
33
def highlight(path, text, encoding, style=DEFAULT_PYGMENT_STYLE):
28
34
    """
29
35
    Returns a list of highlighted (i.e. HTML formatted) strings.
30
36
    """
31
37
 
 
38
    if len(text) > MAX_HIGHLIGHT_SIZE:
 
39
        return map(cgi.escape, text.split('\n'))
 
40
 
32
41
    formatter = HtmlFormatter(style=style, nowrap=True, classprefix='pyg-')
33
42
 
34
43
    try: