From 8850161641a627350956b7fc93f0cff921b92c90 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 27 Oct 2024 01:55:19 +0200 Subject: [PATCH] Add new logic for dialogue narrator break --- novelwriter/gui/dochighlight.py | 34 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/novelwriter/gui/dochighlight.py b/novelwriter/gui/dochighlight.py index ceb4abb4..1931640e 100644 --- a/novelwriter/gui/dochighlight.py +++ b/novelwriter/gui/dochighlight.py @@ -59,7 +59,8 @@ class GuiDocHighlighter(QSyntaxHighlighter): __slots__ = ( "_tHandle", "_isNovel", "_isInactive", "_spellCheck", "_spellErr", - "_hStyles", "_minRules", "_txtRules", "_cmnRules", + "_hStyles", "_minRules", "_txtRules", "_cmnRules", "_dialogLine", + "_narratorBreak", ) def __init__(self, document: QTextDocument) -> None: @@ -78,6 +79,9 @@ class GuiDocHighlighter(QSyntaxHighlighter): self._txtRules: list[tuple[re.Pattern, dict[int, QTextCharFormat]]] = [] self._cmnRules: list[tuple[re.Pattern, dict[int, QTextCharFormat]]] = [] + self._dialogLine = "" + self._narratorBreak = "" + self.initHighlighter() logger.debug("Ready: GuiDocHighlighter") @@ -132,6 +136,9 @@ class GuiDocHighlighter(QSyntaxHighlighter): self._txtRules.clear() self._cmnRules.clear() + self._dialogLine = CONFIG.dialogLine + self._narratorBreak = CONFIG.narratorBreak + # Multiple or Trailing Spaces if CONFIG.showMultiSpaces: rxRule = re.compile(r"[ ]{2,}|[ ]*$", re.UNICODE) @@ -159,20 +166,6 @@ class GuiDocHighlighter(QSyntaxHighlighter): } self._txtRules.append((rxRule, hlRule)) - if CONFIG.dialogLine: - rxRule = REGEX_PATTERNS.dialogLine - hlRule = { - 0: self._hStyles["dialog"], - } - self._txtRules.append((rxRule, hlRule)) - - if CONFIG.narratorBreak: - rxRule = REGEX_PATTERNS.narratorBreak - hlRule = { - 0: self._hStyles["text"], - } - self._txtRules.append((rxRule, hlRule)) - if CONFIG.altDialogOpen and CONFIG.altDialogClose: rxRule = REGEX_PATTERNS.altDialogStyle hlRule = { @@ -411,6 +404,17 @@ class GuiDocHighlighter(QSyntaxHighlighter): self.setCurrentBlockState(BLOCK_TEXT) hRules = self._txtRules if self._isNovel else self._minRules + if self._dialogLine and text.startswith(self._dialogLine): + if self._narratorBreak: + tPos = 0 + for tNum, tBit in enumerate(text[1:].split(self._narratorBreak), 1): + tLen = len(tBit) + 1 + if tNum%2: + self.setFormat(tPos, tLen, self._hStyles["dialog"]) + tPos += tLen + else: + self.setFormat(0, len(text), self._hStyles["dialog"]) + if hRules: for rX, hRule in hRules: for res in re.finditer(rX, text[xOff:]):