
\n",
"https://pkg.go.dev/"+string(pkg.Types().Path()))
// package doc
for _, f := range pkg.Syntax() {
if f.Doc != nil {
fmt.Fprintf(&buf, "\n", docHTML(f.Doc, docpkg.Doc))
break
}
}
// symbol index
fmt.Fprintf(&buf, "
Index
\n")
fmt.Fprintf(&buf, "
\n")
if len(docpkg.Consts) > 0 {
fmt.Fprintf(&buf, "- Constants
\n")
}
if len(docpkg.Vars) > 0 {
fmt.Fprintf(&buf, "- Variables
\n")
}
for _, fn := range docpkg.Funcs {
obj := scope.Lookup(fn.Name).(*types.Func)
fmt.Fprintf(&buf, "- %s
\n",
obj.Name(), escape(fnString(obj)))
}
for _, doctype := range docpkg.Types {
tname := scope.Lookup(doctype.Name).(*types.TypeName)
fmt.Fprintf(&buf, "- type %[1]s
\n",
tname.Name())
if len(doctype.Funcs)+len(doctype.Methods) > 0 {
fmt.Fprintf(&buf, "\n")
// constructors
for _, docfn := range doctype.Funcs {
obj := scope.Lookup(docfn.Name).(*types.Func)
fmt.Fprintf(&buf, "- %s
\n",
docfn.Name, escape(fnString(obj)))
}
// methods
for _, docmethod := range doctype.Methods {
method, _, _ := types.LookupFieldOrMethod(tname.Type(), true, tname.Pkg(), docmethod.Name)
fmt.Fprintf(&buf, "- %s
\n",
doctype.Name,
docmethod.Name,
escape(fnString(method.(*types.Func))))
}
fmt.Fprintf(&buf, "
\n")
}
}
// TODO(adonovan): add index of Examples here.
fmt.Fprintf(&buf, "
\n")
// constants and variables
values := func(vals []*doc.Value) {
for _, v := range vals {
// anchors
for _, name := range v.Names {
fmt.Fprintf(&buf, "
\n", escape(name))
}
// declaration
decl2 := *v.Decl // shallow copy
decl2.Doc = nil
fmt.Fprintf(&buf, "
%s
\n", nodeHTML(&decl2))
// comment (if any)
fmt.Fprintf(&buf, "\n", docHTML(v.Decl, v.Doc))
}
}
fmt.Fprintf(&buf, "
Constants
\n")
if len(docpkg.Consts) == 0 {
fmt.Fprintf(&buf, "
(no constants)
\n")
} else {
values(docpkg.Consts)
}
fmt.Fprintf(&buf, "
Variables
\n")
if len(docpkg.Vars) == 0 {
fmt.Fprintf(&buf, "
(no variables)
\n")
} else {
values(docpkg.Vars)
}
// addedInHTML returns an HTML division containing the Go release version at
// which this obj became available.
addedInHTML := func(obj types.Object) string {
if sym := StdSymbolOf(obj); sym != nil && sym.Version != stdlib.Version(0) {
return fmt.Sprintf("
added in %v", sym.Version)
}
return ""
}
// package-level functions
fmt.Fprintf(&buf, "
Functions
\n")
// funcs emits a list of package-level functions,
// possibly organized beneath the type they construct.
funcs := func(funcs []*doc.Func) {
for _, docfn := range funcs {
obj := scope.Lookup(docfn.Name).(*types.Func)
fmt.Fprintf(&buf, "
func %s %s
\n",
docfn.Name, objHTML(pkg.FileSet(), web, obj), addedInHTML(obj))
// decl: func F(params) results
fmt.Fprintf(&buf, "
%s
\n",
nodeHTML(docfn.Decl.Type))
// comment (if any)
fmt.Fprintf(&buf, "\n", docHTML(docfn.Decl, docfn.Doc))
}
}
funcs(docpkg.Funcs)
// types and their subelements
fmt.Fprintf(&buf, "
Types
\n")
for _, doctype := range docpkg.Types {
tname := scope.Lookup(doctype.Name).(*types.TypeName)
// title and source link
fmt.Fprintf(&buf, "
type %s %s
\n",
doctype.Name, objHTML(pkg.FileSet(), web, tname), addedInHTML(tname))
// declaration
// TODO(adonovan): excise non-exported struct fields somehow.
decl2 := *doctype.Decl // shallow copy
decl2.Doc = nil
fmt.Fprintf(&buf, "
%s
\n", nodeHTML(&decl2))
// comment (if any)
fmt.Fprintf(&buf, "\n", docHTML(doctype.Decl, doctype.Doc))
// subelements
values(doctype.Consts) // constants of type T
values(doctype.Vars) // vars of type T
funcs(doctype.Funcs) // constructors of T
// methods on T
for _, docmethod := range doctype.Methods {
method, _, _ := types.LookupFieldOrMethod(tname.Type(), true, tname.Pkg(), docmethod.Name)
fmt.Fprintf(&buf, "
func (%s) %s %s
\n",
doctype.Name, docmethod.Name,
docmethod.Orig, // T or *T
objHTML(pkg.FileSet(), web, method), addedInHTML(method))
// decl: func (x T) M(params) results
fmt.Fprintf(&buf, "
%s
\n",
nodeHTML(docmethod.Decl.Type))
// comment (if any)
fmt.Fprintf(&buf, "\n",
docHTML(docmethod.Decl, docmethod.Doc))
}
}
// source files
fmt.Fprintf(&buf, "
Source files
\n")
for _, filename := range docpkg.Filenames {
fmt.Fprintf(&buf, "\n",
sourceLink(filepath.Base(filename), web.SrcURL(filename, 1, 1)))
}
fmt.Fprintf(&buf, "\n")
fmt.Fprintf(&buf, "\n")
fmt.Fprintf(&buf, "\n")
return buf.Bytes(), nil
}