mirror of
https://github.com/golang/go
synced 2024-11-21 23:14:40 -07:00
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
This commit is contained in:
parent
b4829c1de6
commit
3358a5068a
@ -66,45 +66,33 @@ function godocs_generateTOC() {
|
|||||||
var i;
|
var i;
|
||||||
for (i = 0; i < navbar.parentNode.childNodes.length; i++) {
|
for (i = 0; i < navbar.parentNode.childNodes.length; i++) {
|
||||||
var node = navbar.parentNode.childNodes[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.tagName == 'h2') || (node.tagName == 'H2')) {
|
||||||
if (!node.id) {
|
item = document.createElement('dt');
|
||||||
node.id = 'tmp_' + i;
|
} else { // h3
|
||||||
|
item = document.createElement('dd');
|
||||||
}
|
}
|
||||||
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);
|
item.appendChild(link);
|
||||||
toc_items.push(item);
|
toc_items.push(item);
|
||||||
}
|
}
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toc_items.length <= 1) { return; }
|
if (toc_items.length <= 1) { return; }
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
Use of this source code is governed by a BSD-style
|
Use of this source code is governed by a BSD-style
|
||||||
license that can be found in the LICENSE file.
|
license that can be found in the LICENSE file.
|
||||||
-->
|
-->
|
||||||
|
{{if .IsPkg}}
|
||||||
<!-- Table of contents; must be named manual-nav to turn off auto nav. -->
|
<!-- Table of contents; must be named manual-nav to turn off auto nav. -->
|
||||||
<div id="manual-nav">
|
<div id="manual-nav">
|
||||||
{{with .PDoc}}
|
{{with .PDoc}}
|
||||||
@ -37,6 +38,7 @@
|
|||||||
</dl>
|
</dl>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
<!-- Main page -->
|
<!-- Main page -->
|
||||||
{{with .PAst}}
|
{{with .PAst}}
|
||||||
|
@ -68,7 +68,8 @@ var (
|
|||||||
html_endp = []byte("</p>\n")
|
html_endp = []byte("</p>\n")
|
||||||
html_pre = []byte("<pre>")
|
html_pre = []byte("<pre>")
|
||||||
html_endpre = []byte("</pre>\n")
|
html_endpre = []byte("</pre>\n")
|
||||||
html_h = []byte("<h3>")
|
html_h = []byte(`<h3 id="`)
|
||||||
|
html_hq = []byte(`">`)
|
||||||
html_endh = []byte("</h3>\n")
|
html_endh = []byte("</h3>\n")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -225,6 +226,12 @@ type block struct {
|
|||||||
lines []string
|
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.
|
// ToHTML converts comment text to formatted HTML.
|
||||||
// The comment was prepared by DocReader,
|
// The comment was prepared by DocReader,
|
||||||
// so it is known not to have leading, trailing blank lines
|
// 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)
|
w.Write(html_endp)
|
||||||
case opHead:
|
case opHead:
|
||||||
w.Write(html_h)
|
w.Write(html_h)
|
||||||
|
id := ""
|
||||||
for _, line := range b.lines {
|
for _, line := range b.lines {
|
||||||
|
if id == "" {
|
||||||
|
id = anchorID(line)
|
||||||
|
w.Write([]byte(id))
|
||||||
|
w.Write(html_hq)
|
||||||
|
}
|
||||||
commentEscape(w, line, true)
|
commentEscape(w, line, true)
|
||||||
}
|
}
|
||||||
|
if id == "" {
|
||||||
|
w.Write(html_hq)
|
||||||
|
}
|
||||||
w.Write(html_endh)
|
w.Write(html_endh)
|
||||||
case opPre:
|
case opPre:
|
||||||
w.Write(html_pre)
|
w.Write(html_pre)
|
||||||
|
Loading…
Reference in New Issue
Block a user