diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-01-29 19:36:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-29 19:36:57 +0800 |
commit | b28fb9085185025415847d7075f1cdf83abb3b56 (patch) | |
tree | 0e0cc62b67546ebcd35c297cd35f097fe486b445 /internal/markup | |
parent | 8b7fa6627fb0f75adb8d1ca4ad953cf3b9b7dc87 (diff) |
all: use lazyregexp (#5911)
* Use lazyregexp
* all: fix imports and usages
Diffstat (limited to 'internal/markup')
-rw-r--r-- | internal/markup/markdown.go | 4 | ||||
-rw-r--r-- | internal/markup/markup.go | 16 | ||||
-rw-r--r-- | internal/markup/sanitizer.go | 6 |
3 files changed, 13 insertions, 13 deletions
diff --git a/internal/markup/markdown.go b/internal/markup/markdown.go index a5380028..6606d2b5 100644 --- a/internal/markup/markdown.go +++ b/internal/markup/markdown.go @@ -9,11 +9,11 @@ import ( "fmt" "path" "path/filepath" - "regexp" "strings" "github.com/russross/blackfriday" + "gogs.io/gogs/internal/lazyregexp" "gogs.io/gogs/internal/setting" "gogs.io/gogs/internal/tool" ) @@ -35,7 +35,7 @@ type MarkdownRenderer struct { urlPrefix string } -var validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://|^mailto:`) +var validLinksPattern = lazyregexp.New(`^[a-z][\w-]+://|^mailto:`) // isLink reports whether link fits valid format. func isLink(link []byte) bool { diff --git a/internal/markup/markup.go b/internal/markup/markup.go index 1a22daae..2bb56603 100644 --- a/internal/markup/markup.go +++ b/internal/markup/markup.go @@ -8,12 +8,12 @@ import ( "bytes" "fmt" "io" - "regexp" "strings" "github.com/unknwon/com" "golang.org/x/net/html" + "gogs.io/gogs/internal/lazyregexp" "gogs.io/gogs/internal/setting" "gogs.io/gogs/internal/tool" ) @@ -35,26 +35,26 @@ const ( var ( // MentionPattern matches string that mentions someone, e.g. @Unknwon - MentionPattern = regexp.MustCompile(`(\s|^|\W)@[0-9a-zA-Z-_\.]+`) + MentionPattern = lazyregexp.New(`(\s|^|\W)@[0-9a-zA-Z-_\.]+`) // CommitPattern matches link to certain commit with or without trailing hash, // e.g. https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2 - CommitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`) + CommitPattern = lazyregexp.New(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`) // IssueFullPattern matches link to an issue with or without trailing hash, // e.g. https://try.gogs.io/gogs/gogs/issues/4#issue-685 - IssueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`) + IssueFullPattern = lazyregexp.New(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`) // IssueNumericPattern matches string that references to a numeric issue, e.g. #1287 - IssueNumericPattern = regexp.MustCompile(`( |^|\(|\[)#[0-9]+\b`) + IssueNumericPattern = lazyregexp.New(`( |^|\(|\[)#[0-9]+\b`) // IssueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234 - IssueAlphanumericPattern = regexp.MustCompile(`( |^|\(|\[)[A-Z]{1,10}-[1-9][0-9]*\b`) + IssueAlphanumericPattern = lazyregexp.New(`( |^|\(|\[)[A-Z]{1,10}-[1-9][0-9]*\b`) // CrossReferenceIssueNumericPattern matches string that references a numeric issue in a difference repository // e.g. gogs/gogs#12345 - CrossReferenceIssueNumericPattern = regexp.MustCompile(`( |^)[0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+\b`) + CrossReferenceIssueNumericPattern = lazyregexp.New(`( |^)[0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+\b`) // Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae // FIXME: this pattern matches pure numbers as well, right now we do a hack to check in RenderSha1CurrentPattern by converting string to a number. - Sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{7,40}\b`) + Sha1CurrentPattern = lazyregexp.New(`\b[0-9a-f]{7,40}\b`) ) // FindAllMentions matches mention patterns in given content diff --git a/internal/markup/sanitizer.go b/internal/markup/sanitizer.go index e8d76b23..981e1c73 100644 --- a/internal/markup/sanitizer.go +++ b/internal/markup/sanitizer.go @@ -5,11 +5,11 @@ package markup import ( - "regexp" "sync" "github.com/microcosm-cc/bluemonday" + "gogs.io/gogs/internal/lazyregexp" "gogs.io/gogs/internal/setting" ) @@ -30,10 +30,10 @@ var sanitizer = &Sanitizer{ func NewSanitizer() { sanitizer.init.Do(func() { // We only want to allow HighlightJS specific classes for code blocks - sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^language-\w+$`)).OnElements("code") + sanitizer.policy.AllowAttrs("class").Matching(lazyregexp.New(`^language-\w+$`).Regexp()).OnElements("code") // Checkboxes - sanitizer.policy.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input") + sanitizer.policy.AllowAttrs("type").Matching(lazyregexp.New(`^checkbox$`).Regexp()).OnElements("input") sanitizer.policy.AllowAttrs("checked", "disabled").OnElements("input") // Data URLs |