cubicweb/misc/source_highlight.py
author Laurent Peuch <cortex@worlddomination.be>
Thu, 23 May 2019 00:33:04 +0200
changeset 12746 5c432a7fc442
parent 12739 c6f8ca03718f
child 12757 e55f6f6a8d28
permissions -rw-r--r--
[migration/pdb] display traceback instead of only the exception for easier debugging Closes #17219820

"""This module provide syntaxe highlight functions"""

from logilab.common.logging_ext import _colorable_terminal

try:
    from pygments import highlight as pygments_highlight
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters.terminal import TerminalFormatter
    has_pygments = True
except ImportError:
    has_pygments = False


def highlight(code, language):
    if not has_pygments:
        return code

    if not _colorable_terminal():
        return code

    return pygments_highlight(code, get_lexer_by_name(language), TerminalFormatter())