[debug/source_highlight] add highlight_html and generate_css for debugtool panels
authorLaurent Peuch <cortex@worlddomination.be>
Thu, 24 Oct 2019 06:34:42 +0200
changeset 12757 e55f6f6a8d28
parent 12756 d91c229de97f
child 12758 db95a417a5ec
[debug/source_highlight] add highlight_html and generate_css for debugtool panels
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 ""