From 7a3a9e4e5705517f272c613b6b5fa22f34dbd43e Mon Sep 17 00:00:00 2001 From: Aaron Crickenberger Date: Sun, 7 Mar 2021 23:49:54 -0500 Subject: generator: add githubURL, orgRepoPath funcs --- generator/app.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'generator/app.go') diff --git a/generator/app.go b/generator/app.go index e48a123b..a73dc7d2 100644 --- a/generator/app.go +++ b/generator/app.go @@ -23,6 +23,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "sort" "strings" "text/template" @@ -47,6 +48,9 @@ const ( endCustomMarkdown = "" beginCustomYaml = "## BEGIN CUSTOM CONTENT" endCustomYaml = "## END CUSTOM CONTENT" + + regexRawGitHubURL = "https://raw.githubusercontent.com/(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/(?P.*)" + regexGitHubURL = "https://github.com/(?P[^/]+)/(?P[^/]+)/(blob|tree)/(?P[^/]+)/(?P.*)" ) var ( @@ -369,6 +373,37 @@ func getExistingContent(path string, fileFormat string) (string, error) { var funcMap = template.FuncMap{ "tzUrlEncode": tzURLEncode, "trimSpace": strings.TrimSpace, + "githubURL": githubURL, + "orgRepoPath": orgRepoPath, +} + +// githubURL converts a raw GitHub url (links directly to file contents) into a +// regular GitHub url (links to Code view for file), otherwise returns url untouched +func githubURL(url string) string { + re := regexp.MustCompile(regexRawGitHubURL) + mat := re.FindStringSubmatchIndex(url) + if mat == nil { + return url + } + result := re.ExpandString([]byte{}, "https://github.com/${org}/${repo}/blob/${branch}/${path}", url, mat) + return string(result) +} + +// orgRepoPath converts either +// - a regular GitHub url of form https://github.com/org/repo/blob/branch/path/to/file +// - a raw GitHub url of form https://raw.githubusercontent.com/org/repo/branch/path/to/file +// to a string of form 'org/repo/path/to/file' +func orgRepoPath(url string) string { + for _, regex := range []string{regexRawGitHubURL, regexGitHubURL} { + re := regexp.MustCompile(regex) + mat := re.FindStringSubmatchIndex(url) + if mat == nil { + continue + } + result := re.ExpandString([]byte{}, "${org}/${repo}/${path}", url, mat) + return string(result) + } + return url } // tzUrlEncode returns a url encoded string without the + shortcut. This is @@ -525,6 +560,7 @@ func main() { log.Fatal(err) } + fmt.Println("Generating group READMEs") for prefix, groups := range ctx.PrefixToGroupMap() { err = createGroupReadme(groups, prefix) if err != nil { -- cgit v1.2.3