From 38482d4846f1b88dc04b768288232d43e75c540c Mon Sep 17 00:00:00 2001 From: mdipierro Date: Tue, 1 May 2018 11:06:36 -0500 Subject: [PATCH] better highlight.py, thanks Paolo --- gluon/highlight.py | 64 ++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/gluon/highlight.py b/gluon/highlight.py index 951dab81..b8cae776 100644 --- a/gluon/highlight.py +++ b/gluon/highlight.py @@ -27,7 +27,7 @@ class Highlighter(object): ): """ Initialize highlighter: - mode = language (PYTHON, WEB2PY,C, CPP, HTML, HTML_PLAIN) + mode = language (PYTHON, WEB2PY, C, CPP, HTML, HTML_PLAIN) """ styles = styles or {} mode = mode.upper() @@ -260,33 +260,27 @@ def highlight( ): styles = styles or {} attributes = attributes or {} - if 'CODE' not in styles: - code_style = """ - font-size: 11px; - font-family: Bitstream Vera Sans Mono,monospace; - background-color: transparent; - margin: 0; - padding: 5px; - border: none; - overflow: auto; - white-space: pre !important;\n""" - else: - code_style = styles['CODE'] - if 'LINENUMBERS' not in styles: - linenumbers_style = """ - font-size: 11px; - font-family: Bitstream Vera Sans Mono,monospace; - background-color: transparent; - margin: 0; - padding: 5px; - border: none; - color: #A0A0A0;\n""" - else: - linenumbers_style = styles['LINENUMBERS'] - if 'LINEHIGHLIGHT' not in styles: - linehighlight_style = "background-color: #EBDDE2;" - else: - linehighlight_style = styles['LINEHIGHLIGHT'] + code_style = styles.get('CODE', None) or ''' +font-size: 11px; +font-family: Bitstream Vera Sans Mono,monospace; +background-color: transparent; +margin: 0; +padding: 5px; +border: none; +overflow: auto; +white-space: pre !important; +''' + linenumbers_style = styles.get('LINENUMBERS', None) or ''' +font-size: 11px; +font-family: Bitstream Vera Sans Mono,monospace; +background-color: transparent; +margin: 0; +padding: 5px; +border: none; +color: #A0A0A0; +''' + linehighlight_style = styles.get('LINEHIGHLIGHT', None) or \ + 'background-color: #EBDDE2;' if language and language.upper() in ['PYTHON', 'C', 'CPP', 'HTML', 'WEB2PY']: @@ -309,18 +303,20 @@ def highlight( else: lineno = highlight_line if lineno < len(lines): - lines[lineno] = '
%s
' % ( + lines[lineno] = '%s' % ( linehighlight_style, lines[lineno]) - linenumbers[lineno] = '
%s
' % ( + linenumbers[lineno] = '%s' % ( linehighlight_style, linenumbers[lineno]) if context_lines: if lineno + context_lines < len(lines): - del lines[lineno + context_lines:] - del linenumbers[lineno + context_lines:] + delslice = slice(lineno + context_lines + 1, len(lines)) + del lines[delslice] + del linenumbers[delslice] if lineno - context_lines > 0: - del lines[0:lineno - context_lines] - del linenumbers[0:lineno - context_lines] + delslice = slice(0, lineno - context_lines) + del lines[delslice] + del linenumbers[delslice] code = '
'.join(lines) numbers = '
'.join(linenumbers)