From 27fb924aad7e33174dacd506de49ac6750b1020a Mon Sep 17 00:00:00 2001 From: Ali Gottschall Date: Mon, 30 Mar 2020 09:03:39 +0200 Subject: [PATCH] remove unnecessary HTML tags if linenumbering is not requred --- src/syntax_highlighting/main.py | 38 ++++++++++++++------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/syntax_highlighting/main.py b/src/syntax_highlighting/main.py index b6da9f6..4e40b05 100644 --- a/src/syntax_highlighting/main.py +++ b/src/syntax_highlighting/main.py @@ -252,12 +252,12 @@ def add_plugin_button_(self, def add_code_langs_combobox(self, func, previous_lang): combo = QComboBox() combo.addItem(previous_lang) - + if LIMITED_LANGS: selection = LIMITED_LANGS else: selection = sorted(LANGUAGES_MAP.keys(), key=string.lower) - + for lang in selection: combo.addItem(lang) @@ -395,32 +395,18 @@ def highlight_code(ed): showError(ERR_STYLE, parent=ed.parentWindow) return False - if linenos: - if centerfragments: - pretty_code = "".join(["
", - highlight(code, my_lexer, my_formatter), - "

"]) - else: - pretty_code = "".join([highlight(code, my_lexer, my_formatter), - "
"]) - # TODO: understand why this is neccessary - else: - if centerfragments: - pretty_code = "".join(["
", - highlight(code, my_lexer, my_formatter), - "

"]) - else: - pretty_code = "".join([highlight(code, my_lexer, my_formatter), - "
"]) - - pretty_code = process_html(pretty_code) + pretty_code = highlight(code, my_lexer, my_formatter) + pretty_code = process_html(pretty_code, linenos) + if centerfragments: + pretty_code = "".join(["
", pretty_code, "

"]) + pretty_code = process_html(pretty_code, linenos) # These two lines insert a piece of HTML in the current cursor position ed.web.eval("document.execCommand('inserthtml', false, %s);" % json.dumps(pretty_code)) -def process_html(html): +def process_html(html, linenos=True): """Modify highlighter output to address some Anki idiosyncracies""" # 1.) "Escape" curly bracket sequences reserved to Anki's card template # system by placing an invisible html tag inbetween @@ -428,6 +414,14 @@ def process_html(html): (r"}}", r"}}"), (r"::", r"::")): html = re.sub(pattern, replacement, html) + # remove unnecessary HTML tags if no linenos are needed + if not linenos: + tostrip="
" + if html.startswith(tostrip): + html = html[len(tostrip):] + tostrip="
" + if html.endswith(tostrip): + html = html[:-len(tostrip)] return html # Hooks and monkey-patches