Browse Source

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.
master
Gustavo Niemeyer 9 years ago
parent
commit
65998c5554
2 changed files with 47 additions and 21 deletions
  1. +39
    -8
      main.go
  2. +8
    -13
      page.go

+ 39
- 8
main.go View File

@@ -59,6 +59,7 @@ var gogetTemplate = template.Must(template.New("").Parse(`
<html>
<head>
<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>
<body>
go get {{.GopkgPath}}
@@ -73,7 +74,26 @@ type Repo struct {
SubPath string
OldFormat bool // The old /v2/pkg format.
MajorVersion Version
AllVersions VersionList

// 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
}

// 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.
@@ -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.
func (repo *Repo) GopkgRoot() string {
return repo.GopkgVersionRoot(repo.MajorVersion)
@@ -153,10 +181,11 @@ func handler(resp http.ResponseWriter, req *http.Request) {
}

repo := &Repo{
User: m[1],
Name: m[2],
SubPath: m[5],
OldFormat: oldFormat,
User: m[1],
Name: m[2],
SubPath: m[5],
OldFormat: oldFormat,
FullVersion: InvalidVersion,
}

var ok bool
@@ -167,9 +196,11 @@ func handler(resp http.ResponseWriter, req *http.Request) {
}

var changed []byte
var versions VersionList
original, err := fetchRefs(repo)
if err == nil {
changed, repo.AllVersions, err = changeRefs(original, repo.MajorVersion)
changed, versions, err = changeRefs(original, repo.MajorVersion)
repo.SetVersions(versions)
}

switch err {
@@ -338,7 +369,7 @@ func changeRefs(data []byte, major Version) (changed []byte, versions VersionLis
}

var buf bytes.Buffer
buf.Grow(len(data)+256)
buf.Grow(len(data) + 256)

// Copy the header as-is.
buf.Write(data[:hlinei])
@@ -346,7 +377,7 @@ func changeRefs(data []byte, major Version) (changed []byte, versions VersionLis
// Extract the original capabilities.
caps := ""
if i := strings.Index(sdata[hlinei:hlinej], "\x00"); i > 0 {
caps = strings.Replace(sdata[hlinei+i+1:hlinej-1], "symref=", "oldref=", -1)
caps = strings.Replace(sdata[hlinei+i+1:hlinej-1], "symref=", "oldref=", -1)
}

// Insert the HEAD reference line with the right hash and a proper symref capability.


+ 8
- 13
page.go View File

@@ -131,7 +131,7 @@ const packageTemplateString = `<!DOCTYPE html>
{{ end }}
<div class="row" >
<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>
</div>
</div>
@@ -212,9 +212,9 @@ func init() {
type packageData struct {
Repo *Repo
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
Synopsis string
GitTreeName string
}

// 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.
latestVersions := make(map[int]Version)
latestUnstable := make(map[int]Version)
for _, v := range repo.AllVersions {
m := latestVersions
if v.Unstable {
m = latestUnstable
continue
}
v2, exists := m[v.Major]
v2, exists := latestVersions[v.Major]
if !exists || v2.Less(v) {
m[v.Major] = v
latestVersions[v.Major] = v
}
}
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))

if repo.MajorVersion.Unstable {
data.FullVersion = latestUnstable[repo.MajorVersion.Major]
// Prepend post-sorting so it's show first.
data.LatestVersions = append([]Version{data.FullVersion}, data.LatestVersions...)
} else {
data.FullVersion = latestVersions[repo.MajorVersion.Major]
if repo.FullVersion.Unstable {
// Prepend post-sorting so it shows first.
data.LatestVersions = append([]Version{repo.FullVersion}, data.LatestVersions...)
}

var dataMutex sync.Mutex


Loading…
Cancel
Save