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 # Match Markdown
for regEx, fmts in self._rxMarkdown: for regEx, fmts in self._rxMarkdown:
rxItt = regEx.globalMatch(text, 0) for match in re.finditer(regEx, text):
while rxItt.hasNext():
rxMatch = rxItt.next()
temp.extend( 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 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.core.index import processComment
from novelwriter.enum import nwComment from novelwriter.enum import nwComment
from novelwriter.text.patterns import REGEX_PATTERNS from novelwriter.text.patterns import REGEX_PATTERNS
from novelwriter.types import QRegExUnicode
logger = logging.getLogger(__name__) 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 from __future__ import annotations
import re
from PyQt5.QtCore import QRegularExpression from PyQt5.QtCore import QRegularExpression
from novelwriter import CONFIG from novelwriter import CONFIG
@@ -33,25 +35,19 @@ from novelwriter.types import QRegExUnicode
class RegExPatterns: class RegExPatterns:
@property @property
def markdownItalic(self) -> QRegularExpression: def markdownItalic(self) -> re.Pattern:
"""Markdown italic style.""" """Markdown italic style."""
rxRule = QRegularExpression(nwRegEx.FMT_EI) return re.compile(nwRegEx.FMT_EI, re.UNICODE)
rxRule.setPatternOptions(QRegExUnicode)
return rxRule
@property @property
def markdownBold(self) -> QRegularExpression: def markdownBold(self) -> re.Pattern:
"""Markdown bold style.""" """Markdown bold style."""
rxRule = QRegularExpression(nwRegEx.FMT_EB) return re.compile(nwRegEx.FMT_EB, re.UNICODE)
rxRule.setPatternOptions(QRegExUnicode)
return rxRule
@property @property
def markdownStrike(self) -> QRegularExpression: def markdownStrike(self) -> re.Pattern:
"""Markdown strikethrough style.""" """Markdown strikethrough style."""
rxRule = QRegularExpression(nwRegEx.FMT_ST) return re.compile(nwRegEx.FMT_ST, re.UNICODE)
rxRule.setPatternOptions(QRegExUnicode)
return rxRule
@property @property
def shortcodePlain(self) -> QRegularExpression: 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 from __future__ import annotations
import re
import pytest import pytest
from PyQt5.QtCore import QRegularExpression 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]]: def allMatches(regEx: QRegularExpression, text: str) -> list[list[str]]:
"""Get all matches for a regex.""" """Get all matches for a regex."""
result = [] result = []
itt = regEx.globalMatch(text, 0) if isinstance(regEx, QRegularExpression):
while itt.hasNext(): itt = regEx.globalMatch(text, 0)
match = itt.next() while itt.hasNext():
result.append([ match = itt.next()
(match.captured(n), match.capturedStart(n), match.capturedEnd(n)) result.append([
for n in range(match.lastCapturedIndex() + 1) (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 return result