From c1b85f2f707a6040352d63982b9db5fb42644ba7 Mon Sep 17 00:00:00 2001 From: psii Date: Sun, 20 Mar 2022 10:26:43 +0100 Subject: [PATCH] fix: display tones in newer anki fixes https://github.com/luoliyan/chinese-support-redux/issues/201 --- chinese/edit.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/chinese/edit.py b/chinese/edit.py index b9c5e39..ecf7746 100644 --- a/chinese/edit.py +++ b/chinese/edit.py @@ -17,7 +17,10 @@ # You should have received a copy of the GNU General Public License along with # Chinese Support Redux. If not, see . +import re + from anki.hooks import addHook +from aqt.utils import showWarning from aqt import mw from .behavior import update_fields @@ -80,12 +83,25 @@ def onFocusLost(self, _, note, index): return False -def append_tone_styling(editor): - js = 'var css = document.styleSheets[0];' +CSS_RULE = re.compile("([^ ]+) *\\{([^}]*)\\}") - for line in editor.note.model()['css'].split('\n'): +def append_tone_styling(editor): + rules = [] + for line in editor.note.note_type()['css'].split('\n'): if line.startswith('.tone'): - js += 'css.insertRule("{}", css.cssRules.length);'.format( - line.rstrip()) + m = CSS_RULE.search(line) + if m: + rules.append((m.group(1), m.group(2))) + else: + showWarning("WARN: could not parse CSS tone rule. " + "Currently, tone CSS rules need to be one liners.") + + inner_js = "" + for rulename, ruledef in rules: + for part in ruledef.split(';'): + if ':' in part: + [property, value] = part.split(':', 1) + inner_js += f"jQuery('{rulename.strip()}', this.shadowRoot).css('{property.strip()}', '{value.strip()}');\n" + js = "jQuery('div.field').each(function () {\n%s})" % inner_js editor.web.eval(js)