From 3a6b0016b0aaa2f52c326bf9405abdd40bbdcb4e Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 28 Jul 2020 08:22:38 -0400 Subject: internal/source: simplify pattern specification Allow regexps to be specified as strings. Change-Id: Icad485e0ec798a99691c002754bc104526e48a84 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/245257 Reviewed-by: Julie Qiu --- internal/source/source.go | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'internal/source/source.go') diff --git a/internal/source/source.go b/internal/source/source.go index 0a18ebac..11b49ec9 100644 --- a/internal/source/source.go +++ b/internal/source/source.go @@ -425,33 +425,34 @@ func removeVersionSuffix(s string) string { // URLs. Each regexp must match a prefix of the target string, and must have a // group named "repo". var patterns = []struct { - re *regexp.Regexp + pattern string // uncompiled regexp templates urlTemplates + re *regexp.Regexp }{ { - regexp.MustCompile(`^(?Pgithub\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`), - githubURLTemplates, + pattern: `^(?Pgithub\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`, + templates: githubURLTemplates, }, { - regexp.MustCompile(`^(?Pbitbucket\.org/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`), - bitbucketURLTemplates, + pattern: `^(?Pbitbucket\.org/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`, + templates: bitbucketURLTemplates, }, { - regexp.MustCompile(`^(?Pgitlab\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`), - githubURLTemplates, + pattern: `^(?Pgitlab\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`, + templates: githubURLTemplates, }, { // Assume that any site beginning "gitlab." works like gitlab.com. - regexp.MustCompile(`^(?Pgitlab\.[a-z0-9A-Z.-]+/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`), - githubURLTemplates, + pattern: `^(?Pgitlab\.[a-z0-9A-Z.-]+/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`, + templates: githubURLTemplates, }, { - regexp.MustCompile(`^(?Pgitee\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`), - githubURLTemplates, + pattern: `^(?Pgitee\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`, + templates: githubURLTemplates, }, { - regexp.MustCompile(`^(?Pgit\.sr\.ht/~[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`), - urlTemplates{ + pattern: `^(?Pgit\.sr\.ht/~[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`, + templates: urlTemplates{ Directory: "{repo}/tree/{commit}/{dir}", File: "{repo}/tree/{commit}/{file}", Line: "{repo}/tree/{commit}/{file}#L{line}", @@ -462,8 +463,8 @@ var patterns = []struct { // a ".git" repo suffix in an import path. If matching a repo URL from a meta tag, // there is no ".git". { - regexp.MustCompile(`^(?P[^.]+\.googlesource\.com/[^.]+)(\.git|$)`), - urlTemplates{ + pattern: `^(?P[^.]+\.googlesource\.com/[^.]+)(\.git|$)`, + templates: urlTemplates{ Directory: "{repo}/+/{commit}/{dir}", File: "{repo}/+/{commit}/{file}", Line: "{repo}/+/{commit}/{file}#{line}", @@ -471,30 +472,32 @@ var patterns = []struct { }, }, { - regexp.MustCompile(`^(?Pgit\.apache\.org/[^.]+)(\.git|$)`), - githubURLTemplates, + pattern: `^(?Pgit\.apache\.org/[^.]+)(\.git|$)`, + templates: githubURLTemplates, }, // General syntax for the go command. We can extract the repo and directory, but // we don't know the URL templates. // Must be last in this list. { - regexp.MustCompile(`(?P([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?(/~?[A-Za-z0-9_.\-]+)+?)\.(bzr|fossil|git|hg|svn)`), - urlTemplates{}, + pattern: `(?P([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?(/~?[A-Za-z0-9_.\-]+)+?)\.(bzr|fossil|git|hg|svn)`, + templates: urlTemplates{}, }, } func init() { - for _, p := range patterns { + for i := range patterns { + re := regexp.MustCompile(patterns[i].pattern) found := false - for _, n := range p.re.SubexpNames() { + for _, n := range re.SubexpNames() { if n == "repo" { found = true break } } if !found { - panic(fmt.Sprintf("pattern %s missing group", p.re)) + panic(fmt.Sprintf("pattern %s missing group", patterns[i].pattern)) } + patterns[i].re = re } } -- cgit v1.3-5-g9baa