mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Refactor the highlighter classes to make them completely trivial
This commit is contained in:
parent
966f4ab841
commit
96a2c155e9
@ -32,7 +32,10 @@ class SyntaxHighlighter(QSyntaxHighlighter):
|
|||||||
|
|
||||||
state_class = SimpleState
|
state_class = SimpleState
|
||||||
state_map = {0:lambda state, text, i, formats:[(len(text), None)]}
|
state_map = {0:lambda state, text, i, formats:[(len(text), None)]}
|
||||||
formats = {}
|
create_formats_func = lambda highlighter: {}
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
QSyntaxHighlighter.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
def rehighlight(self):
|
def rehighlight(self):
|
||||||
self.outlineexplorer_data = {}
|
self.outlineexplorer_data = {}
|
||||||
@ -46,7 +49,7 @@ class SyntaxHighlighter(QSyntaxHighlighter):
|
|||||||
self.rehighlight()
|
self.rehighlight()
|
||||||
|
|
||||||
def create_formats(self):
|
def create_formats(self):
|
||||||
pass
|
self.formats = self.create_formats_func()
|
||||||
|
|
||||||
def highlightBlock(self, text):
|
def highlightBlock(self, text):
|
||||||
try:
|
try:
|
||||||
|
@ -7,7 +7,6 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
|
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
|
||||||
|
|
||||||
@ -200,8 +199,9 @@ def comment(state, text, i, formats):
|
|||||||
state.parse = State.NORMAL
|
state.parse = State.NORMAL
|
||||||
return [(pos - i + 2, formats['comment'])]
|
return [(pos - i + 2, formats['comment'])]
|
||||||
|
|
||||||
def in_string(q, state, text, i, formats):
|
def in_string(state, text, i, formats):
|
||||||
'Inside a string'
|
'Inside a string'
|
||||||
|
q = '"' if state.parse == State.IN_DQS else "'"
|
||||||
pos = text.find(q, i)
|
pos = text.find(q, i)
|
||||||
if pos == -1:
|
if pos == -1:
|
||||||
if text[-1] == '\\':
|
if text[-1] == '\\':
|
||||||
@ -214,44 +214,43 @@ def in_string(q, state, text, i, formats):
|
|||||||
state_map = {
|
state_map = {
|
||||||
State.NORMAL:normal,
|
State.NORMAL:normal,
|
||||||
State.IN_COMMENT: comment,
|
State.IN_COMMENT: comment,
|
||||||
State.IN_SQS: partial(in_string, "'"),
|
State.IN_SQS: in_string,
|
||||||
State.IN_DQS: partial(in_string, '"'),
|
State.IN_DQS: in_string,
|
||||||
State.IN_CONTENT: content,
|
State.IN_CONTENT: content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def create_formats(highlighter):
|
||||||
|
theme = highlighter.theme
|
||||||
|
formats = {
|
||||||
|
'comment': theme['Comment'],
|
||||||
|
'error': theme['Error'],
|
||||||
|
'string': theme['String'],
|
||||||
|
'preproc': theme['PreProc'],
|
||||||
|
'keyword': theme['Keyword'],
|
||||||
|
'colorname': theme['Constant'],
|
||||||
|
'number': theme['Number'],
|
||||||
|
'operator': theme['Function'],
|
||||||
|
'bracket': theme['Special'],
|
||||||
|
'identifier': theme['Identifier'],
|
||||||
|
'id_selector': theme['Special'],
|
||||||
|
'class_selector': theme['Special'],
|
||||||
|
'pseudo_selector': theme['Special'],
|
||||||
|
'tag': theme['Identifier'],
|
||||||
|
}
|
||||||
|
for name, msg in {
|
||||||
|
'unknown-normal': _('Invalid text'),
|
||||||
|
'unterminated-string': _('Unterminated string'),
|
||||||
|
}.iteritems():
|
||||||
|
f = formats[name] = QTextCharFormat(formats['error'])
|
||||||
|
f.setToolTip(msg)
|
||||||
|
return formats
|
||||||
|
|
||||||
|
|
||||||
class CSSHighlighter(SyntaxHighlighter):
|
class CSSHighlighter(SyntaxHighlighter):
|
||||||
|
|
||||||
state_map = state_map
|
state_map = state_map
|
||||||
state_class = State
|
state_class = State
|
||||||
|
create_formats_func = create_formats
|
||||||
def __init__(self, parent):
|
|
||||||
SyntaxHighlighter.__init__(self, parent)
|
|
||||||
|
|
||||||
def create_formats(self):
|
|
||||||
t = self.theme
|
|
||||||
self.formats = {
|
|
||||||
'comment': t['Comment'],
|
|
||||||
'error': t['Error'],
|
|
||||||
'string': t['String'],
|
|
||||||
'preproc': t['PreProc'],
|
|
||||||
'keyword': t['Keyword'],
|
|
||||||
'colorname': t['Constant'],
|
|
||||||
'number': t['Number'],
|
|
||||||
'operator': t['Function'],
|
|
||||||
'bracket': t['Special'],
|
|
||||||
'identifier': t['Identifier'],
|
|
||||||
'id_selector': t['Special'],
|
|
||||||
'class_selector': t['Special'],
|
|
||||||
'pseudo_selector': t['Special'],
|
|
||||||
'tag': t['Identifier'],
|
|
||||||
}
|
|
||||||
for name, msg in {
|
|
||||||
'unknown-normal': _('Invalid text'),
|
|
||||||
'unterminated-string': _('Unterminated string'),
|
|
||||||
}.iteritems():
|
|
||||||
f = self.formats[name] = QTextCharFormat(self.formats['error'])
|
|
||||||
f.setToolTip(msg)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from calibre.gui2.tweak_book.editor.text import launch_editor
|
from calibre.gui2.tweak_book.editor.text import launch_editor
|
||||||
|
@ -249,43 +249,43 @@ for x in (State.IN_COMMENT, State.IN_PI, State.IN_DOCTYPE):
|
|||||||
for x in (State.SQ_VAL, State.DQ_VAL):
|
for x in (State.SQ_VAL, State.DQ_VAL):
|
||||||
state_map[x] = quoted_val
|
state_map[x] = quoted_val
|
||||||
|
|
||||||
|
def create_formats(highlighter):
|
||||||
|
t = highlighter.theme
|
||||||
|
formats = {
|
||||||
|
'tag': t['Function'],
|
||||||
|
'end_tag': t['Function'],
|
||||||
|
'attr': t['Type'],
|
||||||
|
'tag_name' : t['Statement'],
|
||||||
|
'entity': t['Special'],
|
||||||
|
'error': t['Error'],
|
||||||
|
'comment': t['Comment'],
|
||||||
|
'special': t['Special'],
|
||||||
|
'string': t['String'],
|
||||||
|
'nsprefix': t['Constant'],
|
||||||
|
'preproc': t['PreProc'],
|
||||||
|
'nbsp': t['CursorLine'],
|
||||||
|
}
|
||||||
|
for name, msg in {
|
||||||
|
'<': _('An unescaped < is not allowed. Replace it with <'),
|
||||||
|
'&': _('An unescaped ampersand is not allowed. Replace it with &'),
|
||||||
|
'>': _('An unescaped > is not allowed. Replace it with >'),
|
||||||
|
'/': _('/ not allowed except at the end of the tag'),
|
||||||
|
'?': _('Unknown character'),
|
||||||
|
'bad-closing': _('A closing tag must contain only the tag name and nothing else'),
|
||||||
|
'no-attr-value': _('Expecting an attribute value'),
|
||||||
|
}.iteritems():
|
||||||
|
f = formats[name] = QTextCharFormat(formats['error'])
|
||||||
|
f.setToolTip(msg)
|
||||||
|
f = formats['title'] = QTextCharFormat()
|
||||||
|
f.setFontWeight(QFont.Bold)
|
||||||
|
return formats
|
||||||
|
|
||||||
|
|
||||||
class HTMLHighlighter(SyntaxHighlighter):
|
class HTMLHighlighter(SyntaxHighlighter):
|
||||||
|
|
||||||
state_map = state_map
|
state_map = state_map
|
||||||
state_class = State
|
state_class = State
|
||||||
|
create_formats_func = create_formats
|
||||||
def __init__(self, parent):
|
|
||||||
SyntaxHighlighter.__init__(self, parent)
|
|
||||||
|
|
||||||
def create_formats(self):
|
|
||||||
t = self.theme
|
|
||||||
self.formats = {
|
|
||||||
'tag': t['Function'],
|
|
||||||
'end_tag': t['Function'],
|
|
||||||
'attr': t['Type'],
|
|
||||||
'tag_name' : t['Statement'],
|
|
||||||
'entity': t['Special'],
|
|
||||||
'error': t['Error'],
|
|
||||||
'comment': t['Comment'],
|
|
||||||
'special': t['Special'],
|
|
||||||
'string': t['String'],
|
|
||||||
'nsprefix': t['Constant'],
|
|
||||||
'preproc': t['PreProc'],
|
|
||||||
'nbsp': t['CursorLine'],
|
|
||||||
}
|
|
||||||
for name, msg in {
|
|
||||||
'<': _('An unescaped < is not allowed. Replace it with <'),
|
|
||||||
'&': _('An unescaped ampersand is not allowed. Replace it with &'),
|
|
||||||
'>': _('An unescaped > is not allowed. Replace it with >'),
|
|
||||||
'/': _('/ not allowed except at the end of the tag'),
|
|
||||||
'?': _('Unknown character'),
|
|
||||||
'bad-closing': _('A closing tag must contain only the tag name and nothing else'),
|
|
||||||
'no-attr-value': _('Expecting an attribute value'),
|
|
||||||
}.iteritems():
|
|
||||||
f = self.formats[name] = QTextCharFormat(self.formats['error'])
|
|
||||||
f.setToolTip(msg)
|
|
||||||
f = self.formats['title'] = QTextCharFormat()
|
|
||||||
f.setFontWeight(QFont.Bold)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from calibre.gui2.tweak_book.editor.text import launch_editor
|
from calibre.gui2.tweak_book.editor.text import launch_editor
|
||||||
|
Loading…
x
Reference in New Issue
Block a user