Commit f3c31306 authored by Andrew Gerrand's avatar Andrew Gerrand

godoc: support for package examples, display example suffixes

Fixes #2896.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/5677047
parent 9578839d
<div id="example_{{.Name}}" class="example"> <div id="example_{{.Name}}" class="example">
<div class="collapsed"> <div class="collapsed">
<p class="exampleHeading">▹ Example</p> <p class="exampleHeading">▹ Example{{example_suffix .Name}}</p>
</div> </div>
<div class="expanded"> <div class="expanded">
<p class="exampleHeading">▾ Example</p> <p class="exampleHeading">▾ Example{{example_suffix .Name}}</p>
<p>Code:</p> <p>Code:</p>
<pre class="code">{{.Code}}</pre> <pre class="code">{{.Code}}</pre>
{{if .Output}} {{if .Output}}
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
<h2 id="overview">Overview</h2> <h2 id="overview">Overview</h2>
<!-- The package's Name is printed as title by the top-level template --> <!-- The package's Name is printed as title by the top-level template -->
{{comment_html .Doc}} {{comment_html .Doc}}
{{example_html "" $.Examples $.FSet}}
<h2 id="index">Index</h2> <h2 id="index">Index</h2>
<!-- Table of contents for API; must be named manual-nav to turn off auto nav. --> <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
...@@ -56,7 +57,7 @@ ...@@ -56,7 +57,7 @@
<h4>Examples</h4> <h4>Examples</h4>
<dl> <dl>
{{range $.Examples}} {{range $.Examples}}
<dd><a class="exampleLink" href="#example_{{.Name}}">{{.Name}}</a></dd> <dd><a class="exampleLink" href="#example_{{.Name}}">{{example_name .Name}}</a></dd>
{{end}} {{end}}
</dl> </dl>
{{end}} {{end}}
......
...@@ -526,7 +526,7 @@ func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.File ...@@ -526,7 +526,7 @@ func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.File
err := exampleHTML.Execute(&buf, struct { err := exampleHTML.Execute(&buf, struct {
Name, Code, Output string Name, Code, Output string
}{name, code, eg.Output}) }{eg.Name, code, eg.Output})
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
...@@ -534,6 +534,38 @@ func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.File ...@@ -534,6 +534,38 @@ func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.File
return buf.String() return buf.String()
} }
// example_nameFunc takes an example function name and returns its display
// name. For example, "Foo_Bar_quux" becomes "Foo.Bar (Quux)".
func example_nameFunc(s string) string {
name, suffix := splitExampleName(s)
// replace _ with . for method names
name = strings.Replace(name, "_", ".", 1)
// use "Package" if no name provided
if name == "" {
name = "Package"
}
return name + suffix
}
// example_suffixFunc takes an example function name and returns its suffix in
// parenthesized form. For example, "Foo_Bar_quux" becomes " (Quux)".
func example_suffixFunc(name string) string {
_, suffix := splitExampleName(name)
return suffix
}
func splitExampleName(s string) (name, suffix string) {
i := strings.LastIndex(s, "_")
if 0 <= i && i < len(s)-1 && !startsWithUppercase(s[i+1:]) {
name = s[:i]
suffix = " (" + strings.Title(s[i+1:]) + ")"
return
}
name = s
return
}
func pkgLinkFunc(path string) string { func pkgLinkFunc(path string) string {
relpath := relativeURL(path) relpath := relativeURL(path)
// because of the irregular mapping under goroot // because of the irregular mapping under goroot
...@@ -610,7 +642,9 @@ var fmap = template.FuncMap{ ...@@ -610,7 +642,9 @@ var fmap = template.FuncMap{
"posLink_url": posLink_urlFunc, "posLink_url": posLink_urlFunc,
// formatting of Examples // formatting of Examples
"example_html": example_htmlFunc, "example_html": example_htmlFunc,
"example_name": example_nameFunc,
"example_suffix": example_suffixFunc,
} }
func readTemplate(name string) *template.Template { func readTemplate(name string) *template.Template {
......
...@@ -58,10 +58,7 @@ func (pq *PriorityQueue) Pop() interface{} { ...@@ -58,10 +58,7 @@ func (pq *PriorityQueue) Pop() interface{} {
} }
// 99:seven 88:five 77:zero 66:nine 55:three 44:two 33:six 22:one 11:four 00:eight // 99:seven 88:five 77:zero 66:nine 55:three 44:two 33:six 22:one 11:four 00:eight
func ExampleInterface() { func Example() {
// The full code of this example, including the methods that implement
// heap.Interface, is in the file src/pkg/container/heap/example_test.go.
const nItem = 10 const nItem = 10
// Random priorities for the items (a permutation of 0..9, times 11)). // Random priorities for the items (a permutation of 0..9, times 11)).
priorities := [nItem]int{ priorities := [nItem]int{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment