Source File
dochtml.go
Belonging Package
golang.org/x/pkgsite/internal/godoc/dochtml
package dochtml
import (
)
ErrTooLarge = errors.New("rendered documentation HTML size exceeded the specified limit")
)
type ModuleInfo struct {
ModulePath string
ModulePackages map[string]bool
}
FileLinkFunc func(file string) (url string)
SourceLinkFunc func(ast.Node) string
ModInfo *ModuleInfo
Limit int64 // If zero, a default limit of 10 megabytes is used.
}
for , := range .Consts {
.IsDeprecated = false
}
for , := range .Vars {
.IsDeprecated = false
}
for , := range .Funcs {
.IsDeprecated = false
}
for , := range .Types {
.IsDeprecated = false
for , := range .Funcs {
.IsDeprecated = false
}
for , := range .Methods {
.IsDeprecated = false
}
}
}
, , := renderInfo(, , , )
= .Package
if docIsEmpty() {
return &Parts{}, nil
}
:= func( *template.Template) safehtml.HTML {
if != nil {
return safehtml.HTML{}
}
:= template.Must(.Clone()).Funcs()
var safehtml.HTML
, = executeToHTMLWithLimit(, , .Limit)
return
}
:= &Parts{
Body: (bodyTemplate),
Outline: (outlineTemplate),
type item struct {
Doc string
Decl ast.Decl // GenDecl for consts, vars and types; FuncDecl for functions
Name string // for types and functions; empty for consts and vars
FullName string // for methods, the type name + "." + Name; else same as Name
HeaderStart string // text of header, before source link
Examples []*example // for types and functions; empty for vars and consts
IsDeprecated bool
Kind string // for data-kind attribute
HeaderClass string // class for header
}
func ( *doc.Package, map[string][]*example) (, , , []*item) {
= valuesToItems(.Consts)
= valuesToItems(.Vars)
= funcsToItems(.Funcs, "Documentation-functionHeader", "", )
for , := range .Types {
= append(, typeToItem(, ))
}
return , , ,
}
func ( []*doc.Value) []*item {
var []*item
for , := range {
= append(, valueToItem())
}
return
}
func ( *doc.Value) *item {
return &item{
Doc: .Doc,
Decl: .Decl,
IsDeprecated: .IsDeprecated,
}
}
func ( []*doc.Func, , string, map[string][]*example) []*item {
var []*item
for , := range {
:= .Name
if != "" {
= + "." + .Name
}
:= "function"
:= "func"
if .Recv != "" {
= "method"
+= " (" + .Recv + ")"
}
:= &item{
Doc: .Doc,
Decl: .Decl,
Name: .Name,
FullName: ,
HeaderStart: ,
IsDeprecated: .IsDeprecated,
Examples: [],
Kind: ,
HeaderClass: ,
}
= append(, )
}
return
}
func ( *doc.Type, map[string][]*example) *item {
return &item{
Name: .Name,
FullName: .Name,
Doc: .Doc,
Decl: .Decl,
HeaderStart: "type",
IsDeprecated: .IsDeprecated,
Kind: "type",
HeaderClass: "Documentation-typeHeader",
Examples: [.Name],
Consts: valuesToItems(.Consts),
Vars: valuesToItems(.Vars),
Funcs: funcsToItems(.Funcs, "Documentation-typeFuncHeader", "", ),
Methods: funcsToItems(.Methods, "Documentation-typeMethodHeader", .Name, ),
}
}
func ( *doc.Package) bool {
return .Doc == "" &&
len(.Examples) == 0 &&
len(.Consts) == 0 &&
len(.Vars) == 0 &&
len(.Types) == 0 &&
len(.Funcs) == 0
}
:= *
= &
:=
if .ModInfo != nil {
= versionedPkgPath(, .ModInfo)
}
return "/" +
},
DisableHotlinking: true,
EnableCommandTOC: true,
})
:= func( string) safehtml.HTML {
return linkHTML(, .FileLinkFunc(), "Documentation-file")
}
:= func( string, ast.Node) safehtml.HTML {
return linkHTML(, .SourceLinkFunc(), "Documentation-source")
}
:= func( string) safehtml.HTML {
return safehtml.HTMLEscaped(.SinceVersionFunc())
}
:= map[string]interface{}{
"render_short_synopsis": .ShortSynopsis,
"render_synopsis": .Synopsis,
"render_doc": .DocHTML,
"render_doc_extract_links": .DocHTMLExtractLinks,
"render_decl": .DeclHTML,
"render_code": .CodeHTML,
"file_link": ,
"source_link": ,
"since_version": ,
}
:= collectExamples()
:= templateData{
Package: ,
RootURL: "/pkg",
Examples: ,
NoteHeaders: buildNoteHeaders(.Notes),
}
.Consts, .Vars, .Funcs, .Types = packageToItems(, .Map)
return , , .Links
}
func ( *template.Template, interface{}, int64) (safehtml.HTML, error) {
:= &limitBuffer{B: new(bytes.Buffer), Remain: }
:= .Execute(, )
if .Remain < 0 {
return safehtml.HTML{}, fmt.Errorf("dochtml.Render: %w", ErrTooLarge)
} else if != nil {
return safehtml.HTML{}, fmt.Errorf("dochtml.Render: %v", )
}
return uncheckedconversions.HTMLFromStringKnownToSatisfyTypeContract(.B.String()), nil
}
func (, , string) safehtml.HTML {
if == "" {
return safehtml.HTMLEscaped()
}
return render.ExecuteToHTML(render.LinkTemplate, render.Link{Class: , Href: , Text: })
}
func ( *doc.Package, func( string, *doc.Example)) {
for , := range .Examples {
("", )
}
for , := range .Funcs {
for , := range .Examples {
(.Name, )
}
}
for , := range .Types {
for , := range .Examples {
(.Name, )
}
for , := range .Funcs {
for , := range .Examples {
(.Name, )
}
}
for , := range .Methods {
for , := range .Examples {
(.Name+"."+.Name, )
}
}
}
}
return .List[].ParentID < .List[].ParentID
})
return
}
func (, string) safehtml.Identifier {
switch {
case == "" && == "":
return safehtml.IdentifierFromConstant("example-package")
case == "" && != "":
render.ValidateGoDottedExpr()
return legacyconversions.RiskilyAssumeIdentifier("example-package-" + )
case != "" && == "":
render.ValidateGoDottedExpr()
return legacyconversions.RiskilyAssumeIdentifier("example-" + )
case != "" && != "":
render.ValidateGoDottedExpr()
render.ValidateGoDottedExpr()
return legacyconversions.RiskilyAssumeIdentifier("example-" + + "-" + )
default:
panic("unreachable")
}
}
type noteHeader struct {
SafeIdentifier safehtml.Identifier
Label string
}
func ( map[string][]*doc.Note) map[string]noteHeader {
:= map[string]noteHeader{}
for := range {
[] = noteHeader{
SafeIdentifier: safehtml.IdentifierFromConstantPrefix("pkg-note", ),
Label: strings.Title(strings.ToLower()),
}
}
return
}
func ( string, *ModuleInfo) string {
if == nil || !.ModulePackages[] {
return
:= [len(.ModulePath):]
return fmt.Sprintf("%s@%s%s", .ModulePath, .ResolvedVersion, )
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |