Skip to content

Commit

Permalink
Add support for different github branches and configs (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzs981130 committed Mar 29, 2021
1 parent c9cbaf8 commit 0e2595d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
16 changes: 11 additions & 5 deletions generate_package.go
Expand Up @@ -53,11 +53,17 @@ Source: <a href="{{.Repository.URL}}">{{.Repository.URL}}</a><br/>
}

if strings.HasPrefix(r.URL, "https://github.com") || strings.HasPrefix(r.URL, "https://gitlab.com") {
r.Type = "git"
r.SourceURLs = sourceURLs{
Home: r.URL,
Dir: r.URL + "/tree/master{/dir}",
File: r.URL + "/blob/master{/dir}/{file}#L{line}",
if r.Type == "" {
r.Type = "git"
}
if r.SourceURLs.Home == "" {
r.SourceURLs.Home = r.URL
}
if r.SourceURLs.Dir == "" {
r.SourceURLs.Dir = r.URL + "/tree/master{/dir}"
}
if r.SourceURLs.File == "" {
r.SourceURLs.File = r.URL + "/blob/master{/dir}/{file}#L{line}"
}
}

Expand Down
46 changes: 46 additions & 0 deletions generate_package_test.go
Expand Up @@ -336,6 +336,52 @@ Home: <a href="https://pkg.go.dev/example.com/pkg1/subpkg1">https://pkg.go.dev/e
Source: <a href="https://gitlab.com/example/go-pkg1">https://gitlab.com/example/go-pkg1</a><br/>
Sub-packages:<ul><li><a href="/pkg1/subpkg1">example.com/pkg1/subpkg1</a></li><li><a href="/pkg1/subpkg2">example.com/pkg1/subpkg2</a></li></ul></div>
</body>
</html>`,
expectedErr: nil,
},
{
description: "github defaults with custom source",
domain: "example.com",
docsDomain: "",
pkg: "pkg1",
r: repository{
Prefix: "pkg1",
Subs: []sub{{Name: "subpkg1"}, {Name: "subpkg2"}},
Type: "git",
URL: "https://github.com/example/go-pkg1",
SourceURLs: sourceURLs{
Home: "https://github.com/example/go-pkg1",
Dir: "https://github.com/example/go-pkg1/tree/branch{/dir}",
File: "https://github.com/example/go-pkg1/blob/branch{/dir}/{file}#L{line}",
},
Website: website{
URL: "https://www.example.com",
},
},
expectedOut: `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>example.com/pkg1</title>
<meta name="go-import" content="example.com/pkg1 git https://github.com/example/go-pkg1">
<meta name="go-source" content="example.com/pkg1 https://github.com/example/go-pkg1 https://github.com/example/go-pkg1/tree/branch{/dir} https://github.com/example/go-pkg1/blob/branch{/dir}/{file}#L{line}">
<style>
* { font-family: sans-serif; }
body { margin-top: 0; }
.content { display: inline-block; }
code { display: block; font-family: monospace; font-size: 1em; background-color: #d5d5d5; padding: 1em; margin-bottom: 16px; }
ul { margin-top: 16px; margin-bottom: 16px; }
</style>
</head>
<body>
<div class="content">
<h2>example.com/pkg1</h2>
<code>go get example.com/pkg1</code>
<code>import "example.com/pkg1"</code>
Home: <a href="https://www.example.com">https://www.example.com</a><br/>
Source: <a href="https://github.com/example/go-pkg1">https://github.com/example/go-pkg1</a><br/>
Sub-packages:<ul><li><a href="/pkg1/subpkg1">example.com/pkg1/subpkg1</a></li><li><a href="/pkg1/subpkg2">example.com/pkg1/subpkg2</a></li></ul></div>
</body>
</html>`,
expectedErr: nil,
},
Expand Down

0 comments on commit 0e2595d

Please sign in to comment.