From d7c1ab0c7cb898ecb47d8fccae96976462a7ecdd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 Dec 2014 13:34:25 +0530 Subject: [PATCH] Fix make_highlighted_text to work with XML unsafe text --- src/calibre/gui2/tweak_book/widgets.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py index d2788703d6..dce7f3a6ab 100644 --- a/src/calibre/gui2/tweak_book/widgets.py +++ b/src/calibre/gui2/tweak_book/widgets.py @@ -251,13 +251,19 @@ class ImportForeign(Dialog): # {{{ # Quick Open {{{ def make_highlighted_text(emph, text, positions): - positions = sorted(set(positions) - {-1}, reverse=True) - for p in positions: - ch = get_char(text, p) - text = '%s%s%s' % (prepare_string_for_xml(text[:p]), emph, ch, prepare_string_for_xml(text[p+len(ch):])) + positions = sorted(set(positions) - {-1}) + if positions: + parts = [] + pos = 0 + for p in positions: + ch = get_char(text, p) + parts.append(prepare_string_for_xml(text[pos:p])) + parts.append('%s' % (emph, prepare_string_for_xml(ch))) + pos = p + len(ch) + parts.append(prepare_string_for_xml(text[pos:])) + return ''.join(parts) return text - class Results(QWidget): EMPH = "color:magenta; font-weight:bold"