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.
Change-Id: If7a29ee42a7b66c1acae96fdc27c15b4502d3e28
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/385199
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/internal/godoc/dochtml/internal/render/linkify.go b/internal/godoc/dochtml/internal/render/linkify.go
index 9db49da..347c5b0 100644
--- a/internal/godoc/dochtml/internal/render/linkify.go
+++ b/internal/godoc/dochtml/internal/render/linkify.go
@@ -74,61 +74,64 @@
}
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 != "" {
- var els []docElement
- inLinks := false
- for _, blk := range docToBlocks(doc) {
- var el docElement
- switch blk := blk.(type) {
- case *paragraph:
- if inLinks {
- r.links = append(r.links, parseLinks(blk.lines)...)
- } else {
- el.Body = r.linesToHTML(blk.lines)
- els = append(els, el)
- }
- case *preformat:
- if inLinks {
- r.links = append(r.links, parseLinks(blk.lines)...)
- } else {
- el.IsPreformat = true
- el.Body = r.linesToHTML(blk.lines)
- els = append(els, el)
- }
- case *heading:
- if extractLinks && blk.title == "Links" {
- inLinks = true
- } else {
- inLinks = false
- el.IsHeading = true
- el.Title = blk.title
- id := badAnchorRx.ReplaceAllString(blk.title, "_")
- el.ID = safehtml.IdentifierFromConstantPrefix("hdr", id)
- els = append(els, el)
- }
- }
- }
-
- var headings []docElement
- for _, e := range els {
- if e.IsHeading {
- headings = append(headings, e)
- }
- }
- out.Doc = ExecuteToHTML(r.docTmpl, docData{
- Elements: els,
- Headings: headings,
- EnableCommandTOC: r.enableCommandTOC,
- })
+ 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) {
+ var el docElement
+ switch blk := blk.(type) {
+ case *paragraph:
+ if inLinks {
+ r.links = append(r.links, parseLinks(blk.lines)...)
+ } else {
+ el.Body = r.linesToHTML(blk.lines)
+ els = append(els, el)
+ }
+ case *preformat:
+ if inLinks {
+ r.links = append(r.links, parseLinks(blk.lines)...)
+ } else {
+ el.IsPreformat = true
+ el.Body = r.linesToHTML(blk.lines)
+ els = append(els, el)
+ }
+ case *heading:
+ if extractLinks && blk.title == "Links" {
+ inLinks = true
+ } else {
+ inLinks = false
+ el.IsHeading = true
+ el.Title = blk.title
+ id := badAnchorRx.ReplaceAllString(blk.title, "_")
+ el.ID = safehtml.IdentifierFromConstantPrefix("hdr", id)
+ els = append(els, el)
+ }
+ }
+ }
+
+ var headings []docElement
+ for _, e := range els {
+ if e.IsHeading {
+ headings = append(headings, e)
+ }
+ }
+ return ExecuteToHTML(r.docTmpl, docData{
+ Elements: els,
+ Headings: headings,
+ EnableCommandTOC: r.enableCommandTOC,
+ })
+}
+
// parseLinks extracts links from lines.
func parseLinks(lines []string) []Link {
var links []Link