Change regexes to use the pattern object directly
This commit is contained in:
@@ -343,7 +343,7 @@ class DocSearch:
|
|||||||
count = 0
|
count = 0
|
||||||
capped = False
|
capped = False
|
||||||
results = []
|
results = []
|
||||||
for match in re.finditer(self._regEx, text):
|
for match in self._regEx.finditer(text):
|
||||||
pos = match.start(0)
|
pos = match.start(0)
|
||||||
num = len(match.group(0))
|
num = len(match.group(0))
|
||||||
lim = text[:pos].rfind("\n") + 1
|
lim = text[:pos].rfind("\n") + 1
|
||||||
|
|||||||
@@ -1109,14 +1109,14 @@ class Tokenizer(ABC):
|
|||||||
|
|
||||||
# Match Markdown
|
# Match Markdown
|
||||||
for regEx, fmts in self._rxMarkdown:
|
for regEx, fmts in self._rxMarkdown:
|
||||||
for match in re.finditer(regEx, text):
|
for match in regEx.finditer(text):
|
||||||
temp.extend(
|
temp.extend(
|
||||||
(match.start(n), match.end(n), fmt, "")
|
(match.start(n), match.end(n), fmt, "")
|
||||||
for n, fmt in enumerate(fmts) if fmt > 0
|
for n, fmt in enumerate(fmts) if fmt > 0
|
||||||
)
|
)
|
||||||
|
|
||||||
# Match Shortcodes
|
# Match Shortcodes
|
||||||
for match in re.finditer(REGEX_PATTERNS.shortcodePlain, text):
|
for match in REGEX_PATTERNS.shortcodePlain.finditer(text):
|
||||||
temp.append((
|
temp.append((
|
||||||
match.start(1), match.end(1),
|
match.start(1), match.end(1),
|
||||||
self._shortCodeFmt.get(match.group(1).lower(), 0),
|
self._shortCodeFmt.get(match.group(1).lower(), 0),
|
||||||
@@ -1125,7 +1125,7 @@ class Tokenizer(ABC):
|
|||||||
|
|
||||||
# Match Shortcode w/Values
|
# Match Shortcode w/Values
|
||||||
tHandle = self._handle or ""
|
tHandle = self._handle or ""
|
||||||
for match in re.finditer(REGEX_PATTERNS.shortcodeValue, text):
|
for match in REGEX_PATTERNS.shortcodeValue.finditer(text):
|
||||||
kind = self._shortCodeVals.get(match.group(1).lower(), 0)
|
kind = self._shortCodeVals.get(match.group(1).lower(), 0)
|
||||||
temp.append((
|
temp.append((
|
||||||
match.start(0), match.end(0),
|
match.start(0), match.end(0),
|
||||||
@@ -1136,7 +1136,7 @@ class Tokenizer(ABC):
|
|||||||
# Match Dialogue
|
# Match Dialogue
|
||||||
if self._rxDialogue and hDialog:
|
if self._rxDialogue and hDialog:
|
||||||
for regEx, fmtB, fmtE in self._rxDialogue:
|
for regEx, fmtB, fmtE in self._rxDialogue:
|
||||||
for match in re.finditer(regEx, text):
|
for match in regEx.finditer(text):
|
||||||
temp.append((match.start(0), 0, fmtB, ""))
|
temp.append((match.start(0), 0, fmtB, ""))
|
||||||
temp.append((match.end(0), 0, fmtE, ""))
|
temp.append((match.end(0), 0, fmtE, ""))
|
||||||
|
|
||||||
|
|||||||
@@ -483,20 +483,19 @@ class TextBlockData(QTextBlockUserData):
|
|||||||
"""
|
"""
|
||||||
if "[" in text:
|
if "[" in text:
|
||||||
# Strip shortcodes
|
# Strip shortcodes
|
||||||
for rX in [RX_FMT_SC, RX_FMT_SV]:
|
for regEx in [RX_FMT_SC, RX_FMT_SV]:
|
||||||
for match in re.finditer(rX, text[offset:]):
|
for match in regEx.finditer(text, offset):
|
||||||
iS = match.start(0) + offset
|
if (s := match.start(0)) >= 0 and (e := match.end(0)) >= 0:
|
||||||
iE = match.end(0) + offset
|
pad = " "*(e - s)
|
||||||
if iS >= 0 and iE >= 0:
|
text = f"{text[:s]}{pad}{text[e:]}"
|
||||||
text = text[:iS] + " "*(iE - iS) + text[iE:]
|
|
||||||
|
|
||||||
self._spellErrors = []
|
self._spellErrors = []
|
||||||
checker = SHARED.spelling
|
checker = SHARED.spelling
|
||||||
for match in re.finditer(RX_WORDS, text[offset:].replace("_", " ")):
|
for match in RX_WORDS.finditer(text.replace("_", " ")):
|
||||||
if (
|
if (
|
||||||
(word := match.group(0))
|
(word := match.group(0))
|
||||||
and not (word.isnumeric() or word.isupper() or checker.checkWord(word))
|
and not (word.isnumeric() or word.isupper() or checker.checkWord(word))
|
||||||
):
|
):
|
||||||
self._spellErrors.append((match.start(0) + offset, match.end(0) + offset))
|
self._spellErrors.append((match.start(0), match.end(0)))
|
||||||
|
|
||||||
return self._spellErrors
|
return self._spellErrors
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ from novelwriter.text.patterns import REGEX_PATTERNS
|
|||||||
def allMatches(regEx: re.Pattern, text: str) -> list[list[str]]:
|
def allMatches(regEx: re.Pattern, text: str) -> list[list[str]]:
|
||||||
"""Get all matches for a regex."""
|
"""Get all matches for a regex."""
|
||||||
result = []
|
result = []
|
||||||
for match in re.finditer(regEx, text):
|
for match in regEx.finditer(text):
|
||||||
result.append([
|
result.append([
|
||||||
(match.group(n), match.start(n), match.end(n))
|
(match.group(n), match.start(n), match.end(n))
|
||||||
for n in range((match.lastindex or 0) + 1)
|
for n in range((match.lastindex or 0) + 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user