internal/frontend: show build context on doc page

If it is not all/all, show the build context at
the bottom of the documentation.

This is just a preliminary implementation, so users
can experience the feature before we provide a proper UI.

For golang/go#37232

Change-Id: Ic48a246a8793e61917a31f66580249997255ba77
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/290095
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
This commit is contained in:
Jonathan Amsterdam 2021-02-03 17:28:39 -05:00
Родитель 72b4ab8e04
Коммит 4c18cf855d
4 изменённых файлов: 34 добавлений и 5 удалений

Просмотреть файл

@ -35,3 +35,8 @@
.UnitDoc .Documentation h4 {
font-size: 1.375rem;
}
.UnitDoc-buildContext {
font-style: italic;
padding-top: 1rem;
text-align: right;
}

Просмотреть файл

@ -12,6 +12,11 @@
<div class="Documentation js-documentation">
{{if .DocBody.String}}
{{.DocBody}}
{{if or (ne .GOOS "all") (ne .GOARCH "all")}}
<div class=UnitDoc-buildContext>
GOOS={{.GOOS}}, GOARCH={{.GOARCH}}
</div>
{{end}}
{{else}}
<div class="UnitDoc-emptySection">
<img src="/static/img/gopher-airplane.svg" alt="The Go Gopher"/>

Просмотреть файл

@ -351,6 +351,7 @@ func insertTestModules(ctx context.Context, t *testing.T, mods []testModule) {
var (
in = htmlcheck.In
notIn = htmlcheck.NotIn
hasText = htmlcheck.HasText
attr = htmlcheck.HasAttr
@ -778,7 +779,9 @@ func serverTestCases() []serverTestCase {
name: "package default",
urlPath: fmt.Sprintf("/%s", sample.PackagePath),
wantStatusCode: http.StatusOK,
want: pagecheck.UnitHeader(pkgV100, unversioned, isPackage),
want: in("",
pagecheck.UnitHeader(pkgV100, unversioned, isPackage),
notIn(".UnitDoc-buildContext")),
},
{
name: "package default redirect",
@ -1037,25 +1040,33 @@ func serverTestCases() []serverTestCase {
name: "two docs default",
urlPath: "/a.com/two/pkg",
wantStatusCode: http.StatusOK,
want: in(".Documentation-variables", hasText("var L")),
want: in("",
in(".Documentation-variables", hasText("var L")),
in(".UnitDoc-buildContext", hasText("GOOS=linux"))),
},
{
name: "two docs linux",
urlPath: "/a.com/two/pkg?GOOS=linux",
wantStatusCode: http.StatusOK,
want: in(".Documentation-variables", hasText("var L")),
want: in("",
in(".Documentation-variables", hasText("var L")),
in(".UnitDoc-buildContext", hasText("GOOS=linux"))),
},
{
name: "two docs windows",
urlPath: "/a.com/two/pkg?GOOS=windows",
wantStatusCode: http.StatusOK,
want: in(".Documentation-variables", hasText("var W")),
want: in("",
in(".Documentation-variables", hasText("var W")),
in(".UnitDoc-buildContext", hasText("GOOS=windows"))),
},
{
name: "two docs no match",
urlPath: "/a.com/two/pkg?GOOS=dragonfly",
wantStatusCode: http.StatusOK,
want: htmlcheck.NotIn(".Documentation-variables"),
want: in("",
notIn(".Documentation-variables"),
notIn(".UnitDoc-buildContext")),
},
}
}

Просмотреть файл

@ -79,6 +79,9 @@ type MainDetails struct {
// tag on the main unit page.
DocSynopsis string
// GOOS and GOARCH are the build context for the doc.
GOOS, GOARCH string
// SourceFiles contains .go files for the package.
SourceFiles []*File
@ -161,11 +164,14 @@ func fetchMainDetails(ctx context.Context, ds internal.DataSource, um *internal.
docLinks, modLinks []link
files []*File
synopsis string
goos, goarch string
)
doc := internal.DocumentationForBuildContext(unit.Documentation, bc)
if doc != nil {
synopsis = doc.Synopsis
goos = doc.GOOS
goarch = doc.GOARCH
end := middleware.ElapsedStat(ctx, "DecodePackage")
docPkg, err := godoc.DecodePackage(doc.Source)
end()
@ -229,6 +235,8 @@ func fetchMainDetails(ctx context.Context, ds internal.DataSource, um *internal.
DocOutline: docParts.Outline,
DocBody: docParts.Body,
DocSynopsis: synopsis,
GOOS: goos,
GOARCH: goarch,
SourceFiles: files,
RepositoryURL: um.SourceInfo.RepoURL(),
SourceURL: um.SourceInfo.DirectoryURL(internal.Suffix(um.Path, um.ModulePath)),