125 lines
4.6 KiB
Python
125 lines
4.6 KiB
Python
# -*- 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):
|
||
QSyntaxHighlighter.__init__(self, theDoc)
|
||
|
||
logger.debug("Initialising DocHighlighter ...")
|
||
self.mainConf = nw.CONFIG
|
||
self.theDoc = theDoc
|
||
|
||
self.colHead = ( 0,155,200,255)
|
||
self.colEmph = (200,120, 0,255)
|
||
self.colDialN = (48, 200, 0,255)
|
||
self.colDialD = (184,200, 0,255)
|
||
self.colDialS = (136,200, 0,255)
|
||
self.colComm = (120,120,120,255)
|
||
self.colKey = (200, 0, 0,255)
|
||
self.colVal = (184,200, 0,255)
|
||
|
||
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),
|
||
"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),
|
||
}
|
||
|
||
self.hRules = [
|
||
(r"^#{1}[^#].*[^\n]", 0, self.hStyles["header1"]),
|
||
(r"^#{2}[^#].*[^\n]", 0, self.hStyles["header2"]),
|
||
(r"^#{3}[^#].*[^\n]", 0, self.hStyles["header3"]),
|
||
(r"^#{4}[^#].*[^\n]", 0, self.hStyles["header4"]),
|
||
(r"(?<![\w|\\])([\*]{2})(?!\s)(?m:(.+?))(?<![\s|\\])(\1)(?!\w)", 2, self.hStyles["bold"]),
|
||
(r"(?<![\w|_|\\])([_])(?!\s|\1)(?m:(.+?))(?<![\s|\\])(\1)(?!\w)", 2, self.hStyles["italic"]),
|
||
(r"(?<![\w|\\])([_]{2})(?!\s)(?m:(.+?))(?<![\s|\\])(\1)(?!\w)", 2, self.hStyles["underline"]),
|
||
(r"^(@.+?)\s*:\s*(.+?)$", 1, self.hStyles["keyword"]),
|
||
(r"^(@.+?)\s*:\s*(.+?)$", 2, self.hStyles["value"]),
|
||
(r"^%.*$", 0, self.hStyles["hidden"]),
|
||
]
|
||
self.hRules.append(
|
||
("{:s}(.+?){:s}".format('"','"'),0,self.hStyles["dialogue1"])
|
||
)
|
||
self.hRules.append(
|
||
("{:s}(.+?){:s}".format(*self.mainConf.fmtDoubleQuotes),0,self.hStyles["dialogue2"])
|
||
)
|
||
self.hRules.append(
|
||
("{:s}(.+?){:s}".format(*self.mainConf.fmtSingleQuotes),0,self.hStyles["dialogue3"])
|
||
)
|
||
|
||
# Build a QRegExp for each pattern
|
||
self.rules = [(QRegularExpression(a),b,c) for (a,b,c) in self.hRules]
|
||
|
||
logger.debug("DocHighlighter initialisation complete")
|
||
|
||
return
|
||
|
||
def _makeFormat(self, fmtCol=None, fmtStyle=None, fmtSize=None):
|
||
theFormat = QTextCharFormat()
|
||
|
||
if fmtCol is not None:
|
||
theCol = QColor()
|
||
if isinstance(fmtCol,str):
|
||
theCol.setNamedColor(fmtCol)
|
||
else:
|
||
theCol.setRgb(*fmtCol)
|
||
theFormat.setForeground(theCol)
|
||
|
||
if fmtStyle is not None:
|
||
if "bold" in fmtStyle:
|
||
theFormat.setFontWeight(QFont.Bold)
|
||
if "italic" in fmtStyle:
|
||
theFormat.setFontItalic(True)
|
||
if "strike" in fmtStyle:
|
||
theFormat.setFontStrikeOut(True)
|
||
if "underline" in fmtStyle:
|
||
theFormat.setFontUnderline(True)
|
||
|
||
if fmtSize is not None:
|
||
theFormat.setFontPointSize(round(fmtSize*self.mainConf.textSize))
|
||
|
||
return theFormat
|
||
|
||
def highlightBlock(self, theText):
|
||
for rX, nth, fmt in self.rules:
|
||
rxItt = rX.globalMatch(theText, 0)
|
||
while rxItt.hasNext():
|
||
rxMatch = rxItt.next()
|
||
xPos = rxMatch.capturedStart(nth)
|
||
xLen = rxMatch.capturedLength(nth)
|
||
# logger.verbose("Captured[%d]: '%s'" % (nth,rxMatch.captured(nth)))
|
||
# print(rxMatch.capturedTexts())
|
||
self.setFormat(xPos, xLen, fmt)
|
||
|
||
self.setCurrentBlockState(0)
|
||
|
||
# END Class DocHighlighter
|