cubicweb/misc/source_highlight.py
changeset 12739 c6f8ca03718f
child 12757 e55f6f6a8d28
equal deleted inserted replaced
12738:a54037a68b14 12739:c6f8ca03718f
       
     1 """This module provide syntaxe highlight functions"""
       
     2 
       
     3 from logilab.common.logging_ext import _colorable_terminal
       
     4 
       
     5 try:
       
     6     from pygments import highlight as pygments_highlight
       
     7     from pygments.lexers import get_lexer_by_name
       
     8     from pygments.formatters.terminal import TerminalFormatter
       
     9     has_pygments = True
       
    10 except ImportError:
       
    11     has_pygments = False
       
    12 
       
    13 
       
    14 def highlight(code, language):
       
    15     if not has_pygments:
       
    16         return code
       
    17 
       
    18     if not _colorable_terminal():
       
    19         return code
       
    20 
       
    21     return pygments_highlight(code, get_lexer_by_name(language), TerminalFormatter())