Add URL regex pattern
This commit is contained in:
@@ -60,6 +60,7 @@ class nwConst:
|
||||
|
||||
class nwRegEx:
|
||||
|
||||
URL = r"https?://(?:www\.|(?!www))[\w/()@:%_\+-.~#?&=]+"
|
||||
WORDS = r"\b[^\s\-\+\/–—\[\]:]+\b"
|
||||
BREAK = r"(?i)(?<!\\)(\[br\]\n?)"
|
||||
FMT_EI = r"(?<![\w\\])(_)(?![\s_])(.+?)(?<![\s\\])(\1)(?!\w)"
|
||||
|
||||
@@ -32,6 +32,7 @@ from novelwriter.constants import nwRegEx
|
||||
class RegExPatterns:
|
||||
|
||||
# Static RegExes
|
||||
_rxUrl = re.compile(nwRegEx.URL, re.ASCII)
|
||||
_rxWords = re.compile(nwRegEx.WORDS, re.UNICODE)
|
||||
_rxBreak = re.compile(nwRegEx.BREAK, re.UNICODE)
|
||||
_rxItalic = re.compile(nwRegEx.FMT_EI, re.UNICODE)
|
||||
@@ -40,6 +41,11 @@ class RegExPatterns:
|
||||
_rxSCPlain = re.compile(nwRegEx.FMT_SC, re.UNICODE)
|
||||
_rxSCValue = re.compile(nwRegEx.FMT_SV, re.UNICODE)
|
||||
|
||||
@property
|
||||
def url(self) -> re.Pattern:
|
||||
"""Find URLs."""
|
||||
return self._rxUrl
|
||||
|
||||
@property
|
||||
def wordSplit(self) -> re.Pattern:
|
||||
"""Split text into words."""
|
||||
|
||||
@@ -40,6 +40,48 @@ def allMatches(regEx: re.Pattern, text: str) -> list[list[str]]:
|
||||
return result
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testTextPatterns_Urls():
|
||||
"""Test the URL regex."""
|
||||
regEx = REGEX_PATTERNS.url
|
||||
|
||||
valid = [
|
||||
"http://example.com",
|
||||
"http://example.com/",
|
||||
"http://example.com/path+to+page",
|
||||
"http://example.com/path-to-page",
|
||||
"http://example.com/path_to_page",
|
||||
"http://example.com/path~to~page",
|
||||
"http://example.com/path/to/page",
|
||||
"http://example.com/path/to/page.html",
|
||||
"http://example.com/path/to/page.html#title",
|
||||
"http://example.com/path/to/page.html#title%20here",
|
||||
"http://example.com/path/to/page.html#title%20here",
|
||||
"http://example.com/path/to/page?foo=bar&bar=baz",
|
||||
"http://example.com/path/to/page.html?foo=bar&bar=baz",
|
||||
"http://example.com/path/to/page.html#title?foo=bar&bar=baz",
|
||||
"http://user:password@example.com/",
|
||||
"http://www.example.com/",
|
||||
"http://www.www.example.com/",
|
||||
"http://www.www.www.example.com/",
|
||||
"https://example.com",
|
||||
"https://www.example.com/",
|
||||
]
|
||||
invalid = [
|
||||
"hppt://example.com/",
|
||||
"sftp://example.com/",
|
||||
"http:/example.com/",
|
||||
"http://www example com/",
|
||||
"http://www\texample\tcom/",
|
||||
]
|
||||
|
||||
for test in valid:
|
||||
assert allMatches(regEx, f"Text {test} more text") == [[(test, 5, 5 + len(test))]]
|
||||
|
||||
for test in invalid:
|
||||
assert allMatches(regEx, f"Text {test} more text") == []
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testTextPatterns_Words():
|
||||
"""Test the word split regex."""
|
||||
@@ -198,8 +240,10 @@ def testTextPatterns_ShortcodesPlain():
|
||||
|
||||
assert allMatches(regEx, "one [x]two[/x] three") == []
|
||||
|
||||
# Line Break Substitution
|
||||
# =======================
|
||||
|
||||
@pytest.mark.core
|
||||
def testTextPatterns_LineBreakReplace():
|
||||
"""Test replacing forced line breaks."""
|
||||
regEx = REGEX_PATTERNS.lineBreak
|
||||
|
||||
assert regEx.sub("\n", "one[br]two") == "one\ntwo"
|
||||
|
||||
Reference in New Issue
Block a user