Load all syntax related code on demand

This commit is contained in:
Kovid Goyal 2014-11-21 14:15:20 +05:30
parent 4d7da61387
commit c40483eb86
6 changed files with 26 additions and 19 deletions

View File

@ -26,6 +26,6 @@ def find_rule(raw, rule_address):
rules = getattr(r, 'rules', ()) rules = getattr(r, 'rules', ())
return ans return ans
class CSSSmarts(NullSmarts): class Smarts(NullSmarts):
pass pass

View File

@ -269,7 +269,7 @@ def set_style_property(tag, property_name, value, editor):
d.setProperty(property_name, value) d.setProperty(property_name, value)
c.insertText('"%s"' % css(d)) c.insertText('"%s"' % css(d))
class HTMLSmarts(NullSmarts): class Smarts(NullSmarts):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
NullSmarts.__init__(self, *args, **kwargs) NullSmarts.__init__(self, *args, **kwargs)

View File

@ -306,7 +306,7 @@ def create_formats(highlighter):
return formats return formats
class CSSHighlighter(SyntaxHighlighter): class Highlighter(SyntaxHighlighter):
state_map = state_map state_map = state_map
create_formats_func = create_formats create_formats_func = create_formats

View File

@ -543,7 +543,7 @@ def create_formats(highlighter, add_css=True):
return formats return formats
class HTMLHighlighter(SyntaxHighlighter): class Highlighter(SyntaxHighlighter):
state_map = state_map state_map = state_map
create_formats_func = create_formats create_formats_func = create_formats
@ -553,7 +553,7 @@ class HTMLHighlighter(SyntaxHighlighter):
def tag_ok_for_spell(self, name): def tag_ok_for_spell(self, name):
return HTMLUserData.tag_ok_for_spell(name) return HTMLUserData.tag_ok_for_spell(name)
class XMLHighlighter(HTMLHighlighter): class XMLHighlighter(Highlighter):
state_map = xml_state_map state_map = xml_state_map
spell_attributes = ('opf:file-as',) spell_attributes = ('opf:file-as',)
@ -576,7 +576,7 @@ def profile():
raw = open(sys.argv[-2], 'rb').read().decode('utf-8') raw = open(sys.argv[-2], 'rb').read().decode('utf-8')
doc = QTextDocument() doc = QTextDocument()
doc.setPlainText(raw) doc.setPlainText(raw)
h = HTMLHighlighter() h = Highlighter()
theme = get_theme(tprefs['editor_theme']) theme = get_theme(tprefs['editor_theme'])
h.apply_theme(theme) h.apply_theme(theme)
h.set_document(doc) h.set_document(doc)

View File

@ -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 <kovid at kovidgoyal.net>'
from calibre.gui2.tweak_book.editor.syntax.html import XMLHighlighter as Highlighter # noqa

View File

@ -23,11 +23,7 @@ from calibre.gui2.tweak_book.editor import (
SYNTAX_PROPERTY, SPELL_PROPERTY, SPELL_LOCALE_PROPERTY, store_locale, LINK_PROPERTY) 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.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.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 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.spell.break_iterator import index_of
from calibre.utils.icu import safe_chr, string_length, capitalize, upper, lower, swapcase from calibre.utils.icu import safe_chr, string_length, capitalize, upper, lower, swapcase
from calibre.utils.titlecase import titlecase from calibre.utils.titlecase import titlecase
@ -35,13 +31,11 @@ from calibre.utils.titlecase import titlecase
PARAGRAPH_SEPARATOR = '\u2029' PARAGRAPH_SEPARATOR = '\u2029'
def get_highlighter(syntax): def get_highlighter(syntax):
ans = {'html':HTMLHighlighter, 'css':CSSHighlighter, 'xml':XMLHighlighter}.get(syntax, SyntaxHighlighter)
if ans is SyntaxHighlighter:
# Load these highlighters only on demand # Load these highlighters only on demand
try: try:
ans = importlib.import_module('calibre.gui2.tweak_book.editor.syntax.' + syntax).Highlighter ans = importlib.import_module('calibre.gui2.tweak_book.editor.syntax.' + syntax).Highlighter
except (ImportError, AttributeError): except (ImportError, AttributeError):
pass ans = SyntaxHighlighter
return ans return ans
_dff = None _dff = None
@ -216,8 +210,12 @@ class TextEdit(PlainTextEdit):
self.highlighter = get_highlighter(syntax)() self.highlighter = get_highlighter(syntax)()
self.highlighter.apply_theme(self.theme) self.highlighter.apply_theme(self.theme)
self.highlighter.set_document(self.document(), doc_name=doc_name) self.highlighter.set_document(self.document(), doc_name=doc_name)
sclass = {'html':HTMLSmarts, 'xml':HTMLSmarts, 'css':CSSSmarts}.get(syntax, None) smartsname = {'xml':'html'}.get(syntax, syntax)
if sclass is not None: try:
sclass = importlib.import_module('calibre.gui2.tweak_book.editor.smart.' + smartsname).Smarts
except (ImportError, AttributeError):
pass
else:
self.smarts = sclass(self) self.smarts = sclass(self)
self.setPlainText(unicodedata.normalize('NFC', unicode(text))) self.setPlainText(unicodedata.normalize('NFC', unicode(text)))
if process_template and QPlainTextEdit.find(self, '%CURSOR%'): if process_template and QPlainTextEdit.find(self, '%CURSOR%'):