internal/fetch: move renderDocHTML logic to separate function
Logic for rendering renderDocHTML is moved to its own function, as a
step towards splitting the logic for rendering HTML and generating the
AST and *doc.Package.
Change-Id: Ied42b566b171ecc82b55ee54b8824c0b1e774c5c
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/256309
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/fetch/dochtml/dochtml.go b/internal/fetch/dochtml/dochtml.go
index da9d78d..3239ef6 100644
--- a/internal/fetch/dochtml/dochtml.go
+++ b/internal/fetch/dochtml/dochtml.go
@@ -26,6 +26,7 @@
"github.com/google/safehtml/legacyconversions"
"github.com/google/safehtml/template"
"github.com/google/safehtml/uncheckedconversions"
+ "golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/fetch/dochtml/internal/render"
"golang.org/x/pkgsite/internal/fetch/internal/doc"
)
@@ -66,7 +67,8 @@
//
// If the rendered documentation HTML size exceeds the specified limit,
// an error with ErrTooLarge in its chain will be returned.
-func Render(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions) (safehtml.HTML, error) {
+func Render(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions) (_ safehtml.HTML, err error) {
+ defer derrors.Wrap(&err, "dochtml.Render")
if opt.Limit == 0 {
const megabyte = 1000 * 1000
opt.Limit = 10 * megabyte
diff --git a/internal/fetch/load.go b/internal/fetch/load.go
index af19039..321c138 100644
--- a/internal/fetch/load.go
+++ b/internal/fetch/load.go
@@ -26,6 +26,7 @@
"strconv"
"strings"
+ "github.com/google/safehtml"
"github.com/google/safehtml/template"
"go.opencensus.io/trace"
"golang.org/x/pkgsite/internal"
@@ -185,7 +186,29 @@
return nil, fmt.Errorf("%d imports found package %q; exceeds limit %d for maxImportsPerPackage", len(d.Imports), importPath, maxImportsPerPackage)
}
- // Render documentation HTML.
+ docHTML, err := renderDocHTML(ctx, innerPath, d, fset, sourceInfo, modInfo)
+ if err != nil && !errors.Is(err, dochtml.ErrTooLarge) {
+ return nil, err
+ }
+ if modulePath == stdlib.ModulePath {
+ importPath = innerPath
+ }
+ v1path := internal.V1Path(importPath, modulePath)
+ return &goPackage{
+ path: importPath,
+ name: packageName,
+ synopsis: doc.Synopsis(d.Doc),
+ v1path: v1path,
+ imports: d.Imports,
+ documentationHTML: docHTML,
+ goos: goos,
+ goarch: goarch,
+ }, err
+}
+
+// renderDocHTML renders documentation HTML for a given package.
+func renderDocHTML(ctx context.Context, innerPath string, d *doc.Package, fset *token.FileSet, sourceInfo *source.Info, modInfo *dochtml.ModuleInfo) (_ safehtml.HTML, err error) {
+ defer derrors.Wrap(&err, "renderDocHTML")
sourceLinkFunc := func(n ast.Node) string {
if sourceInfo == nil {
return ""
@@ -212,22 +235,9 @@
if errors.Is(err, dochtml.ErrTooLarge) {
docHTML = template.MustParseAndExecuteToHTML(docTooLargeReplacement)
} else if err != nil {
- return nil, fmt.Errorf("dochtml.Render: %v", err)
+ return safehtml.HTML{}, err
}
- if modulePath == stdlib.ModulePath {
- importPath = innerPath
- }
- v1path := internal.V1Path(importPath, modulePath)
- return &goPackage{
- path: importPath,
- name: packageName,
- synopsis: doc.Synopsis(d.Doc),
- v1path: v1path,
- imports: d.Imports,
- documentationHTML: docHTML,
- goos: goos,
- goarch: goarch,
- }, err
+ return docHTML, err
}
// matchingFiles returns a map from file names to their contents, read from zipGoFiles.