Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

fix: display tones in newer anki #204

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 21 additions & 5 deletions chinese/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
# You should have received a copy of the GNU General Public License along with
# Chinese Support Redux. If not, see <https://www.gnu.org/licenses/>.

import re

from anki.hooks import addHook
from aqt.utils import showWarning
from aqt import mw

from .behavior import update_fields
Expand Down Expand Up @@ -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)