# -*- coding: utf-8 -*- """novelWriter GUI Document Highlighter novelWriter – GUI Document Highlighter ======================================== Syntax highlighting for MarkDown File History: Created: 2019-04-06 [0.0.1] """ import logging import nw from PyQt5.QtCore import QRegularExpression from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter logger = logging.getLogger(__name__) class GuiDocHighlighter(QSyntaxHighlighter): def __init__(self, theDoc, theParent): QSyntaxHighlighter.__init__(self, theDoc) logger.debug("Initialising DocHighlighter ...") self.mainConf = nw.CONFIG self.theDoc = theDoc self.theParent = theParent self.theTheme = theParent.theTheme self.theIndex = theParent.theIndex self.theDict = None self.theHandle = None self.spellCheck = False self.hRules = [] self.colHead = QColor(*self.theTheme.colHead) self.colHeadH = QColor(*self.theTheme.colHeadH) self.colEmph = QColor(*self.theTheme.colEmph) self.colDialN = QColor(*self.theTheme.colDialN) self.colDialD = QColor(*self.theTheme.colDialD) self.colDialS = QColor(*self.theTheme.colDialS) self.colComm = QColor(*self.theTheme.colComm) self.colKey = QColor(*self.theTheme.colKey) self.colVal = QColor(*self.theTheme.colVal) self.colSpell = QColor(*self.theTheme.colSpell) self.colTagErr = QColor(*self.theTheme.colTagErr) self.hStyles = { "header1" : self._makeFormat(self.colHead, "bold",1.8), "header2" : self._makeFormat(self.colHead, "bold",1.6), "header3" : self._makeFormat(self.colHead, "bold",1.4), "header4" : self._makeFormat(self.colHead, "bold",1.2), "header1h" : self._makeFormat(self.colHeadH,"bold",1.8), "header2h" : self._makeFormat(self.colHeadH,"bold",1.6), "header3h" : self._makeFormat(self.colHeadH,"bold",1.4), "header4h" : self._makeFormat(self.colHeadH,"bold",1.2), "bold" : self._makeFormat(self.colEmph, "bold"), "italic" : self._makeFormat(self.colEmph, "italic"), "strike" : self._makeFormat(self.colEmph, "strike"), "underline" : self._makeFormat(self.colEmph, "underline"), "dialogue1" : self._makeFormat(self.colDialN), "dialogue2" : self._makeFormat(self.colDialD), "dialogue3" : self._makeFormat(self.colDialS), "hidden" : self._makeFormat(self.colComm), "keyword" : self._makeFormat(self.colKey), "value" : self._makeFormat(self.colVal), } # Headers self.hRules.append(( r"^(#{1}) (.*)[^\n]", { 0 : self.hStyles["header1"], 1 : self.hStyles["header1h"], } )) self.hRules.append(( r"^(#{2}) (.*)[^\n]", { 0 : self.hStyles["header2"], 1 : self.hStyles["header2h"], } )) self.hRules.append(( r"^(#{3}) (.*)[^\n]", { 0 : self.hStyles["header3"], 1 : self.hStyles["header3h"], } )) self.hRules.append(( r"^(#{4}) (.*)[^\n]", { 0 : self.hStyles["header4"], 1 : self.hStyles["header4h"], } )) # Keyword/Value # self.hRules.append(( # r"^(@.+?)\s*:\s*(.+?)$", { # 1 : self.hStyles["keyword"], # 2 : self.hStyles["value"], # } # )) # Comments self.hRules.append(( r"^%.*$", { 0 : self.hStyles["hidden"], } )) # Markdown self.hRules.append(( r"(?