|
65 | 65 | showTimestamps = flag.Bool("timestamps", false, "show timestamps with directory listings")
|
66 | 66 | templateDir = flag.String("templates", "", "directory containing alternate template files")
|
67 | 67 | showPlayground = flag.Bool("play", false, "enable playground in web interface")
|
| 68 | + showExamples = flag.Bool("ex", false, "show examples in command line mode") |
68 | 69 |
|
69 | 70 | // search index
|
70 | 71 | indexEnabled = flag.Bool("index", false, "enable search index")
|
@@ -329,6 +330,47 @@ func stripExampleSuffix(name string) string {
|
329 | 330 | return name
|
330 | 331 | }
|
331 | 332 |
|
| 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 | + |
332 | 374 | func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.FileSet) string {
|
333 | 375 | var buf bytes.Buffer
|
334 | 376 | for _, eg := range examples {
|
@@ -494,6 +536,7 @@ var fmap = template.FuncMap{
|
494 | 536 |
|
495 | 537 | // formatting of Examples
|
496 | 538 | "example_html": example_htmlFunc,
|
| 539 | + "example_text": example_textFunc, |
497 | 540 | "example_name": example_nameFunc,
|
498 | 541 | "example_suffix": example_suffixFunc,
|
499 | 542 | }
|
|
0 commit comments