diff --git a/src/calibre/gui2/tweak_book/editor/syntax/base.py b/src/calibre/gui2/tweak_book/editor/syntax/base.py index c0d3ca5541..1911c145b0 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/base.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/base.py @@ -26,3 +26,27 @@ class SyntaxHighlighter(QSyntaxHighlighter): def create_formats(self): pass + def highlightBlock(self, text): + try: + state = self.previousBlockState() + if state == -1: + state = 0 + self.do_highlight(state, unicode(text)) + except: + import traceback + traceback.print_exc() + + def do_highlight(self, state, text): + state = self.state_class(state) + + i = 0 + while i < len(text): + fmt = self.state_map[state.parse](state, text, i, self.formats) + for num, f in fmt: + if f is not None: + self.setFormat(i, num, f) + i += num + + self.setCurrentBlockState(state.value) + + diff --git a/src/calibre/gui2/tweak_book/editor/syntax/html.py b/src/calibre/gui2/tweak_book/editor/syntax/html.py index 7fd7d0fa18..2883e84eee 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/html.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/html.py @@ -251,6 +251,9 @@ for x in (State.SQ_VAL, State.DQ_VAL): class HTMLHighlighter(SyntaxHighlighter): + state_map = state_map + state_class = State + def __init__(self, parent): SyntaxHighlighter.__init__(self, parent) @@ -284,29 +287,6 @@ class HTMLHighlighter(SyntaxHighlighter): f = self.formats['title'] = QTextCharFormat() f.setFontWeight(QFont.Bold) - def highlightBlock(self, text): - try: - self.do_highlight(unicode(text)) - except: - import traceback - traceback.print_exc() - - def do_highlight(self, text): - state = self.previousBlockState() - if state == -1: - state = State.NORMAL - state = State(state) - - i = 0 - while i < len(text): - fmt = state_map[state.parse](state, text, i, self.formats) - for num, f in fmt: - if f is not None: - self.setFormat(i, num, f) - i += num - - self.setCurrentBlockState(state.value) - if __name__ == '__main__': from calibre.gui2.tweak_book.editor.text import launch_editor launch_editor('''\