# HG changeset patch # User Laurent Peuch # Date 1571891682 -7200 # Node ID e55f6f6a8d28a732a5ff68b99f99adc67675ea10 # Parent d91c229de97f8eb7a377580b9581d2d25528334b [debug/source_highlight] add highlight_html and generate_css for debugtool panels diff -r d91c229de97f -r e55f6f6a8d28 cubicweb/misc/source_highlight.py --- a/cubicweb/misc/source_highlight.py Thu Aug 01 05:42:45 2019 +0200 +++ b/cubicweb/misc/source_highlight.py Thu Oct 24 06:34:42 2019 +0200 @@ -6,6 +6,7 @@ from pygments import highlight as pygments_highlight from pygments.lexers import get_lexer_by_name from pygments.formatters.terminal import TerminalFormatter + from pygments.formatters.html import HtmlFormatter has_pygments = True except ImportError: has_pygments = False @@ -19,3 +20,17 @@ return code return pygments_highlight(code, get_lexer_by_name(language), TerminalFormatter()) + + +def highlight_html(code, language, linenos=False): + if not has_pygments: + return code + + return pygments_highlight(str(code), get_lexer_by_name(language), HtmlFormatter(wrapcode=True, linenos=linenos)) + + +def generate_css(): + if has_pygments: + return HtmlFormatter().get_style_defs() + else: + return ""