Skip to content

Commit

Permalink
Escape curly brackets that are otherwise reserved to Anki template sy…
Browse files Browse the repository at this point in the history
…ntax

Went with an HTML-comment solution as it's the only workaround
that does not require modifying the user's card templates.

cf.:

- https://anki.tenderapp.com/discussions/ankidesktop/15367-escape-field-tags-curly-brackets
- https://old.reddit.com/r/Anki/comments/9fk4ri/using_double_curly_brackets_inside_cards/#
- badlydrawnrob/anki#9
  • Loading branch information
glutanimate committed Oct 7, 2018
1 parent 21be91e commit cacf1b0
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/syntax_highlighting/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import os
import sys
import re

from .consts import * # import addon_path
# always use shipped pygments library
Expand Down Expand Up @@ -411,10 +412,20 @@ def highlight_code(ed):
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))
% 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
html = re.sub(r"{{", "{<!---->{", html)
html = re.sub(r"}}", "}<!---->}", html)
return html

# Hooks and monkey-patches

Expand Down

0 comments on commit cacf1b0

Please sign in to comment.