author | Arthur Lutz <arthur.lutz@logilab.fr> |
Sun, 02 Feb 2020 22:24:59 +0100 | |
branch | 3.27 |
changeset 12859 | 5dacd3ac9584 |
parent 12770 | be0864a2eec8 |
permissions | -rw-r--r-- |
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 |
|
12758
db95a417a5ec
[debug/source_highlight] rename highlight to highlight_terminal
Laurent Peuch <cortex@worlddomination.be>
parents:
12757
diff
changeset
|
15 |
def highlight_terminal(code, language): |
12739
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 |
|
12770
be0864a2eec8
[mod] allow to pass generic additional arguments to pygments HtmlFormatter
Laurent Peuch <cortex@worlddomination.be>
parents:
12769
diff
changeset
|
25 |
def highlight_html(code, language, linenos=False, linenostart=1, **kwargs): |
12757
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: |
12769
a61e0fe17a69
[debug/fix] ensure that not syntax highlighted code is a string
Laurent Peuch <cortex@worlddomination.be>
parents:
12758
diff
changeset
|
27 |
return str(code) |
12757
e55f6f6a8d28
[debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents:
12739
diff
changeset
|
28 |
|
12770
be0864a2eec8
[mod] allow to pass generic additional arguments to pygments HtmlFormatter
Laurent Peuch <cortex@worlddomination.be>
parents:
12769
diff
changeset
|
29 |
return pygments_highlight(str(code), |
be0864a2eec8
[mod] allow to pass generic additional arguments to pygments HtmlFormatter
Laurent Peuch <cortex@worlddomination.be>
parents:
12769
diff
changeset
|
30 |
get_lexer_by_name(language), |
be0864a2eec8
[mod] allow to pass generic additional arguments to pygments HtmlFormatter
Laurent Peuch <cortex@worlddomination.be>
parents:
12769
diff
changeset
|
31 |
HtmlFormatter(wrapcode=True, linenos=linenos, linenostart=linenostart, **kwargs)) |
12757
e55f6f6a8d28
[debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents:
12739
diff
changeset
|
32 |
|
e55f6f6a8d28
[debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents:
12739
diff
changeset
|
33 |
|
e55f6f6a8d28
[debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents:
12739
diff
changeset
|
34 |
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
|
35 |
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
|
36 |
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
|
37 |
else: |
e55f6f6a8d28
[debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents:
12739
diff
changeset
|
38 |
return "" |