Only operate on normalized unicodedata in the editor

This commit is contained in:
Kovid Goyal 2013-12-01 12:37:39 +05:30
parent 06aa59563d
commit f1fc5598b0
2 changed files with 8 additions and 6 deletions

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import textwrap import textwrap, unicodedata
from future_builtins import map from future_builtins import map
import regex import regex
@ -75,7 +75,7 @@ class TextEdit(QPlainTextEdit):
@property @property
def selected_text(self): def selected_text(self):
return unicode(self.textCursor().selectedText()) return unicodedata.normalize('NFC', unicode(self.textCursor().selectedText()))
def sizeHint(self): def sizeHint(self):
return self.size_hint return self.size_hint
@ -125,7 +125,7 @@ class TextEdit(QPlainTextEdit):
self.highlighter = {'html':HTMLHighlighter, 'css':CSSHighlighter, 'xml':XMLHighlighter}.get(syntax, SyntaxHighlighter)(self) self.highlighter = {'html':HTMLHighlighter, 'css':CSSHighlighter, 'xml':XMLHighlighter}.get(syntax, SyntaxHighlighter)(self)
self.highlighter.apply_theme(self.theme) self.highlighter.apply_theme(self.theme)
self.highlighter.setDocument(self.document()) self.highlighter.setDocument(self.document())
self.setPlainText(text) self.setPlainText(unicodedata.normalize('NFC', text))
if process_template and QPlainTextEdit.find(self, '%CURSOR%'): if process_template and QPlainTextEdit.find(self, '%CURSOR%'):
c = self.textCursor() c = self.textCursor()
c.insertText('') c.insertText('')
@ -136,7 +136,7 @@ class TextEdit(QPlainTextEdit):
c.beginEditBlock() c.beginEditBlock()
c.clearSelection() c.clearSelection()
c.select(c.Document) c.select(c.Document)
c.insertText(text) c.insertText(unicodedata.normalize('NFC', text))
c.endEditBlock() c.endEditBlock()
c.setPosition(min(pos, len(text))) c.setPosition(min(pos, len(text)))
self.setTextCursor(c) self.setTextCursor(c)

View File

@ -6,6 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import unicodedata
from PyQt4.Qt import QMainWindow, Qt, QApplication, pyqtSignal from PyQt4.Qt import QMainWindow, Qt, QApplication, pyqtSignal
from calibre import xml_replace_entities from calibre import xml_replace_entities
@ -56,7 +58,7 @@ class Editor(QMainWindow):
@dynamic_property @dynamic_property
def data(self): def data(self):
def fget(self): def fget(self):
ans = unicode(self.editor.toPlainText()) ans = self.get_raw_data()
if self.syntax == 'html': if self.syntax == 'html':
ans = xml_replace_entities(ans) ans = xml_replace_entities(ans)
return ans.encode('utf-8') return ans.encode('utf-8')
@ -68,7 +70,7 @@ class Editor(QMainWindow):
self.editor.load_text(template, syntax=self.syntax, process_template=True) self.editor.load_text(template, syntax=self.syntax, process_template=True)
def get_raw_data(self): def get_raw_data(self):
return unicode(self.editor.toPlainText()) return unicodedata.normalize('NFC', unicode(self.editor.toPlainText()))
def replace_data(self, raw, only_if_different=True): def replace_data(self, raw, only_if_different=True):
if isinstance(raw, bytes): if isinstance(raw, bytes):