From 2754f780834420a4527421caed092fdd9a8eeb41 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 3 Nov 2024 21:14:11 +0100 Subject: [PATCH] Merge Spanish and Portuguese narrator break logic --- novelwriter/text/patterns.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/novelwriter/text/patterns.py b/novelwriter/text/patterns.py index 350ab69d..cecaf166 100644 --- a/novelwriter/text/patterns.py +++ b/novelwriter/text/patterns.py @@ -136,7 +136,7 @@ class DialogParser: self._dialog = uniqueCompact(CONFIG.dialogLine) self._narrator = CONFIG.narratorBreak.strip()[:1] self._break = re.compile( - f"({self._narrator}\\s?.*?\\s?(?:{self._narrator}[{punct}]?|$))", re.UNICODE + f"({self._narrator}\\s?.*?)(\\s?(?:{self._narrator}[{punct}]?|$))", re.UNICODE ) self._enabled = bool(self._quotes or self._dialog or self._narrator) return @@ -145,26 +145,31 @@ class DialogParser: """Caller wrapper for dialogue processing.""" temp: list[int] = [] if text: - plain = True if self._dialog and text[0] in self._dialog: - plain = False + # The whole line is dialogue temp.append(0) temp.append(len(text)) if self._narrator: + # Process narrator breaks in the dialogue for res in self._break.finditer(text, 1): temp.append(res.start(0)) - temp.append(res.end(0)) + if (two := res.group(2)) and two[0].isspace(): + temp.append(res.start(2)) + else: + temp.append(res.end(0)) elif self._quotes: + # The line contains quoted dialogue for res in self._quotes.finditer(text): - plain = False temp.append(res.start(0)) temp.append(res.end(0)) if self._narrator: - for sub in self._break.finditer(text, res.start(0), res.end(0)): - temp.append(sub.start(0)) - temp.append(sub.end(0)) - - if plain and self._narrator: + for res in self._break.finditer(text, 1): + temp.append(res.start(0)) + if (two := res.group(2)) and two[0].isspace(): + temp.append(res.start(2)) + else: + temp.append(res.end(0)) + elif not self._dialog and self._narrator: pos = 0 for num, bit in enumerate(text.split(self._narrator)): length = len(bit) + int(num > 0)