canonicalize to NFC form on edit/import

This commit is contained in:
Damien Elmes 2017-02-19 12:49:52 +10:00
parent 2539896b22
commit 6335dcb90e
2 changed files with 9 additions and 1 deletions

View file

@ -3,6 +3,9 @@
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import cgi import cgi
import unicodedata
from anki.consts import NEW_CARDS_RANDOM, STARTING_FACTOR from anki.consts import NEW_CARDS_RANDOM, STARTING_FACTOR
from anki.lang import _ from anki.lang import _
from anki.utils import fieldChecksum, guid64, timestampID, \ from anki.utils import fieldChecksum, guid64, timestampID, \
@ -126,6 +129,8 @@ class NoteImporter(Importer):
n.fields[c] = n.fields[c].strip() n.fields[c] = n.fields[c].strip()
if not self.allowHTML: if not self.allowHTML:
n.fields[c] = n.fields[c].replace("\n", "<br>") n.fields[c] = n.fields[c].replace("\n", "<br>")
n.fields[c] = unicodedata.normalize("NFC", n.fields[c])
n.tags = [unicodedata.normalize("NFC", t) for t in n.tags]
fld0 = n.fields[fld0idx] fld0 = n.fields[fld0idx]
csum = fieldChecksum(fld0) csum = fieldChecksum(fld0)
# first field must exist # first field must exist

View file

@ -10,6 +10,7 @@ import warnings
import html import html
import mimetypes import mimetypes
import base64 import base64
import unicodedata
from anki.lang import _ from anki.lang import _
from aqt.qt import * from aqt.qt import *
@ -569,6 +570,7 @@ class Editor:
if cmd.startswith("blur") or cmd.startswith("key"): if cmd.startswith("blur") or cmd.startswith("key"):
(type, txt) = cmd.split(":", 1) (type, txt) = cmd.split(":", 1)
txt = urllib.parse.unquote(txt) txt = urllib.parse.unquote(txt)
txt = unicodedata.normalize("NFC", txt)
txt = self.mungeHTML(txt) txt = self.mungeHTML(txt)
# misbehaving apps may include a null byte in the text # misbehaving apps may include a null byte in the text
txt = txt.replace("\x00", "") txt = txt.replace("\x00", "")
@ -763,8 +765,9 @@ class Editor:
def saveTags(self): def saveTags(self):
if not self.note: if not self.note:
return return
tagsTxt = unicodedata.normalize("NFC", self.tags.text())
self.note.tags = self.mw.col.tags.canonify( self.note.tags = self.mw.col.tags.canonify(
self.mw.col.tags.split(self.tags.text())) self.mw.col.tags.split(tagsTxt))
self.tags.setText(self.mw.col.tags.join(self.note.tags).strip()) self.tags.setText(self.mw.col.tags.join(self.note.tags).strip())
if not self.addMode: if not self.addMode:
self.note.flush() self.note.flush()