Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unnecessary HTML tags if linenumbering is not requred #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions src/syntax_highlighting/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -395,39 +395,33 @@ def highlight_code(ed):
showError(ERR_STYLE, parent=ed.parentWindow)
return False

if linenos:
if centerfragments:
pretty_code = "".join(["<center>",
highlight(code, my_lexer, my_formatter),
"</center><br>"])
else:
pretty_code = "".join([highlight(code, my_lexer, my_formatter),
"<br>"])
# TODO: understand why this is neccessary
else:
if centerfragments:
pretty_code = "".join(["<center>",
highlight(code, my_lexer, my_formatter),
"</center><br>"])
else:
pretty_code = "".join([highlight(code, my_lexer, my_formatter),
"<br>"])

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(["<center>", pretty_code, "</center><br>"])
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
for pattern, replacement in ((r"{{", r"{<!---->{"),
(r"}}", r"}<!---->}"),
(r"::", r":<!---->:")):
html = re.sub(pattern, replacement, html)
# remove unnecessary HTML tags if no linenos are needed
if not linenos:
tostrip="<table><tbody><tr><td>"
if html.startswith(tostrip):
html = html[len(tostrip):]
tostrip="</td></tr></tbody></table>"
if html.endswith(tostrip):
html = html[:-len(tostrip)]
return html

# Hooks and monkey-patches
Expand Down