diff --git a/src/calibre/gui2/tweak_book/editor/smart/css.py b/src/calibre/gui2/tweak_book/editor/smart/css.py index 5cc2134dd4..6268ed9e62 100644 --- a/src/calibre/gui2/tweak_book/editor/smart/css.py +++ b/src/calibre/gui2/tweak_book/editor/smart/css.py @@ -26,6 +26,6 @@ def find_rule(raw, rule_address): rules = getattr(r, 'rules', ()) return ans -class CSSSmarts(NullSmarts): +class Smarts(NullSmarts): pass diff --git a/src/calibre/gui2/tweak_book/editor/smart/html.py b/src/calibre/gui2/tweak_book/editor/smart/html.py index 2e445d8ed0..01b4321960 100644 --- a/src/calibre/gui2/tweak_book/editor/smart/html.py +++ b/src/calibre/gui2/tweak_book/editor/smart/html.py @@ -269,7 +269,7 @@ def set_style_property(tag, property_name, value, editor): d.setProperty(property_name, value) c.insertText('"%s"' % css(d)) -class HTMLSmarts(NullSmarts): +class Smarts(NullSmarts): def __init__(self, *args, **kwargs): NullSmarts.__init__(self, *args, **kwargs) diff --git a/src/calibre/gui2/tweak_book/editor/syntax/css.py b/src/calibre/gui2/tweak_book/editor/syntax/css.py index 4f6d5c7f11..2b00e3192f 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/css.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/css.py @@ -306,7 +306,7 @@ def create_formats(highlighter): return formats -class CSSHighlighter(SyntaxHighlighter): +class Highlighter(SyntaxHighlighter): state_map = state_map create_formats_func = create_formats diff --git a/src/calibre/gui2/tweak_book/editor/syntax/html.py b/src/calibre/gui2/tweak_book/editor/syntax/html.py index a52ae14c76..6ee188c9f5 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/html.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/html.py @@ -543,7 +543,7 @@ def create_formats(highlighter, add_css=True): return formats -class HTMLHighlighter(SyntaxHighlighter): +class Highlighter(SyntaxHighlighter): state_map = state_map create_formats_func = create_formats @@ -553,7 +553,7 @@ class HTMLHighlighter(SyntaxHighlighter): def tag_ok_for_spell(self, name): return HTMLUserData.tag_ok_for_spell(name) -class XMLHighlighter(HTMLHighlighter): +class XMLHighlighter(Highlighter): state_map = xml_state_map spell_attributes = ('opf:file-as',) @@ -576,7 +576,7 @@ def profile(): raw = open(sys.argv[-2], 'rb').read().decode('utf-8') doc = QTextDocument() doc.setPlainText(raw) - h = HTMLHighlighter() + h = Highlighter() theme = get_theme(tprefs['editor_theme']) h.apply_theme(theme) h.set_document(doc) diff --git a/src/calibre/gui2/tweak_book/editor/syntax/xml.py b/src/calibre/gui2/tweak_book/editor/syntax/xml.py new file mode 100644 index 0000000000..5c67c57333 --- /dev/null +++ b/src/calibre/gui2/tweak_book/editor/syntax/xml.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +from __future__ import (unicode_literals, division, absolute_import, + print_function) + +__license__ = 'GPL v3' +__copyright__ = '2014, Kovid Goyal ' + +from calibre.gui2.tweak_book.editor.syntax.html import XMLHighlighter as Highlighter # noqa diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index f2cb315d5f..c7d2a2c52c 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -23,11 +23,7 @@ from calibre.gui2.tweak_book.editor import ( SYNTAX_PROPERTY, SPELL_PROPERTY, SPELL_LOCALE_PROPERTY, store_locale, LINK_PROPERTY) from calibre.gui2.tweak_book.editor.themes import get_theme, theme_color, theme_format from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter -from calibre.gui2.tweak_book.editor.syntax.html import HTMLHighlighter, XMLHighlighter -from calibre.gui2.tweak_book.editor.syntax.css import CSSHighlighter from calibre.gui2.tweak_book.editor.smart import NullSmarts -from calibre.gui2.tweak_book.editor.smart.html import HTMLSmarts -from calibre.gui2.tweak_book.editor.smart.css import CSSSmarts from calibre.spell.break_iterator import index_of from calibre.utils.icu import safe_chr, string_length, capitalize, upper, lower, swapcase from calibre.utils.titlecase import titlecase @@ -35,13 +31,11 @@ from calibre.utils.titlecase import titlecase PARAGRAPH_SEPARATOR = '\u2029' def get_highlighter(syntax): - ans = {'html':HTMLHighlighter, 'css':CSSHighlighter, 'xml':XMLHighlighter}.get(syntax, SyntaxHighlighter) - if ans is SyntaxHighlighter: - # Load these highlighters only on demand - try: - ans = importlib.import_module('calibre.gui2.tweak_book.editor.syntax.' + syntax).Highlighter - except (ImportError, AttributeError): - pass + # Load these highlighters only on demand + try: + ans = importlib.import_module('calibre.gui2.tweak_book.editor.syntax.' + syntax).Highlighter + except (ImportError, AttributeError): + ans = SyntaxHighlighter return ans _dff = None @@ -216,8 +210,12 @@ class TextEdit(PlainTextEdit): self.highlighter = get_highlighter(syntax)() self.highlighter.apply_theme(self.theme) self.highlighter.set_document(self.document(), doc_name=doc_name) - sclass = {'html':HTMLSmarts, 'xml':HTMLSmarts, 'css':CSSSmarts}.get(syntax, None) - if sclass is not None: + smartsname = {'xml':'html'}.get(syntax, syntax) + try: + sclass = importlib.import_module('calibre.gui2.tweak_book.editor.smart.' + smartsname).Smarts + except (ImportError, AttributeError): + pass + else: self.smarts = sclass(self) self.setPlainText(unicodedata.normalize('NFC', unicode(text))) if process_template and QPlainTextEdit.find(self, '%CURSOR%'):