mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Load all syntax related code on demand
This commit is contained in:
parent
4d7da61387
commit
c40483eb86
@ -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
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
@ -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
|
||||||
|
@ -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)
|
||||||
|
9
src/calibre/gui2/tweak_book/editor/syntax/xml.py
Normal file
9
src/calibre/gui2/tweak_book/editor/syntax/xml.py
Normal 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
|
@ -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)
|
# Load these highlighters only on demand
|
||||||
if ans is SyntaxHighlighter:
|
try:
|
||||||
# Load these highlighters only on demand
|
ans = importlib.import_module('calibre.gui2.tweak_book.editor.syntax.' + syntax).Highlighter
|
||||||
try:
|
except (ImportError, AttributeError):
|
||||||
ans = importlib.import_module('calibre.gui2.tweak_book.editor.syntax.' + syntax).Highlighter
|
ans = SyntaxHighlighter
|
||||||
except (ImportError, AttributeError):
|
|
||||||
pass
|
|
||||||
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%'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user