aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Utility
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2024-05-05 19:17:39 -0700
committerDustin Mierau <dustin@mierau.me>2024-05-05 19:17:39 -0700
commit3feb709522b38f1aa5ddedc5e1aee28597375aab (patch)
tree9df2ad849703004901261c53bcffad318b876ad4 /Hotline/Utility
parent4da73c630183e482e4ab2accef97ad54c6741bf4 (diff)
Write better URL regular expressions for our needs.
Diffstat (limited to 'Hotline/Utility')
-rw-r--r--Hotline/Utility/FoundationExtensions.swift39
-rw-r--r--Hotline/Utility/RegularExpressions.swift116
2 files changed, 133 insertions, 22 deletions
diff --git a/Hotline/Utility/FoundationExtensions.swift b/Hotline/Utility/FoundationExtensions.swift
index c154dbd..6270ac7 100644
--- a/Hotline/Utility/FoundationExtensions.swift
+++ b/Hotline/Utility/FoundationExtensions.swift
@@ -1,4 +1,5 @@
import Foundation
+import SwiftUI
enum Endianness {
case big
@@ -6,23 +7,13 @@ enum Endianness {
}
extension String {
- func convertToAttributedStringWithLinks(relaxed: Bool = false) -> AttributedString {
+ func convertToAttributedStringWithLinks() -> AttributedString {
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: self)
- let urlPattern = relaxed ?
- #"(hotline|http|https)?(://)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?"# :
- #"(hotline|http|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?"#
-
- guard let regex = try? NSRegularExpression(pattern: urlPattern, options: .caseInsensitive) else {
- return AttributedString(attributedString)
- }
-
- regex.enumerateMatches(in: self, range: NSMakeRange(0, self.count)) { (result: NSTextCheckingResult!, _, _) in
- if let newRange = Range(result.range, in: self) {
- let str = String(self[newRange])
- attributedString.addAttribute(.link, value: str, range: result.range)
- }
+ let matches = self.ranges(of: RegularExpressions.relaxedLink)
+ for match in matches {
+ attributedString.addAttribute(.link, value: self[match], range: NSRange(match, in: self))
+// attributedString.addAttribute(.underlineStyle, value: 1, range: NSRange(match, in: self))
}
-
return AttributedString(attributedString)
}
@@ -51,14 +42,18 @@ extension String {
}
}
- func convertLinksToMarkdown() -> String {
- let urlPattern = #"(hotline|http|https)?(://)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?"#
-
- guard let regex = try? NSRegularExpression(pattern: urlPattern, options: .caseInsensitive) else {
- return self
+ func convertingLinksToMarkdown() -> String {
+ var cp = String(self)
+ cp.replace(RegularExpressions.relaxedLink) { match -> String in
+ let linkText = self[match.range]
+ var injectedScheme = "https://"
+ if let _ = try? RegularExpressions.supportedLinkScheme.prefixMatch(in: linkText) {
+ injectedScheme = ""
+ }
+
+ return "[\(linkText)](\(injectedScheme)\(linkText))"
}
-
- return regex.stringByReplacingMatches(in: self, range: NSRange(location: 0, length: self.count), withTemplate: "[$0]($0)")
+ return cp
}
}
diff --git a/Hotline/Utility/RegularExpressions.swift b/Hotline/Utility/RegularExpressions.swift
index 0314dd7..0a0dda9 100644
--- a/Hotline/Utility/RegularExpressions.swift
+++ b/Hotline/Utility/RegularExpressions.swift
@@ -20,4 +20,120 @@ struct RegularExpressions {
}
}
}
+
+ static let supportedLinkScheme = Regex {
+ Anchor.startOfLine
+ ChoiceOf {
+ "hotline"
+ "http"
+ "https"
+ }
+ "://"
+ }.ignoresCase().anchorsMatchLineEndings()
+
+ static let relaxedLink = Regex {
+ ChoiceOf {
+ Anchor.startOfLine
+ Anchor.wordBoundary
+ }
+ Capture {
+ // scheme (optional)
+ Optionally {
+ ChoiceOf {
+ "hotline://"
+ "http://"
+ "https://"
+ }
+ }
+ // domain name
+ OneOrMore {
+ CharacterClass(
+ .anyOf(".-"),
+ ("a"..."z"),
+ ("0"..."9")
+ )
+ }
+ // top-level domain name
+ "."
+ ChoiceOf {
+ "com"
+ "net"
+ "org"
+ "edu"
+ "gov"
+ "mil"
+ "aero"
+ "asia"
+ "biz"
+ "cat"
+ "coop"
+ "info"
+ "int"
+ "jobs"
+ "mobi"
+ "museum"
+ "name"
+ "pizza"
+ "post"
+ "pro"
+ "red"
+ "tel"
+ "today"
+ "travel"
+ "ai"
+ "be"
+ "by"
+ "ca"
+ "co"
+ "de"
+ "er"
+ "es"
+ "fr"
+ "gs"
+ "ie"
+ "im"
+ "in"
+ "io"
+ "is"
+ "it"
+ "jp"
+ "la"
+ "ly"
+ "ma"
+ "md"
+ "me"
+ "my"
+ "nl"
+ "ps"
+ "pt"
+ "ja"
+ "st"
+ "to"
+ "tv"
+ "uk"
+ "ws"
+ }
+ // Port
+ Optionally {
+ ":"
+ OneOrMore {
+ CharacterClass(.digit)
+ }
+ }
+ // path
+ ZeroOrMore {
+ CharacterClass(
+ .anyOf("#_-/.?=&%\\()[]"),
+ ("a"..."z"),
+ ("0"..."9")
+ )
+ }
+ }
+ ChoiceOf {
+ Anchor.endOfLine
+ Anchor.wordBoundary
+ }
+ }
+ .anchorsMatchLineEndings()
+ .ignoresCase()
}