Use stdlib re for Markdown matching

This commit is contained in:
Veronica Berglyd Olsen
2024-09-22 16:56:21 +02:00
parent d3799bc30a
commit c3cb12e5e2
4 changed files with 26 additions and 24 deletions
+2 -4
View File
@@ -1109,11 +1109,9 @@ class Tokenizer(ABC):
# Match Markdown
for regEx, fmts in self._rxMarkdown:
rxItt = regEx.globalMatch(text, 0)
while rxItt.hasNext():
rxMatch = rxItt.next()
for match in re.finditer(regEx, text):
temp.extend(
(rxMatch.capturedStart(n), rxMatch.capturedLength(n), fmt, "")
(match.start(n), len(match.group(n)), fmt, "")
for n, fmt in enumerate(fmts) if fmt > 0
)
-1
View File
@@ -41,7 +41,6 @@ from novelwriter.constants import nwHeaders, nwRegEx, nwUnicode
from novelwriter.core.index import processComment
from novelwriter.enum import nwComment
from novelwriter.text.patterns import REGEX_PATTERNS
from novelwriter.types import QRegExUnicode
logger = logging.getLogger(__name__)
+8 -12
View File
@@ -23,6 +23,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import re
from PyQt5.QtCore import QRegularExpression
from novelwriter import CONFIG
@@ -33,25 +35,19 @@ from novelwriter.types import QRegExUnicode
class RegExPatterns:
@property
def markdownItalic(self) -> QRegularExpression:
def markdownItalic(self) -> re.Pattern:
"""Markdown italic style."""
rxRule = QRegularExpression(nwRegEx.FMT_EI)
rxRule.setPatternOptions(QRegExUnicode)
return rxRule
return re.compile(nwRegEx.FMT_EI, re.UNICODE)
@property
def markdownBold(self) -> QRegularExpression:
def markdownBold(self) -> re.Pattern:
"""Markdown bold style."""
rxRule = QRegularExpression(nwRegEx.FMT_EB)
rxRule.setPatternOptions(QRegExUnicode)
return rxRule
return re.compile(nwRegEx.FMT_EB, re.UNICODE)
@property
def markdownStrike(self) -> QRegularExpression:
def markdownStrike(self) -> re.Pattern:
"""Markdown strikethrough style."""
rxRule = QRegularExpression(nwRegEx.FMT_ST)
rxRule.setPatternOptions(QRegExUnicode)
return rxRule
return re.compile(nwRegEx.FMT_ST, re.UNICODE)
@property
def shortcodePlain(self) -> QRegularExpression:
+16 -7
View File
@@ -20,6 +20,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import re
import pytest
from PyQt5.QtCore import QRegularExpression
@@ -32,13 +34,20 @@ from novelwriter.text.patterns import REGEX_PATTERNS
def allMatches(regEx: QRegularExpression, text: str) -> list[list[str]]:
"""Get all matches for a regex."""
result = []
itt = regEx.globalMatch(text, 0)
while itt.hasNext():
match = itt.next()
result.append([
(match.captured(n), match.capturedStart(n), match.capturedEnd(n))
for n in range(match.lastCapturedIndex() + 1)
])
if isinstance(regEx, QRegularExpression):
itt = regEx.globalMatch(text, 0)
while itt.hasNext():
match = itt.next()
result.append([
(match.captured(n), match.capturedStart(n), match.capturedEnd(n))
for n in range(match.lastCapturedIndex() + 1)
])
else:
for match in re.finditer(regEx, text):
result.append([
(match.group(n), match.start(n), match.end(n))
for n in range((match.lastindex or -1) + 1)
])
return result