internal/godoc/dochtml/internal/render: refactor Renderer.declHTML
Split out the part that renders doc comments to a separate function.
This will make it easier to integrate the (probably) forthcoming
go/doc/comments package.
Trust: Jonathan Amsterdam
Run-TryBot: Jonathan Amsterdam
@@ -74,9 +74,17 @@ type docElement struct {
}
func (r *Renderer) declHTML(doc string, decl ast.Decl, extractLinks bool) (out struct{ Doc, Decl safehtml.HTML }) {
- dids := newDeclIDs(decl)
- idr := &identifierResolver{r.pids, dids, r.packageURL}
if doc != "" {
+ out.Doc = r.formatDocHTML(doc, extractLinks)
+ }
+ if decl != nil {
+ idr := &identifierResolver{r.pids, newDeclIDs(decl), r.packageURL}
+ out.Decl = r.formatDeclHTML(decl, idr)
+ }
+ return out
+}
+
+func (r *Renderer) formatDocHTML(doc string, extractLinks bool) safehtml.HTML {
var els []docElement
inLinks := false
for _, blk := range docToBlocks(doc) {
@@ -117,17 +125,12 @@ func (r *Renderer) declHTML(doc string, decl ast.Decl, extractLinks bool) (out s
headings = append(headings, e)
}
}
- out.Doc = ExecuteToHTML(r.docTmpl, docData{
+ return ExecuteToHTML(r.docTmpl, docData{
Elements: els,
Headings: headings,
EnableCommandTOC: r.enableCommandTOC,
})
}
- if decl != nil {
- out.Decl = r.formatDeclHTML(decl, idr)
- }
- return out
-}
// parseLinks extracts links from lines.
func parseLinks(lines []string) []Link {
