Skip to content

Commit

Permalink
Updated the symmetric regexp matching
Browse files Browse the repository at this point in the history
Signed-off-by: Sandy <sandy@sandyuraz.com>
  • Loading branch information
thecsw committed Dec 26, 2022
1 parent 59db732 commit bc99eaf
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions yunyun/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ var (

// BuildRegex uses patterns from `ActiveMarkings` to build Yunyun's regexes.
func (m Markings) BuildRegex() {
BoldText = MarkupRegex(m.Bold)
ItalicText = MarkupRegex(m.Italic)
BoldText = SymmetricEmphasis(m.Bold)
ItalicText = SymmetricEmphasis(m.Italic)
BoldItalicTextBegin = regexp.MustCompile(`(?mU)(^|[ ()_%<>])` + m.Bold + m.Italic)
BoldItalicTextEnd = regexp.MustCompile(`(?mU)` + m.Italic + m.Bold + `($|[ (),.!?;&_%><])`)
VerbatimText = MarkupRegex(m.Verbatim)
StrikethroughText = MarkupRegex(m.Strikethrough)
UnderlineText = MarkupRegex(m.Underline)
VerbatimText = SymmetricEmphasis(m.Verbatim)
StrikethroughText = SymmetricEmphasis(m.Strikethrough)
UnderlineText = SymmetricEmphasis(m.Underline)

LinkRegexp = regexp.MustCompile(m.Link)
linkLinkIndex = LinkRegexp.SubexpIndex("link")
Expand Down Expand Up @@ -154,10 +154,27 @@ func RemoveFormatting(what string) string {
return what
}

// MarkupRegex is a useful tool to create simple text markups.
func MarkupRegex(delimeter string) *regexp.Regexp {
return regexp.MustCompile(
`(?mU)(?P<l>^|[ ()\[\]_%>β€œβ€])` + delimeter +
`(?P<text>\S|\S\S|\S.+\S)` + delimeter +
`(?P<r>$|[ ()\[\],.!?:;&_%<β€œβ€])`)
const (
// darknessPunct is our altornative to [[:punct:]] re2 class.
darknessPunctLeft = `[!(\[%><&"'β€œβ€žβ€˜{]`
darknessPunctRight = `[!()\[\]%><&'"β€œβ€β€žβ€˜β€™;:?.,{}]`
)

// SymmetricEmphasis is a useful tool to create simple text markups.
func SymmetricEmphasis(delimeter string) *regexp.Regexp {
return AsymmetricEmphasis(delimeter, delimeter)
}

// AsymmetricEmphasis returns regexp with asymmetric borders.
func AsymmetricEmphasis(left, right string) *regexp.Regexp {
return regexp.MustCompile(emphasisPattern(left, right))
}

// emphasisPattern returns pattern given left and right delimeters.
func emphasisPattern(left, right string) string {
return `(?m)` +
`(?P<l>[[:space:]]|` + darknessPunctLeft + `|^)` +
left +
`(?P<text>(?:[^ \n\t]|[^ \n\t](?:.|\n[^\n])*?[^ ]))` +
right + `(?P<r>[[:space:]]|` + darknessPunctRight + `|$)`
}

0 comments on commit bc99eaf

Please sign in to comment.