mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-09-28 15:31:38 +12:00
81 lines
No EOL
2.9 KiB
Python
81 lines
No EOL
2.9 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf8 -*-
|
|
|
|
from qt import *
|
|
import re
|
|
from functions import *
|
|
|
|
class basicHighlighter(QSyntaxHighlighter):
|
|
|
|
def __init__(self, editor):
|
|
QSyntaxHighlighter.__init__(self, editor.document())
|
|
|
|
self.editor = editor
|
|
self._misspelledColor = Qt.red
|
|
self._defaultBlockFormat = QTextBlockFormat()
|
|
self._defaultCharFormat = QTextCharFormat()
|
|
|
|
def setDefaultBlockFormat(self, bf):
|
|
self._defaultBlockFormat = bf
|
|
self.rehighlight()
|
|
|
|
def setDefaultCharFormat(self, cf):
|
|
self._defaultCharFormat = cf
|
|
self.rehighlight()
|
|
|
|
def setMisspelledColor(self, color):
|
|
self._misspelledColor = color
|
|
|
|
|
|
def highlightBlock(self, text):
|
|
"""Apply syntax highlighting to the given block of text.
|
|
"""
|
|
self.highlightBlockBefore(text)
|
|
self.highlightBlockAfter(text)
|
|
|
|
def highlightBlockBefore(self, text):
|
|
"""Highlighting to do before anything else.
|
|
|
|
When subclassing basicHighlighter, you must call highlightBlockBefore
|
|
before you do any custom highlighting.
|
|
"""
|
|
bf = QTextBlockFormat(self._defaultBlockFormat)
|
|
bf.setAlignment(QTextCursor(self.currentBlock()).blockFormat().alignment())
|
|
QTextCursor(self.currentBlock()).setBlockFormat(bf)
|
|
|
|
self.setFormat(0, len(text), self._defaultCharFormat)
|
|
|
|
def highlightBlockAfter(self, text):
|
|
"""Highlighting to do after everything else.
|
|
|
|
When subclassing basicHighlighter, you must call highlightBlockAfter
|
|
after your custom highlighting.
|
|
"""
|
|
|
|
# References
|
|
for txt in re.finditer(r"::(\w):(\d+?)::", text):
|
|
fmt = self.format(txt.start())
|
|
fmt.setFontFixedPitch(True)
|
|
fmt.setFontWeight(QFont.DemiBold)
|
|
if txt.group(1) == "T":
|
|
fmt.setBackground(QBrush(QColor(Qt.blue).lighter(190)))
|
|
elif txt.group(1) == "C":
|
|
fmt.setBackground(QBrush(QColor(Qt.yellow).lighter(170)))
|
|
self.setFormat(txt.start(),
|
|
txt.end() - txt.start(),
|
|
fmt)
|
|
|
|
# Spell checking
|
|
# Based on http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
|
|
WORDS = '(?iu)[\w\']+'
|
|
if self.editor.spellcheck:
|
|
for word_object in re.finditer(WORDS, text):
|
|
if self.editor._dict and not self.editor._dict.check(word_object.group()):
|
|
format = self.format(word_object.start())
|
|
format.setUnderlineColor(self._misspelledColor)
|
|
format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
|
|
self.setFormat(word_object.start(),
|
|
word_object.end() - word_object.start(), format)
|
|
|
|
|
|
|