Skip to content
Open
Show file tree
Hide file tree
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions src/syntax_highlighting/libs/pygments/formatters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _get_ttype_class(ttype):


CSSFILE_TEMPLATE = '''\
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
td.linenos { background-color: #; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 125%%; }
%(styledefs)s
Expand Down Expand Up @@ -609,7 +609,7 @@ def _wrap_tablelinenos(self, inner):
if nocls:
yield 0, ('<table class="%stable">' % self.cssclass +
'<tr><td><div class="linenodiv" '
'style="background-color: #f0f0f0; padding-right: 10px">'
'style="background-color: #; padding-right: 10px">'
'<pre style="line-height: 125%">' +
ls + '</pre></div></td><td class="code">')
else:
Expand Down
25 changes: 11 additions & 14 deletions src/syntax_highlighting/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
import os
import sys
import re
import json

from .consts import * # import addon_path
# always use shipped pygments library
# FIXME: properly vendorize pygments, lest we interfere with
# other add-ons that might be shipping their own pygments
sys.path.insert(0, os.path.join(addon_path, "libs"))

from pygments import highlight
Expand All @@ -34,6 +31,7 @@
from aqt import mw
from aqt.editor import Editor
from aqt.utils import showWarning
from anki.utils import json
from anki.hooks import addHook, wrap

from .config import local_conf
Expand Down Expand Up @@ -406,33 +404,32 @@ def highlight_code(ed):
# TODO: understand why this is neccessary
else:
if centerfragments:
pretty_code = "".join(["<center>",
pretty_code = "".join(["<center><table><tbody><tr><td>",
highlight(code, my_lexer, my_formatter),
"</center><br>"])
"</td></tr></tbody></table></center><br>"])
else:
pretty_code = "".join([highlight(code, my_lexer, my_formatter),
"<br>"])
pretty_code = "".join(["<table><tbody><tr><td>",
highlight(code, my_lexer, my_formatter),
"</td></tr></tbody></table><br>"])

pretty_code = process_html(pretty_code)

# 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):
"""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)

# protect the {{c1::}} clone field
html = re.sub(r"\{\{c\d+::", r"\{\{c\d+::", html)
html = re.sub(r"}}", "}}", html)

return html

# Hooks and monkey-patches


if anki21:
addHook("setupEditorButtons", onSetupButtons21)
Editor.onBridgeCmd = wrap(Editor.onBridgeCmd, onBridgeCmd, "around")
Expand Down
1 change: 1 addition & 0 deletions src/syntax_highlighting/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": "Syntax Highlighting for Code", "mod": 1538875907, "min_point_version": 0, "max_point_version": 0, "branch_index": 0, "disabled": false, "config": {"hotkey": "Alt+s", "limitToLangs": [], "style": "monokai"}}