internal/frontend: add unit_header.go
Logic related to rendering the page title, type, and labels is moved to
unit_header.go. Pure code in motion.
Change-Id: I225c1397421223d17077185de90b48a7562a6b4b
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/262002
Trust: Julie Qiu <julie@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/internal/frontend/header.go b/internal/frontend/header.go
index b012186..b98b58d 100644
--- a/internal/frontend/header.go
+++ b/internal/frontend/header.go
@@ -10,7 +10,6 @@
"strings"
"time"
- "golang.org/x/mod/module"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/licenses"
@@ -118,21 +117,6 @@
return fmt.Sprintf("/%s@%s/%s", modulePath, linkVersion, strings.TrimPrefix(pkgPath, modulePath+"/"))
}
-// effectiveName returns either the command name or package name.
-func effectiveName(pkgPath, pkgName string) string {
- if pkgName != "main" {
- return pkgName
- }
- var prefix string // package path without version
- if pkgPath[len(pkgPath)-3:] == "/v1" {
- prefix = pkgPath[:len(pkgPath)-3]
- } else {
- prefix, _, _ = module.SplitPathVersion(pkgPath)
- }
- _, base := path.Split(prefix)
- return base
-}
-
// packageHTMLTitle constructs the details page title for pkg.
// The string will appear in the <title> element (and thus
// the browser tab).
diff --git a/internal/frontend/unit.go b/internal/frontend/unit.go
index 34e832c..dc61d0b 100644
--- a/internal/frontend/unit.go
+++ b/internal/frontend/unit.go
@@ -7,12 +7,10 @@
import (
"context"
"net/http"
- "path"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/middleware"
- "golang.org/x/pkgsite/internal/stdlib"
)
// UnitPage contains data needed to render the unit template.
@@ -125,70 +123,3 @@
s.servePage(ctx, w, tabSettings.TemplateName, page)
return nil
}
-
-const (
- pageTypeModule = "module"
- pageTypeDirectory = "directory"
- pageTypePackage = "package"
- pageTypeCommand = "command"
- pageTypeModuleStd = "std"
- pageTypeStdlib = "standard library"
-)
-
-// pageTitle determines the pageTitles for a given unit.
-// See TestPageTitlesAndTypes for examples.
-func pageTitle(um *internal.UnitMeta) string {
- switch {
- case um.Path == stdlib.ModulePath:
- return "Standard library"
- case um.IsCommand():
- return effectiveName(um.Path, um.Name)
- case um.IsPackage():
- return um.Name
- case um.IsModule():
- return path.Base(um.Path)
- default:
- return path.Base(um.Path) + "/"
- }
-}
-
-// pageType determines the pageType for a given unit.
-func pageType(um *internal.UnitMeta) string {
- if um.Path == stdlib.ModulePath {
- return pageTypeModuleStd
- }
- if um.IsCommand() {
- return pageTypeCommand
- }
- if um.IsPackage() {
- return pageTypePackage
- }
- if um.IsModule() {
- return pageTypeModule
- }
- return pageTypeDirectory
-}
-
-// pageLabels determines the labels to display for a given unit.
-// See TestPageTitlesAndTypes for examples.
-func pageLabels(um *internal.UnitMeta) []string {
- var pageTypes []string
- if um.Path == stdlib.ModulePath {
- return nil
- }
- if um.IsCommand() {
- pageTypes = append(pageTypes, pageTypeCommand)
- } else if um.IsPackage() {
- pageTypes = append(pageTypes, pageTypePackage)
- }
- if um.IsModule() {
- pageTypes = append(pageTypes, pageTypeModule)
- }
- if !um.IsPackage() && !um.IsModule() {
- pageTypes = append(pageTypes, pageTypeDirectory)
- }
- if stdlib.Contains(um.Path) {
- pageTypes = append(pageTypes, pageTypeStdlib)
- }
- return pageTypes
-}
diff --git a/internal/frontend/unit_header.go b/internal/frontend/unit_header.go
new file mode 100644
index 0000000..3e9269d
--- /dev/null
+++ b/internal/frontend/unit_header.go
@@ -0,0 +1,95 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package frontend
+
+import (
+ "path"
+
+ "golang.org/x/mod/module"
+ "golang.org/x/pkgsite/internal"
+ "golang.org/x/pkgsite/internal/stdlib"
+)
+
+const (
+ pageTypeModule = "module"
+ pageTypeDirectory = "directory"
+ pageTypePackage = "package"
+ pageTypeCommand = "command"
+ pageTypeModuleStd = "std"
+ pageTypeStdlib = "standard library"
+)
+
+// pageTitle determines the pageTitles for a given unit.
+// See TestPageTitlesAndTypes for examples.
+func pageTitle(um *internal.UnitMeta) string {
+ switch {
+ case um.Path == stdlib.ModulePath:
+ return "Standard library"
+ case um.IsCommand():
+ return effectiveName(um.Path, um.Name)
+ case um.IsPackage():
+ return um.Name
+ case um.IsModule():
+ return path.Base(um.Path)
+ default:
+ return path.Base(um.Path) + "/"
+ }
+}
+
+// pageType determines the pageType for a given unit.
+func pageType(um *internal.UnitMeta) string {
+ if um.Path == stdlib.ModulePath {
+ return pageTypeModuleStd
+ }
+ if um.IsCommand() {
+ return pageTypeCommand
+ }
+ if um.IsPackage() {
+ return pageTypePackage
+ }
+ if um.IsModule() {
+ return pageTypeModule
+ }
+ return pageTypeDirectory
+}
+
+// pageLabels determines the labels to display for a given unit.
+// See TestPageTitlesAndTypes for examples.
+func pageLabels(um *internal.UnitMeta) []string {
+ var pageTypes []string
+ if um.Path == stdlib.ModulePath {
+ return nil
+ }
+ if um.IsCommand() {
+ pageTypes = append(pageTypes, pageTypeCommand)
+ } else if um.IsPackage() {
+ pageTypes = append(pageTypes, pageTypePackage)
+ }
+ if um.IsModule() {
+ pageTypes = append(pageTypes, pageTypeModule)
+ }
+ if !um.IsPackage() && !um.IsModule() {
+ pageTypes = append(pageTypes, pageTypeDirectory)
+ }
+ if stdlib.Contains(um.Path) {
+ pageTypes = append(pageTypes, pageTypeStdlib)
+ }
+ return pageTypes
+}
+
+// effectiveName returns either the command name or package name.
+func effectiveName(pkgPath, pkgName string) string {
+ if pkgName != "main" {
+ return pkgName
+ }
+ var prefix string // package path without version
+ if pkgPath[len(pkgPath)-3:] == "/v1" {
+ prefix = pkgPath[:len(pkgPath)-3]
+ } else {
+ prefix, _, _ = module.SplitPathVersion(pkgPath)
+ }
+ _, base := path.Split(prefix)
+ return base
+}
diff --git a/internal/frontend/unit_test.go b/internal/frontend/unit_header_test.go
similarity index 100%
rename from internal/frontend/unit_test.go
rename to internal/frontend/unit_header_test.go