1
0
mirror of https://github.com/golang/go synced 2024-11-21 15:34:45 -07:00

godoc: list examples in index

Fixes #2897.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5658047
This commit is contained in:
Andrew Gerrand 2012-02-14 14:52:00 +11:00
parent e3f11b3f3c
commit 5ab935f68a
4 changed files with 30 additions and 7 deletions

View File

@ -23,7 +23,7 @@ bindEvent(window, 'load', godocs_onload);
function godocs_onload() {
godocs_bindSearchEvents();
godocs_generateTOC();
godocs_bindExampleToggles();
godocs_bindExamples();
}
function godocs_bindSearchEvents() {
@ -158,11 +158,14 @@ function godocs_nodeToText(node) {
return text;
}
function godocs_bindExampleToggles() {
function godocs_bindExamples() {
var examples = document.getElementsByClassName("example");
for (var i = 0; i < examples.length; i++) {
var eg = examples[i];
godocs_bindExampleToggle(eg);
godocs_bindExampleToggle(examples[i]);
}
var links = document.getElementsByClassName("exampleLink");
for (var i = 0; i < links.length; i++) {
godocs_bindExampleLink(links[i]);
}
}
function godocs_bindExampleToggle(eg) {
@ -177,3 +180,14 @@ function godocs_bindExampleToggle(eg) {
});
}
}
function godocs_bindExampleLink(l) {
var prefix = "example_";
bindEvent(l, "click", function() {
var i = l.href.indexOf("#"+prefix);
if (i < 0)
return;
var id = prefix + l.href.slice(i+1+prefix.length);
var eg = document.getElementById(id);
eg.className = "exampleVisible";
});
}

View File

@ -1,4 +1,4 @@
<div class="example">
<div id="example_{{.Name}}" class="example">
<div class="collapsed">
<p class="exampleHeading">▹ Example</p>
</div>

View File

@ -52,6 +52,15 @@
{{end}}
</dl>
{{if $.Examples}}
<h4>Examples</h4>
<dl>
{{range $.Examples}}
<dd><a class="exampleLink" href="#example_{{.Name}}">{{.Name}}</a></dd>
{{end}}
</dl>
{{end}}
{{with .Filenames}}
<h4>Package files</h4>
<p>

View File

@ -522,8 +522,8 @@ func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.File
code = code[2 : len(code)-2]
err := exampleHTML.Execute(&buf, struct {
Code, Output string
}{code, eg.Output})
Name, Code, Output string
}{name, code, eg.Output})
if err != nil {
log.Print(err)
}