Skip to content

Commit d97b975

Browse files
vdobleradg
authored andcommitted
cmd/godoc: show examples in text mode
Added the command line flag -ex to godoc to print examples in text output. Samples from the generated output: $ godoc -ex strings Index ... func Index(s, sep string) int Index returns the index of the first instance of sep in s, or -1 if sep is not present in s. Example: fmt.Println(strings.Index("chicken", "ken")) fmt.Println(strings.Index("chicken", "dmr")) // Output: // 4 // -1 ... $ godoc -ex container/heap ... package heap import "container/heap" Package heap provides heap operations for any type that implements heap.Interface. A heap is a tree with the property that each node is the minimum-valued node in its subtree. Example: // This example demonstrates an integer heap built using the heap interface. package heap_test import ( "container/heap" "fmt" ... Example: // This example demonstrates a priority queue built using the heap interface. package heap_test import ( "container/heap" "fmt" ) ... Fixes golang#3587. R=golang-dev, minux.ma, adg, rsc, gri CC=golang-dev https://golang.org/cl/7356043
1 parent 1174a18 commit d97b975

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

lib/godoc/package.txt

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ package {{.Name}}
99

1010
{{else}}COMMAND DOCUMENTATION
1111

12-
{{end}}{{comment_text .Doc " " "\t"}}{{/*
12+
{{end}}{{comment_text .Doc " " "\t"}}
13+
{{example_text "" $.Examples $.FSet " "}}{{/*
1314

1415
---------------------------------------
1516

@@ -36,23 +37,27 @@ FUNCTIONS
3637

3738
{{range .}}{{node .Decl $.FSet}}
3839
{{comment_text .Doc " " "\t"}}
40+
{{example_text .Name $.Examples $.FSet " "}}
3941
{{end}}{{end}}{{/*
4042

4143
---------------------------------------
4244

4345
*/}}{{with .Types}}
4446
TYPES
4547

46-
{{range .}}{{node .Decl $.FSet}}
48+
{{range .}}{{$tname := .Name}}{{node .Decl $.FSet}}
4749
{{comment_text .Doc " " "\t"}}
4850
{{range .Consts}}{{node .Decl $.FSet}}
4951
{{comment_text .Doc " " "\t"}}
5052
{{end}}{{range .Vars}}{{node .Decl $.FSet}}
5153
{{comment_text .Doc " " "\t"}}
52-
{{end}}{{range .Funcs}}{{node .Decl $.FSet}}
54+
{{end}}{{example_text .Name $.Examples $.FSet " "}}
55+
{{range .Funcs}}{{node .Decl $.FSet}}
5356
{{comment_text .Doc " " "\t"}}
57+
{{example_text .Name $.Examples $.FSet " "}}
5458
{{end}}{{range .Methods}}{{node .Decl $.FSet}}
5559
{{comment_text .Doc " " "\t"}}
60+
{{$name := printf "%s_%s" $tname .Name}}{{example_text $name $.Examples $.FSet " "}}
5661
{{end}}{{end}}{{end}}{{/*
5762

5863
---------------------------------------

src/cmd/godoc/godoc.go

+43
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var (
6565
showTimestamps = flag.Bool("timestamps", false, "show timestamps with directory listings")
6666
templateDir = flag.String("templates", "", "directory containing alternate template files")
6767
showPlayground = flag.Bool("play", false, "enable playground in web interface")
68+
showExamples = flag.Bool("ex", false, "show examples in command line mode")
6869

6970
// search index
7071
indexEnabled = flag.Bool("index", false, "enable search index")
@@ -329,6 +330,47 @@ func stripExampleSuffix(name string) string {
329330
return name
330331
}
331332

333+
func example_textFunc(funcName string, examples []*doc.Example, fset *token.FileSet, indent string) string {
334+
if !*showExamples {
335+
return ""
336+
}
337+
338+
var buf bytes.Buffer
339+
first := true
340+
for _, eg := range examples {
341+
name := stripExampleSuffix(eg.Name)
342+
if name != funcName {
343+
continue
344+
}
345+
346+
if !first {
347+
buf.WriteString("\n")
348+
}
349+
first = false
350+
351+
// print code
352+
cnode := &printer.CommentedNode{Node: eg.Code, Comments: eg.Comments}
353+
var buf1 bytes.Buffer
354+
writeNode(&buf1, fset, cnode)
355+
code := buf1.String()
356+
// Additional formatting if this is a function body.
357+
if n := len(code); n >= 2 && code[0] == '{' && code[n-1] == '}' {
358+
// remove surrounding braces
359+
code = code[1 : n-1]
360+
// unindent
361+
code = strings.Replace(code, "\n ", "\n", -1)
362+
}
363+
code = strings.Trim(code, "\n")
364+
code = strings.Replace(code, "\n", "\n\t", -1)
365+
366+
buf.WriteString(indent)
367+
buf.WriteString("Example:\n\t")
368+
buf.WriteString(code)
369+
buf.WriteString("\n")
370+
}
371+
return buf.String()
372+
}
373+
332374
func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.FileSet) string {
333375
var buf bytes.Buffer
334376
for _, eg := range examples {
@@ -494,6 +536,7 @@ var fmap = template.FuncMap{
494536

495537
// formatting of Examples
496538
"example_html": example_htmlFunc,
539+
"example_text": example_textFunc,
497540
"example_name": example_nameFunc,
498541
"example_suffix": example_suffixFunc,
499542
}

0 commit comments

Comments
 (0)