From 96a2c155e92cde3b6b319f2bdad279d7afd4f904 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 31 Oct 2013 17:24:46 +0530 Subject: [PATCH] Refactor the highlighter classes to make them completely trivial --- .../gui2/tweak_book/editor/syntax/base.py | 7 +- .../gui2/tweak_book/editor/syntax/css.py | 65 +++++++++--------- .../gui2/tweak_book/editor/syntax/html.py | 66 +++++++++---------- 3 files changed, 70 insertions(+), 68 deletions(-) diff --git a/src/calibre/gui2/tweak_book/editor/syntax/base.py b/src/calibre/gui2/tweak_book/editor/syntax/base.py index 3e59729762..e9ce07dfb9 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/base.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/base.py @@ -32,7 +32,10 @@ class SyntaxHighlighter(QSyntaxHighlighter): state_class = SimpleState 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): self.outlineexplorer_data = {} @@ -46,7 +49,7 @@ class SyntaxHighlighter(QSyntaxHighlighter): self.rehighlight() def create_formats(self): - pass + self.formats = self.create_formats_func() def highlightBlock(self, text): try: diff --git a/src/calibre/gui2/tweak_book/editor/syntax/css.py b/src/calibre/gui2/tweak_book/editor/syntax/css.py index cbc3ebf5a0..c4ad909ac0 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/css.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/css.py @@ -7,7 +7,6 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' import re -from functools import partial from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter @@ -200,8 +199,9 @@ def comment(state, text, i, formats): state.parse = State.NORMAL return [(pos - i + 2, formats['comment'])] -def in_string(q, state, text, i, formats): +def in_string(state, text, i, formats): 'Inside a string' + q = '"' if state.parse == State.IN_DQS else "'" pos = text.find(q, i) if pos == -1: if text[-1] == '\\': @@ -214,44 +214,43 @@ def in_string(q, state, text, i, formats): state_map = { State.NORMAL:normal, State.IN_COMMENT: comment, - State.IN_SQS: partial(in_string, "'"), - State.IN_DQS: partial(in_string, '"'), + State.IN_SQS: in_string, + State.IN_DQS: in_string, 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): state_map = state_map state_class = State - - 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) - + create_formats_func = create_formats if __name__ == '__main__': from calibre.gui2.tweak_book.editor.text import launch_editor diff --git a/src/calibre/gui2/tweak_book/editor/syntax/html.py b/src/calibre/gui2/tweak_book/editor/syntax/html.py index dccb4f7043..c9aa5a6f73 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/html.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/html.py @@ -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): 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): state_map = state_map state_class = State - - 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) + create_formats_func = create_formats if __name__ == '__main__': from calibre.gui2.tweak_book.editor.text import launch_editor