Improved highlighting to work for all regex matches by specifying format in a dictionary
This commit is contained in:
+104
-38
@@ -26,25 +26,31 @@ class GuiDocHighlighter(QSyntaxHighlighter):
|
|||||||
logger.debug("Initialising DocHighlighter ...")
|
logger.debug("Initialising DocHighlighter ...")
|
||||||
self.mainConf = nw.CONFIG
|
self.mainConf = nw.CONFIG
|
||||||
self.theDoc = theDoc
|
self.theDoc = theDoc
|
||||||
|
self.hRules = []
|
||||||
|
|
||||||
self.colHead = ( 0,155,200,255)
|
self.colHead = ( 0,155,200,255)
|
||||||
|
self.colHeadH = ( 0,105,135,255)
|
||||||
self.colEmph = (200,120, 0,255)
|
self.colEmph = (200,120, 0,255)
|
||||||
self.colDialN = (48, 200, 0,255)
|
self.colDialN = (200, 46, 0,255)
|
||||||
self.colDialD = (184,200, 0,255)
|
self.colDialD = (184,200, 0,255)
|
||||||
self.colDialS = (136,200, 0,255)
|
self.colDialS = (136,200, 0,255)
|
||||||
self.colComm = (120,120,120,255)
|
self.colComm = (150,150,150,255)
|
||||||
self.colKey = (200, 0, 0,255)
|
self.colKey = (200, 0, 0,255)
|
||||||
self.colVal = (184,200, 0,255)
|
self.colVal = (184,200, 0,255)
|
||||||
|
|
||||||
self.hStyles = {
|
self.hStyles = {
|
||||||
"header1" : self._makeFormat(self.colHead,"bold",1.8),
|
"header1" : self._makeFormat(self.colHead, "bold",1.8),
|
||||||
"header2" : self._makeFormat(self.colHead,"bold",1.6),
|
"header2" : self._makeFormat(self.colHead, "bold",1.6),
|
||||||
"header3" : self._makeFormat(self.colHead,"bold",1.4),
|
"header3" : self._makeFormat(self.colHead, "bold",1.4),
|
||||||
"header4" : self._makeFormat(self.colHead,"bold",1.2),
|
"header4" : self._makeFormat(self.colHead, "bold",1.2),
|
||||||
"bold" : self._makeFormat(self.colEmph,"bold"),
|
"header1h" : self._makeFormat(self.colHeadH,"bold",1.8),
|
||||||
"italic" : self._makeFormat(self.colEmph,"italic"),
|
"header2h" : self._makeFormat(self.colHeadH,"bold",1.6),
|
||||||
"strike" : self._makeFormat(self.colEmph,"strike"),
|
"header3h" : self._makeFormat(self.colHeadH,"bold",1.4),
|
||||||
"underline" : self._makeFormat(self.colEmph,"underline"),
|
"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),
|
"dialogue1" : self._makeFormat(self.colDialN),
|
||||||
"dialogue2" : self._makeFormat(self.colDialD),
|
"dialogue2" : self._makeFormat(self.colDialD),
|
||||||
"dialogue3" : self._makeFormat(self.colDialS),
|
"dialogue3" : self._makeFormat(self.colDialS),
|
||||||
@@ -53,30 +59,89 @@ class GuiDocHighlighter(QSyntaxHighlighter):
|
|||||||
"value" : self._makeFormat(self.colVal),
|
"value" : self._makeFormat(self.colVal),
|
||||||
}
|
}
|
||||||
|
|
||||||
self.hRules = [
|
# Headers
|
||||||
(r"^#{1}[^#].*[^\n]", 0, self.hStyles["header1"]),
|
self.hRules.append((
|
||||||
(r"^#{2}[^#].*[^\n]", 0, self.hStyles["header2"]),
|
r"^(#{1})[^#](.*)[^\n]", {
|
||||||
(r"^#{3}[^#].*[^\n]", 0, self.hStyles["header3"]),
|
0 : self.hStyles["header1"],
|
||||||
(r"^#{4}[^#].*[^\n]", 0, self.hStyles["header4"]),
|
1 : self.hStyles["header1h"],
|
||||||
(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"]),
|
self.hRules.append((
|
||||||
(r"^(@.+?)\s*:\s*(.+?)$", 1, self.hStyles["keyword"]),
|
r"^(#{2})[^#](.*)[^\n]", {
|
||||||
(r"^(@.+?)\s*:\s*(.+?)$", 2, self.hStyles["value"]),
|
0 : self.hStyles["header2"],
|
||||||
(r"^%.*$", 0, self.hStyles["hidden"]),
|
1 : self.hStyles["header2h"],
|
||||||
]
|
}
|
||||||
self.hRules.append(
|
))
|
||||||
("{:s}(.+?){:s}".format('"','"'),0,self.hStyles["dialogue1"])
|
self.hRules.append((
|
||||||
)
|
r"^(#{3})[^#](.*)[^\n]", {
|
||||||
self.hRules.append(
|
0 : self.hStyles["header3"],
|
||||||
("{:s}(.+?){:s}".format(*self.mainConf.fmtDoubleQuotes),0,self.hStyles["dialogue2"])
|
1 : self.hStyles["header3h"],
|
||||||
)
|
}
|
||||||
self.hRules.append(
|
))
|
||||||
("{:s}(.+?){:s}".format(*self.mainConf.fmtSingleQuotes),0,self.hStyles["dialogue3"])
|
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"(?<![\w|\\])([\*]{2})(?!\s)(?m:(.+?))(?<![\s|\\])(\1)(?!\w)", {
|
||||||
|
1 : self.hStyles["hidden"],
|
||||||
|
2 : self.hStyles["bold"],
|
||||||
|
3 : self.hStyles["hidden"],
|
||||||
|
}
|
||||||
|
))
|
||||||
|
self.hRules.append((
|
||||||
|
r"(?<![\w|_|\\])([_])(?!\s|\1)(?m:(.+?))(?<![\s|\\])(\1)(?!\w)", {
|
||||||
|
1 : self.hStyles["hidden"],
|
||||||
|
2 : self.hStyles["italic"],
|
||||||
|
3 : self.hStyles["hidden"],
|
||||||
|
}
|
||||||
|
))
|
||||||
|
self.hRules.append((
|
||||||
|
r"(?<![\w|\\])([_]{2})(?!\s)(?m:(.+?))(?<![\s|\\])(\1)(?!\w)", {
|
||||||
|
1 : self.hStyles["hidden"],
|
||||||
|
2 : self.hStyles["underline"],
|
||||||
|
3 : self.hStyles["hidden"],
|
||||||
|
}
|
||||||
|
))
|
||||||
|
|
||||||
|
# Quoted Strings
|
||||||
|
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
|
# Build a QRegExp for each pattern
|
||||||
self.rules = [(QRegularExpression(a),b,c) for (a,b,c) in self.hRules]
|
self.rules = [(QRegularExpression(a),b) for (a,b) in self.hRules]
|
||||||
|
|
||||||
logger.debug("DocHighlighter initialisation complete")
|
logger.debug("DocHighlighter initialisation complete")
|
||||||
|
|
||||||
@@ -109,15 +174,16 @@ class GuiDocHighlighter(QSyntaxHighlighter):
|
|||||||
return theFormat
|
return theFormat
|
||||||
|
|
||||||
def highlightBlock(self, theText):
|
def highlightBlock(self, theText):
|
||||||
for rX, nth, fmt in self.rules:
|
for rX, xFmt in self.rules:
|
||||||
rxItt = rX.globalMatch(theText, 0)
|
rxItt = rX.globalMatch(theText, 0)
|
||||||
while rxItt.hasNext():
|
while rxItt.hasNext():
|
||||||
rxMatch = rxItt.next()
|
rxMatch = rxItt.next()
|
||||||
xPos = rxMatch.capturedStart(nth)
|
for xM in xFmt.keys():
|
||||||
xLen = rxMatch.capturedLength(nth)
|
xPos = rxMatch.capturedStart(xM)
|
||||||
# logger.verbose("Captured[%d]: '%s'" % (nth,rxMatch.captured(nth)))
|
xLen = rxMatch.capturedLength(xM)
|
||||||
# print(rxMatch.capturedTexts())
|
# logger.verbose("Captured[%d]: '%s'" % (nth,rxMatch.captured(nth)))
|
||||||
self.setFormat(xPos, xLen, fmt)
|
# print(rxMatch.capturedTexts())
|
||||||
|
self.setFormat(xPos, xLen, xFmt[xM])
|
||||||
|
|
||||||
self.setCurrentBlockState(0)
|
self.setCurrentBlockState(0)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user