Move text regex patterns to a separate class
This commit is contained in:
@@ -39,6 +39,7 @@ from novelwriter.common import checkInt
|
|||||||
from novelwriter.constants import nwHeaders, nwRegEx, nwUnicode
|
from novelwriter.constants import nwHeaders, nwRegEx, nwUnicode
|
||||||
from novelwriter.core.index import processComment
|
from novelwriter.core.index import processComment
|
||||||
from novelwriter.enum import nwComment
|
from novelwriter.enum import nwComment
|
||||||
|
from novelwriter.text.patterns import REGEX_PATTERNS
|
||||||
from novelwriter.types import QRegExUnicode
|
from novelwriter.types import QRegExUnicode
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -155,54 +156,35 @@ class GuiDocHighlighter(QSyntaxHighlighter):
|
|||||||
|
|
||||||
# Dialogue
|
# Dialogue
|
||||||
if CONFIG.dialogStyle > 0:
|
if CONFIG.dialogStyle > 0:
|
||||||
symO = ""
|
rxRule = REGEX_PATTERNS.dialogStyle
|
||||||
symC = ""
|
|
||||||
if CONFIG.dialogStyle in (1, 3):
|
|
||||||
symO += CONFIG.fmtSQuoteOpen
|
|
||||||
symC += CONFIG.fmtSQuoteClose
|
|
||||||
if CONFIG.dialogStyle in (2, 3):
|
|
||||||
symO += CONFIG.fmtDQuoteOpen
|
|
||||||
symC += CONFIG.fmtDQuoteClose
|
|
||||||
|
|
||||||
rxEnd = "|$" if CONFIG.allowOpenDial else ""
|
|
||||||
rxRule = QRegularExpression(f"\\B[{symO}].*?[{symC}]\\B{rxEnd}")
|
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
0: self._hStyles["dialog"],
|
0: self._hStyles["dialog"],
|
||||||
}
|
}
|
||||||
self._txtRules.append((rxRule, hlRule))
|
self._txtRules.append((rxRule, hlRule))
|
||||||
|
|
||||||
if CONFIG.dialogLine:
|
if CONFIG.dialogLine:
|
||||||
sym = QRegularExpression.escape(CONFIG.dialogLine)
|
rxRule = REGEX_PATTERNS.dialogLine
|
||||||
rxRule = QRegularExpression(f"^{sym}.*?$")
|
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
0: self._hStyles["dialog"],
|
0: self._hStyles["dialog"],
|
||||||
}
|
}
|
||||||
self._txtRules.append((rxRule, hlRule))
|
self._txtRules.append((rxRule, hlRule))
|
||||||
|
|
||||||
if CONFIG.narratorBreak:
|
if CONFIG.narratorBreak:
|
||||||
sym = QRegularExpression.escape(CONFIG.narratorBreak)
|
rxRule = REGEX_PATTERNS.narratorBreak
|
||||||
rxRule = QRegularExpression(f"({sym}\\b)(.*?)(\\b{sym})")
|
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
0: self._hStyles["text"],
|
0: self._hStyles["text"],
|
||||||
}
|
}
|
||||||
self._txtRules.append((rxRule, hlRule))
|
self._txtRules.append((rxRule, hlRule))
|
||||||
|
|
||||||
if CONFIG.altDialogOpen and CONFIG.altDialogClose:
|
if CONFIG.altDialogOpen and CONFIG.altDialogClose:
|
||||||
symO = QRegularExpression.escape(CONFIG.altDialogOpen)
|
rxRule = REGEX_PATTERNS.altDialogStyle
|
||||||
symC = QRegularExpression.escape(CONFIG.altDialogClose)
|
|
||||||
rxRule = QRegularExpression(f"\\B{symO}.*?{symC}\\B")
|
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
0: self._hStyles["altdialog"],
|
0: self._hStyles["altdialog"],
|
||||||
}
|
}
|
||||||
self._txtRules.append((rxRule, hlRule))
|
self._txtRules.append((rxRule, hlRule))
|
||||||
|
|
||||||
# Markdown Italic
|
# Markdown Italic
|
||||||
rxRule = QRegularExpression(nwRegEx.FMT_EI)
|
rxRule = REGEX_PATTERNS.markdownItalic
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
1: self._hStyles["markup"],
|
1: self._hStyles["markup"],
|
||||||
2: self._hStyles["italic"],
|
2: self._hStyles["italic"],
|
||||||
@@ -213,8 +195,7 @@ class GuiDocHighlighter(QSyntaxHighlighter):
|
|||||||
self._cmnRules.append((rxRule, hlRule))
|
self._cmnRules.append((rxRule, hlRule))
|
||||||
|
|
||||||
# Markdown Bold
|
# Markdown Bold
|
||||||
rxRule = QRegularExpression(nwRegEx.FMT_EB)
|
rxRule = REGEX_PATTERNS.markdownBold
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
1: self._hStyles["markup"],
|
1: self._hStyles["markup"],
|
||||||
2: self._hStyles["bold"],
|
2: self._hStyles["bold"],
|
||||||
@@ -225,8 +206,7 @@ class GuiDocHighlighter(QSyntaxHighlighter):
|
|||||||
self._cmnRules.append((rxRule, hlRule))
|
self._cmnRules.append((rxRule, hlRule))
|
||||||
|
|
||||||
# Markdown Strikethrough
|
# Markdown Strikethrough
|
||||||
rxRule = QRegularExpression(nwRegEx.FMT_ST)
|
rxRule = REGEX_PATTERNS.markdownStrike
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
1: self._hStyles["markup"],
|
1: self._hStyles["markup"],
|
||||||
2: self._hStyles["strike"],
|
2: self._hStyles["strike"],
|
||||||
@@ -237,8 +217,7 @@ class GuiDocHighlighter(QSyntaxHighlighter):
|
|||||||
self._cmnRules.append((rxRule, hlRule))
|
self._cmnRules.append((rxRule, hlRule))
|
||||||
|
|
||||||
# Shortcodes
|
# Shortcodes
|
||||||
rxRule = QRegularExpression(nwRegEx.FMT_SC)
|
rxRule = REGEX_PATTERNS.shortcodePlain
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
1: self._hStyles["code"],
|
1: self._hStyles["code"],
|
||||||
}
|
}
|
||||||
@@ -247,8 +226,7 @@ class GuiDocHighlighter(QSyntaxHighlighter):
|
|||||||
self._cmnRules.append((rxRule, hlRule))
|
self._cmnRules.append((rxRule, hlRule))
|
||||||
|
|
||||||
# Shortcodes w/Value
|
# Shortcodes w/Value
|
||||||
rxRule = QRegularExpression(nwRegEx.FMT_SV)
|
rxRule = REGEX_PATTERNS.shortcodeValue
|
||||||
rxRule.setPatternOptions(QRegExUnicode)
|
|
||||||
hlRule = {
|
hlRule = {
|
||||||
1: self._hStyles["code"],
|
1: self._hStyles["code"],
|
||||||
2: self._hStyles["value"],
|
2: self._hStyles["value"],
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
"""
|
||||||
|
novelWriter – Text Pattern Functions
|
||||||
|
====================================
|
||||||
|
|
||||||
|
File History:
|
||||||
|
Created: 2024-06-01 [2.5ec1]
|
||||||
|
|
||||||
|
This file is a part of novelWriter
|
||||||
|
Copyright 2018–2024, Veronica Berglyd Olsen
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but
|
||||||
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from PyQt5.QtCore import QRegularExpression
|
||||||
|
|
||||||
|
from novelwriter import CONFIG
|
||||||
|
from novelwriter.constants import nwRegEx
|
||||||
|
from novelwriter.types import QRegExUnicode
|
||||||
|
|
||||||
|
|
||||||
|
class RegExPatterns:
|
||||||
|
|
||||||
|
@property
|
||||||
|
def markdownItalic(self) -> QRegularExpression:
|
||||||
|
"""Markdown italic style."""
|
||||||
|
rxRule = QRegularExpression(nwRegEx.FMT_EI)
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
@property
|
||||||
|
def markdownBold(self) -> QRegularExpression:
|
||||||
|
"""Markdown bold style."""
|
||||||
|
rxRule = QRegularExpression(nwRegEx.FMT_EB)
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
@property
|
||||||
|
def markdownStrike(self) -> QRegularExpression:
|
||||||
|
"""Markdown strikethrough style."""
|
||||||
|
rxRule = QRegularExpression(nwRegEx.FMT_ST)
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
@property
|
||||||
|
def shortcodePlain(self) -> QRegularExpression:
|
||||||
|
"""Plain shortcode style."""
|
||||||
|
rxRule = QRegularExpression(nwRegEx.FMT_SC)
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
@property
|
||||||
|
def shortcodeValue(self) -> QRegularExpression:
|
||||||
|
"""Plain shortcode style."""
|
||||||
|
rxRule = QRegularExpression(nwRegEx.FMT_SV)
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
@property
|
||||||
|
def dialogStyle(self) -> QRegularExpression:
|
||||||
|
"""Dialogue detection rule based on user settings."""
|
||||||
|
symO = ""
|
||||||
|
symC = ""
|
||||||
|
if CONFIG.dialogStyle in (1, 3):
|
||||||
|
symO += CONFIG.fmtSQuoteOpen
|
||||||
|
symC += CONFIG.fmtSQuoteClose
|
||||||
|
if CONFIG.dialogStyle in (2, 3):
|
||||||
|
symO += CONFIG.fmtDQuoteOpen
|
||||||
|
symC += CONFIG.fmtDQuoteClose
|
||||||
|
|
||||||
|
rxEnd = "|$" if CONFIG.allowOpenDial else ""
|
||||||
|
rxRule = QRegularExpression(f"\\B[{symO}].*?[{symC}]\\B{rxEnd}")
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
@property
|
||||||
|
def dialogLine(self) -> QRegularExpression:
|
||||||
|
"""Dialogue line rule based on user settings."""
|
||||||
|
sym = QRegularExpression.escape(CONFIG.dialogLine)
|
||||||
|
rxRule = QRegularExpression(f"^{sym}.*?$")
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
@property
|
||||||
|
def narratorBreak(self) -> QRegularExpression:
|
||||||
|
"""Dialogue narrator break rule based on user settings."""
|
||||||
|
sym = QRegularExpression.escape(CONFIG.narratorBreak)
|
||||||
|
rxRule = QRegularExpression(f"({sym}\\b)(.*?)(\\b{sym})")
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
@property
|
||||||
|
def altDialogStyle(self) -> QRegularExpression:
|
||||||
|
"""Dialogue alternative rule based on user settings."""
|
||||||
|
symO = QRegularExpression.escape(CONFIG.altDialogOpen)
|
||||||
|
symC = QRegularExpression.escape(CONFIG.altDialogClose)
|
||||||
|
rxRule = QRegularExpression(f"\\B{symO}.*?{symC}\\B")
|
||||||
|
rxRule.setPatternOptions(QRegExUnicode)
|
||||||
|
return rxRule
|
||||||
|
|
||||||
|
|
||||||
|
REGEX_PATTERNS = RegExPatterns()
|
||||||
Reference in New Issue
Block a user