~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/highlight.py

  • Committer: Tres Seaver
  • Date: 2010-03-23 15:44:51 UTC
  • mto: This revision was merged to the branch mainline in revision 405.
  • Revision ID: tseaver@agendaless.com-20100323154451-enjul7pa2crkk18b
Add Sphinx-based documentation.

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