Browse Source

Fix tree and blob URLs for Gitea / Gogs

The directory structure for viewing branches and code on Gitea / Gogs is
different from GitHub. This fixes that, so the Source Code link on the
gopkg webpage works, as well as the URLs to lines in the source code
included in GoDoc.
Matt Baer 1 year ago
parent
commit
16294edeb5
2 changed files with 18 additions and 2 deletions
  1. 17
    1
      main.go
  2. 1
    1
      page.go

+ 17
- 1
main.go View File

@@ -123,7 +123,7 @@ var gogetTemplate = template.Must(template.New("").Parse(`
123 123
 <html>
124 124
 <head>
125 125
 <meta name="go-import" content="{{.Original.GopkgRoot}} git https://{{.Original.GopkgRoot}}">
126
-{{$root := .GitHubRoot}}{{$tree := .GitHubTree}}<meta name="go-source" content="{{.Original.GopkgRoot}} _ https://{{$root}}/tree/{{$tree}}{/dir} https://{{$root}}/blob/{{$tree}}{/dir}/{file}#L{line}">
126
+{{$root := .GitHubRoot}}{{$tree := .GitHubTree}}<meta name="go-source" content="{{.Original.GopkgRoot}} _ https://{{$root}}/{{.TreeDir}}/{{$tree}}{/dir} https://{{$root}}/{{.BlobDir}}/{{$tree}}{/dir}/{file}#L{line}">
127 127
 </head>
128 128
 <body>
129 129
 go get {{.GopkgPath}}
@@ -197,6 +197,22 @@ func (repo *Repo) GitHubRoot() string {
197 197
 	return *srcDomainFlag + "/" + repo.User + "/" + repo.Name
198 198
 }
199 199
 
200
+// TreeDir returns the repository's web tree dir
201
+func (repo *Repo) TreeDir() string {
202
+	if *srcDomainFlag != "github.com" {
203
+		return "src/branch"
204
+	}
205
+	return "tree"
206
+}
207
+
208
+// BlobDir returns the repository's dir for source files
209
+func (repo *Repo) BlobDir() string {
210
+	if *srcDomainFlag != "github.com" {
211
+		return "src/branch"
212
+	}
213
+	return "blob"
214
+}
215
+
200 216
 // GitHubTree returns the repository tree name for the selected version.
201 217
 func (repo *Repo) GitHubTree() string {
202 218
 	if repo.FullVersion == InvalidVersion {

+ 1
- 1
page.go View File

@@ -131,7 +131,7 @@ const packageTemplateString = `<!DOCTYPE html>
131 131
 				{{ end }}
132 132
 				<div class="row" >
133 133
 					<div class="col-sm-12" >
134
-						<a class="btn btn-lg btn-info" href="https://{{.Repo.GitHubRoot}}/tree/{{.Repo.GitHubTree}}{{.Repo.SubPath}}" ><i class="fa fa-github"></i> Source Code</a>
134
+						<a class="btn btn-lg btn-info" href="https://{{.Repo.GitHubRoot}}/{{.Repo.TreeDir}}/{{.Repo.GitHubTree}}{{.Repo.SubPath}}" ><i class="fa fa-github"></i> Source Code</a>
135 135
 						<a class="btn btn-lg btn-info" href="http://godoc.org/{{.Repo.GopkgPath}}" ><i class="fa fa-info-circle"></i> API Documentation</a>
136 136
 					</div>
137 137
 				</div>