New godoc.org's go-source support by Gary Burd.
This also includes further tweaks to unify the handling of FullVersion across the go-get and the package page rendering.
This commit is contained in:
commit
65998c5554
33
main.go
33
main.go
@ -59,6 +59,7 @@ var gogetTemplate = template.Must(template.New("").Parse(`
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="go-import" content="{{.GopkgRoot}} git https://{{.GopkgRoot}}">
|
<meta name="go-import" content="{{.GopkgRoot}} git https://{{.GopkgRoot}}">
|
||||||
|
{{$root := .GitHubRoot}}{{$tree := .GitHubTree}}<meta name="go-source" content="{{.GopkgRoot}} _ https://{{$root}}/tree/{{$tree}}{/dir} https://{{$root}}/blob/{{$tree}}{/dir}/{file}#L{line}">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
go get {{.GopkgPath}}
|
go get {{.GopkgPath}}
|
||||||
@ -73,9 +74,28 @@ type Repo struct {
|
|||||||
SubPath string
|
SubPath string
|
||||||
OldFormat bool // The old /v2/pkg format.
|
OldFormat bool // The old /v2/pkg format.
|
||||||
MajorVersion Version
|
MajorVersion Version
|
||||||
|
|
||||||
|
// FullVersion is the best version in AllVersions that matches MajorVersion.
|
||||||
|
// It defaults to InvalidVersion if there are no matches.
|
||||||
|
FullVersion Version
|
||||||
|
|
||||||
|
// AllVersions holds all versions currently available in the repository,
|
||||||
|
// either coming from branch names or from tag names. Version zero (v0)
|
||||||
|
// is only present in the list if it really exists in the repository.
|
||||||
AllVersions VersionList
|
AllVersions VersionList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetVersions records in the relevant fields the details about which
|
||||||
|
// package versions are available in the repository.
|
||||||
|
func (repo *Repo) SetVersions(all []Version) {
|
||||||
|
repo.AllVersions = all
|
||||||
|
for _, v := range repo.AllVersions {
|
||||||
|
if v.Major == repo.MajorVersion.Major && v.Unstable == repo.MajorVersion.Unstable && repo.FullVersion.Less(v) {
|
||||||
|
repo.FullVersion = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GitHubRoot returns the repository root at GitHub, without a schema.
|
// GitHubRoot returns the repository root at GitHub, without a schema.
|
||||||
func (repo *Repo) GitHubRoot() string {
|
func (repo *Repo) GitHubRoot() string {
|
||||||
if repo.User == "" {
|
if repo.User == "" {
|
||||||
@ -85,6 +105,14 @@ func (repo *Repo) GitHubRoot() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GitHubTree returns the repository tree name at GitHub for the selected version.
|
||||||
|
func (repo *Repo) GitHubTree() string {
|
||||||
|
if repo.FullVersion == InvalidVersion {
|
||||||
|
return "master"
|
||||||
|
}
|
||||||
|
return repo.FullVersion.String()
|
||||||
|
}
|
||||||
|
|
||||||
// GopkgRoot returns the package root at gopkg.in, without a schema.
|
// GopkgRoot returns the package root at gopkg.in, without a schema.
|
||||||
func (repo *Repo) GopkgRoot() string {
|
func (repo *Repo) GopkgRoot() string {
|
||||||
return repo.GopkgVersionRoot(repo.MajorVersion)
|
return repo.GopkgVersionRoot(repo.MajorVersion)
|
||||||
@ -157,6 +185,7 @@ func handler(resp http.ResponseWriter, req *http.Request) {
|
|||||||
Name: m[2],
|
Name: m[2],
|
||||||
SubPath: m[5],
|
SubPath: m[5],
|
||||||
OldFormat: oldFormat,
|
OldFormat: oldFormat,
|
||||||
|
FullVersion: InvalidVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
var ok bool
|
var ok bool
|
||||||
@ -167,9 +196,11 @@ func handler(resp http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var changed []byte
|
var changed []byte
|
||||||
|
var versions VersionList
|
||||||
original, err := fetchRefs(repo)
|
original, err := fetchRefs(repo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
changed, repo.AllVersions, err = changeRefs(original, repo.MajorVersion)
|
changed, versions, err = changeRefs(original, repo.MajorVersion)
|
||||||
|
repo.SetVersions(versions)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch err {
|
switch err {
|
||||||
|
21
page.go
21
page.go
@ -131,7 +131,7 @@ const packageTemplateString = `<!DOCTYPE html>
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
<div class="row" >
|
<div class="row" >
|
||||||
<div class="col-sm-12" >
|
<div class="col-sm-12" >
|
||||||
<a class="btn btn-lg btn-info" href="https://{{.Repo.GitHubRoot}}/tree/{{if .Repo.AllVersions}}{{.FullVersion}}{{else}}master{{end}}{{.Repo.SubPath}}" ><i class="fa fa-github"></i> Source Code</a>
|
<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>
|
||||||
<a class="btn btn-lg btn-info" href="http://godoc.org/{{.Repo.GopkgPath}}" ><i class="fa fa-info-circle"></i> API Documentation</a>
|
<a class="btn btn-lg btn-info" href="http://godoc.org/{{.Repo.GopkgPath}}" ><i class="fa fa-info-circle"></i> API Documentation</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -212,9 +212,9 @@ func init() {
|
|||||||
type packageData struct {
|
type packageData struct {
|
||||||
Repo *Repo
|
Repo *Repo
|
||||||
LatestVersions VersionList // Contains only the latest version for each major
|
LatestVersions VersionList // Contains only the latest version for each major
|
||||||
FullVersion Version // Version that the major requested resolves to
|
|
||||||
PackageName string // Actual package identifier as specified in http://golang.org/ref/spec#PackageClause
|
PackageName string // Actual package identifier as specified in http://golang.org/ref/spec#PackageClause
|
||||||
Synopsis string
|
Synopsis string
|
||||||
|
GitTreeName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchResults is used with the godoc.org search API
|
// SearchResults is used with the godoc.org search API
|
||||||
@ -234,15 +234,13 @@ func renderPackagePage(resp http.ResponseWriter, req *http.Request, repo *Repo)
|
|||||||
|
|
||||||
// Calculate the latest version for each major version, both stable and unstable.
|
// Calculate the latest version for each major version, both stable and unstable.
|
||||||
latestVersions := make(map[int]Version)
|
latestVersions := make(map[int]Version)
|
||||||
latestUnstable := make(map[int]Version)
|
|
||||||
for _, v := range repo.AllVersions {
|
for _, v := range repo.AllVersions {
|
||||||
m := latestVersions
|
|
||||||
if v.Unstable {
|
if v.Unstable {
|
||||||
m = latestUnstable
|
continue
|
||||||
}
|
}
|
||||||
v2, exists := m[v.Major]
|
v2, exists := latestVersions[v.Major]
|
||||||
if !exists || v2.Less(v) {
|
if !exists || v2.Less(v) {
|
||||||
m[v.Major] = v
|
latestVersions[v.Major] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
data.LatestVersions = make(VersionList, 0, len(latestVersions))
|
data.LatestVersions = make(VersionList, 0, len(latestVersions))
|
||||||
@ -251,12 +249,9 @@ func renderPackagePage(resp http.ResponseWriter, req *http.Request, repo *Repo)
|
|||||||
}
|
}
|
||||||
sort.Sort(sort.Reverse(data.LatestVersions))
|
sort.Sort(sort.Reverse(data.LatestVersions))
|
||||||
|
|
||||||
if repo.MajorVersion.Unstable {
|
if repo.FullVersion.Unstable {
|
||||||
data.FullVersion = latestUnstable[repo.MajorVersion.Major]
|
// Prepend post-sorting so it shows first.
|
||||||
// Prepend post-sorting so it's show first.
|
data.LatestVersions = append([]Version{repo.FullVersion}, data.LatestVersions...)
|
||||||
data.LatestVersions = append([]Version{data.FullVersion}, data.LatestVersions...)
|
|
||||||
} else {
|
|
||||||
data.FullVersion = latestVersions[repo.MajorVersion.Major]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataMutex sync.Mutex
|
var dataMutex sync.Mutex
|
||||||
|
Loading…
Reference in New Issue
Block a user