From 3358a5068a87bc25bd551698f4f0be7c5677168d Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Thu, 19 Jan 2012 18:59:06 +1100 Subject: [PATCH] godoc: add anchors to cmd documentation headings Also, disable server-side generation of TOC for commands as they would only ever show Overview. The JS does a better job (for now). Fixes #2732. R=gri, dsymonds CC=golang-dev https://golang.org/cl/5558046 --- doc/godocs.js | 58 ++++++++++++++++----------------------- lib/godoc/package.html | 2 ++ src/pkg/go/doc/comment.go | 18 +++++++++++- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/doc/godocs.js b/doc/godocs.js index bbf5ea28931..3b8625a58d7 100644 --- a/doc/godocs.js +++ b/doc/godocs.js @@ -66,44 +66,32 @@ function godocs_generateTOC() { var i; for (i = 0; i < navbar.parentNode.childNodes.length; i++) { var node = navbar.parentNode.childNodes[i]; + if ((node.tagName != 'h2') && (node.tagName != 'H2') && + (node.tagName != 'h3') && (node.tagName != 'H3')) { + continue; + } + if (!node.id) { + node.id = 'tmp_' + i; + } + var text = godocs_nodeToText(node); + if (!text) { continue; } + + var textNode = document.createTextNode(text); + + var link = document.createElement('a'); + link.href = '#' + node.id; + link.appendChild(textNode); + + // Then create the item itself + var item; if ((node.tagName == 'h2') || (node.tagName == 'H2')) { - if (!node.id) { - node.id = 'tmp_' + i; - } - var text = godocs_nodeToText(node); - if (!text) { continue; } - - var textNode = document.createTextNode(text); - - var link = document.createElement('a'); - link.href = '#' + node.id; - link.appendChild(textNode); - - // Then create the item itself - var item = document.createElement('dt'); - - item.appendChild(link); - toc_items.push(item); + item = document.createElement('dt'); + } else { // h3 + item = document.createElement('dd'); } - if ((node.tagName == 'h3') || (node.tagName == 'H3')) { - if (!node.id) { - node.id = 'tmp_' + i; - } - var text = godocs_nodeToText(node); - if (!text) { continue; } - var textNode = document.createTextNode(text); - - var link = document.createElement('a'); - link.href = '#' + node.id; - link.appendChild(textNode); - - // Then create the item itself - var item = document.createElement('dd'); - - item.appendChild(link); - toc_items.push(item); - } + item.appendChild(link); + toc_items.push(item); } if (toc_items.length <= 1) { return; } diff --git a/lib/godoc/package.html b/lib/godoc/package.html index 155f24032ab..5a7f3ef976d 100644 --- a/lib/godoc/package.html +++ b/lib/godoc/package.html @@ -3,6 +3,7 @@ Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. --> +{{if .IsPkg}}
{{with .PDoc}} @@ -37,6 +38,7 @@ {{end}}
+{{end}} {{with .PAst}} diff --git a/src/pkg/go/doc/comment.go b/src/pkg/go/doc/comment.go index 060e37bff14..d73b1315954 100644 --- a/src/pkg/go/doc/comment.go +++ b/src/pkg/go/doc/comment.go @@ -68,7 +68,8 @@ var ( html_endp = []byte("

\n") html_pre = []byte("
")
 	html_endpre = []byte("
\n") - html_h = []byte("

") + html_h = []byte(`

`) html_endh = []byte("

\n") ) @@ -225,6 +226,12 @@ type block struct { lines []string } +var nonAlphaNumRx = regexp.MustCompile(`[^a-zA-Z0-9]`) + +func anchorID(line string) string { + return nonAlphaNumRx.ReplaceAllString(line, "_") +} + // ToHTML converts comment text to formatted HTML. // The comment was prepared by DocReader, // so it is known not to have leading, trailing blank lines @@ -253,9 +260,18 @@ func ToHTML(w io.Writer, text string, words map[string]string) { w.Write(html_endp) case opHead: w.Write(html_h) + id := "" for _, line := range b.lines { + if id == "" { + id = anchorID(line) + w.Write([]byte(id)) + w.Write(html_hq) + } commentEscape(w, line, true) } + if id == "" { + w.Write(html_hq) + } w.Write(html_endh) case opPre: w.Write(html_pre)