internal/frontend: move functions to unit_main.go
Functions used only by fetchMainDetails is moved to unit_main.go. Pure
code in motion.
Change-Id: I78cff88145ffeec324e87ce9c837c8d4909e1d48
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/261722
Trust: Julie Qiu <julie@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/internal/frontend/unit.go b/internal/frontend/unit.go
index a311036..92cd396 100644
--- a/internal/frontend/unit.go
+++ b/internal/frontend/unit.go
@@ -8,14 +8,9 @@
"context"
"net/http"
"path"
- "sort"
- "strings"
- "github.com/google/safehtml"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
- "golang.org/x/pkgsite/internal/experiment"
- "golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/middleware"
"golang.org/x/pkgsite/internal/stdlib"
)
@@ -131,45 +126,6 @@
return nil
}
-func getHTML(ctx context.Context, u *internal.Unit) safehtml.HTML {
- if experiment.IsActive(ctx, internal.ExperimentFrontendRenderDoc) && len(u.Documentation.Source) > 0 {
- dd, err := renderDoc(ctx, u)
- if err != nil {
- log.Errorf(ctx, "render doc failed: %v", err)
- // Fall through to use stored doc.
- } else {
- return dd.Documentation
- }
- }
- return u.Documentation.HTML
-}
-
-// moduleInfo extracts module info from a unit. This is a shim
-// for functions ReadmeHTML and createDirectory that will be removed
-// when we complete the switch to units.
-func moduleInfo(um *internal.UnitMeta) *internal.ModuleInfo {
- return &internal.ModuleInfo{
- ModulePath: um.ModulePath,
- Version: um.Version,
- CommitTime: um.CommitTime,
- IsRedistributable: um.IsRedistributable,
- SourceInfo: um.SourceInfo,
- }
-}
-
-// readmeContent renders the readme to html.
-func readmeContent(ctx context.Context, um *internal.UnitMeta, readme *internal.Readme) (safehtml.HTML, error) {
- if um.IsRedistributable && readme != nil {
- mi := moduleInfo(um)
- readme, err := ReadmeHTML(ctx, mi, readme)
- if err != nil {
- return safehtml.HTML{}, err
- }
- return readme, nil
- }
- return safehtml.HTML{}, nil
-}
-
const (
pageTypeModule = "module"
pageTypeDirectory = "directory"
@@ -247,40 +203,3 @@
bc.Links = append([]link{{Href: "/", Body: "Discover Packages"}}, bc.Links...)
return bc
}
-
-func getNestedModules(ctx context.Context, ds internal.DataSource, um *internal.UnitMeta) ([]*NestedModule, error) {
- nestedModules, err := ds.GetNestedModules(ctx, um.ModulePath)
- if err != nil {
- return nil, err
- }
- var mods []*NestedModule
- for _, m := range nestedModules {
- if m.SeriesPath() == internal.SeriesPathForModule(um.ModulePath) {
- continue
- }
- if !strings.HasPrefix(m.ModulePath, um.Path+"/") {
- continue
- }
- mods = append(mods, &NestedModule{
- URL: constructPackageURL(m.ModulePath, m.ModulePath, internal.LatestVersion),
- Suffix: internal.Suffix(m.SeriesPath(), um.Path),
- })
- }
- return mods, nil
-}
-
-func getSubdirectories(um *internal.UnitMeta, pkgs []*internal.PackageMeta) []*Subdirectory {
- var sdirs []*Subdirectory
- for _, pm := range pkgs {
- if um.Path == pm.Path {
- continue
- }
- sdirs = append(sdirs, &Subdirectory{
- URL: constructPackageURL(pm.Path, um.ModulePath, linkVersion(um.Version, um.ModulePath)),
- Suffix: internal.Suffix(pm.Path, um.Path),
- Synopsis: pm.Synopsis,
- })
- }
- sort.Slice(sdirs, func(i, j int) bool { return sdirs[i].Suffix < sdirs[j].Suffix })
- return sdirs
-}
diff --git a/internal/frontend/unit_main.go b/internal/frontend/unit_main.go
index 08c2ade..549de42 100644
--- a/internal/frontend/unit_main.go
+++ b/internal/frontend/unit_main.go
@@ -6,11 +6,15 @@
import (
"context"
+ "sort"
"strconv"
+ "strings"
"github.com/google/safehtml"
"golang.org/x/pkgsite/internal"
+ "golang.org/x/pkgsite/internal/experiment"
"golang.org/x/pkgsite/internal/godoc"
+ "golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/postgres"
)
@@ -156,3 +160,79 @@
IsPackage: unit.IsPackage(),
}, nil
}
+
+// moduleInfo extracts module info from a unit. This is a shim
+// for functions ReadmeHTML and createDirectory that will be removed
+// when we complete the switch to units.
+func moduleInfo(um *internal.UnitMeta) *internal.ModuleInfo {
+ return &internal.ModuleInfo{
+ ModulePath: um.ModulePath,
+ Version: um.Version,
+ CommitTime: um.CommitTime,
+ IsRedistributable: um.IsRedistributable,
+ SourceInfo: um.SourceInfo,
+ }
+}
+
+// readmeContent renders the readme to html.
+func readmeContent(ctx context.Context, um *internal.UnitMeta, readme *internal.Readme) (safehtml.HTML, error) {
+ if um.IsRedistributable && readme != nil {
+ mi := moduleInfo(um)
+ readme, err := ReadmeHTML(ctx, mi, readme)
+ if err != nil {
+ return safehtml.HTML{}, err
+ }
+ return readme, nil
+ }
+ return safehtml.HTML{}, nil
+}
+
+func getNestedModules(ctx context.Context, ds internal.DataSource, um *internal.UnitMeta) ([]*NestedModule, error) {
+ nestedModules, err := ds.GetNestedModules(ctx, um.ModulePath)
+ if err != nil {
+ return nil, err
+ }
+ var mods []*NestedModule
+ for _, m := range nestedModules {
+ if m.SeriesPath() == internal.SeriesPathForModule(um.ModulePath) {
+ continue
+ }
+ if !strings.HasPrefix(m.ModulePath, um.Path+"/") {
+ continue
+ }
+ mods = append(mods, &NestedModule{
+ URL: constructPackageURL(m.ModulePath, m.ModulePath, internal.LatestVersion),
+ Suffix: internal.Suffix(m.SeriesPath(), um.Path),
+ })
+ }
+ return mods, nil
+}
+
+func getSubdirectories(um *internal.UnitMeta, pkgs []*internal.PackageMeta) []*Subdirectory {
+ var sdirs []*Subdirectory
+ for _, pm := range pkgs {
+ if um.Path == pm.Path {
+ continue
+ }
+ sdirs = append(sdirs, &Subdirectory{
+ URL: constructPackageURL(pm.Path, um.ModulePath, linkVersion(um.Version, um.ModulePath)),
+ Suffix: internal.Suffix(pm.Path, um.Path),
+ Synopsis: pm.Synopsis,
+ })
+ }
+ sort.Slice(sdirs, func(i, j int) bool { return sdirs[i].Suffix < sdirs[j].Suffix })
+ return sdirs
+}
+
+func getHTML(ctx context.Context, u *internal.Unit) safehtml.HTML {
+ if experiment.IsActive(ctx, internal.ExperimentFrontendRenderDoc) && len(u.Documentation.Source) > 0 {
+ dd, err := renderDoc(ctx, u)
+ if err != nil {
+ log.Errorf(ctx, "render doc failed: %v", err)
+ // Fall through to use stored doc.
+ } else {
+ return dd.Documentation
+ }
+ }
+ return u.Documentation.HTML
+}