cubicweb/misc/source_highlight.py
author Laurent Peuch <cortex@worlddomination.be>
Thu, 24 Oct 2019 06:34:42 +0200
changeset 12757 e55f6f6a8d28
parent 12739 c6f8ca03718f
child 12758 db95a417a5ec
permissions -rw-r--r--
[debug/source_highlight] add highlight_html and generate_css for debugtool panels
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12739
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     1
"""This module provide syntaxe highlight functions"""
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     2
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     3
from logilab.common.logging_ext import _colorable_terminal
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     4
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     5
try:
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     6
    from pygments import highlight as pygments_highlight
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     7
    from pygments.lexers import get_lexer_by_name
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     8
    from pygments.formatters.terminal import TerminalFormatter
12757
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
     9
    from pygments.formatters.html import HtmlFormatter
12739
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    10
    has_pygments = True
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    11
except ImportError:
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    12
    has_pygments = False
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    13
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    14
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    15
def highlight(code, language):
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    16
    if not has_pygments:
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    17
        return code
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    18
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    19
    if not _colorable_terminal():
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    20
        return code
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    21
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    22
    return pygments_highlight(code, get_lexer_by_name(language), TerminalFormatter())
12757
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    23
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    24
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    25
def highlight_html(code, language, linenos=False):
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    26
    if not has_pygments:
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    27
        return code
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    28
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    29
    return pygments_highlight(str(code), get_lexer_by_name(language), HtmlFormatter(wrapcode=True, linenos=linenos))
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    30
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    31
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    32
def generate_css():
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    33
    if has_pygments:
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    34
        return HtmlFormatter().get_style_defs()
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    35
    else:
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    36
        return ""