Skip to content

Commit

Permalink
Use RegexBuilder Reference in Captures (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolsinga committed Jun 15, 2024
1 parent 4a8d5a0 commit 785104b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions Sources/Site/Utility/String+LibrarySorting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ extension String {
}

internal var removeCommonInitialPunctuation: String {
var result = self.removeCommonInitialWords
let result = self.removeCommonInitialWords

let body = Reference(Substring.self)

let regex = Regex {
ZeroOrMore { .word.inverted }
Capture {
Capture(as: body) {
OneOrMore {
.word
Optionally {
Expand All @@ -42,8 +44,8 @@ extension String {
ZeroOrMore { .word.inverted }
}

if let match = result.wholeMatch(of: regex) {
result = String(match.output.1)
if let match = try? regex.wholeMatch(in: result) {
return String(match[body])
}
return result
}
Expand Down
10 changes: 6 additions & 4 deletions Sources/Site/Utility/String+Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ extension String {
// <a href="url">link</a>
// --->
// [link](url)
let url = Reference(Substring.self)
let link = Reference(Substring.self)

let regex = Regex {
"<a href=\""
Capture {
Capture(as: url) {
ZeroOrMore(.reluctant) {
.any
}
}
"\">"
Capture {
Capture(as: link) {
ZeroOrMore(.reluctant) {
.any
}
Expand All @@ -31,8 +34,7 @@ extension String {
.anchorsMatchLineEndings()

return self.replacing(regex) { match in
let (_, url, link) = match.output
return "[\(link)](\(url))"
return "[\(match[link])](\(match[url]))"
}
}

Expand Down

0 comments on commit 785104b

Please sign in to comment.