mirror of
https://github.com/golang/go
synced 2024-11-21 15:44:44 -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:
parent
e3f11b3f3c
commit
5ab935f68a
@ -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";
|
||||
});
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
<div class="example">
|
||||
<div id="example_{{.Name}}" class="example">
|
||||
<div class="collapsed">
|
||||
<p class="exampleHeading">▹ Example</p>
|
||||
</div>
|
||||
|
@ -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>
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user