github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/godoc/godoc.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package godoc is a work-in-progress (2013-07-17) package to 6 // begin splitting up the godoc binary into multiple pieces. 7 // 8 // This package comment will evolve over time as this package splits 9 // into smaller pieces. 10 package godoc // import "github.com/jhump/golang-x-tools/godoc" 11 12 import ( 13 "bufio" 14 "bytes" 15 "fmt" 16 "go/ast" 17 "go/doc" 18 "go/format" 19 "go/printer" 20 "go/token" 21 htmltemplate "html/template" 22 "io" 23 "log" 24 "os" 25 pathpkg "path" 26 "regexp" 27 "strconv" 28 "strings" 29 "text/template" 30 "time" 31 "unicode" 32 "unicode/utf8" 33 ) 34 35 // Fake relative package path for built-ins. Documentation for all globals 36 // (not just exported ones) will be shown for packages in this directory, 37 // and there will be no association of consts, vars, and factory functions 38 // with types (see issue 6645). 39 const builtinPkgPath = "builtin" 40 41 // FuncMap defines template functions used in godoc templates. 42 // 43 // Convention: template function names ending in "_html" or "_url" produce 44 // HTML- or URL-escaped strings; all other function results may 45 // require explicit escaping in the template. 46 func (p *Presentation) FuncMap() template.FuncMap { 47 p.initFuncMapOnce.Do(p.initFuncMap) 48 return p.funcMap 49 } 50 51 func (p *Presentation) TemplateFuncs() template.FuncMap { 52 p.initFuncMapOnce.Do(p.initFuncMap) 53 return p.templateFuncs 54 } 55 56 func (p *Presentation) initFuncMap() { 57 if p.Corpus == nil { 58 panic("nil Presentation.Corpus") 59 } 60 p.templateFuncs = template.FuncMap{ 61 "code": p.code, 62 } 63 p.funcMap = template.FuncMap{ 64 // various helpers 65 "filename": filenameFunc, 66 "repeat": strings.Repeat, 67 "since": p.Corpus.pkgAPIInfo.sinceVersionFunc, 68 69 // access to FileInfos (directory listings) 70 "fileInfoName": fileInfoNameFunc, 71 "fileInfoTime": fileInfoTimeFunc, 72 73 // access to search result information 74 "infoKind_html": infoKind_htmlFunc, 75 "infoLine": p.infoLineFunc, 76 "infoSnippet_html": p.infoSnippet_htmlFunc, 77 78 // formatting of AST nodes 79 "node": p.nodeFunc, 80 "node_html": p.node_htmlFunc, 81 "comment_html": comment_htmlFunc, 82 "sanitize": sanitizeFunc, 83 84 // support for URL attributes 85 "pkgLink": pkgLinkFunc, 86 "srcLink": srcLinkFunc, 87 "posLink_url": newPosLink_urlFunc(srcPosLinkFunc), 88 "docLink": docLinkFunc, 89 "queryLink": queryLinkFunc, 90 "srcBreadcrumb": srcBreadcrumbFunc, 91 "srcToPkgLink": srcToPkgLinkFunc, 92 93 // formatting of Examples 94 "example_html": p.example_htmlFunc, 95 "example_name": p.example_nameFunc, 96 "example_suffix": p.example_suffixFunc, 97 98 // formatting of analysis information 99 "callgraph_html": p.callgraph_htmlFunc, 100 "implements_html": p.implements_htmlFunc, 101 "methodset_html": p.methodset_htmlFunc, 102 103 // formatting of Notes 104 "noteTitle": noteTitle, 105 106 // Number operation 107 "multiply": multiply, 108 109 // formatting of PageInfoMode query string 110 "modeQueryString": modeQueryString, 111 112 // check whether to display third party section or not 113 "hasThirdParty": hasThirdParty, 114 115 // get the no. of columns to split the toc in search page 116 "tocColCount": tocColCount, 117 } 118 if p.URLForSrc != nil { 119 p.funcMap["srcLink"] = p.URLForSrc 120 } 121 if p.URLForSrcPos != nil { 122 p.funcMap["posLink_url"] = newPosLink_urlFunc(p.URLForSrcPos) 123 } 124 if p.URLForSrcQuery != nil { 125 p.funcMap["queryLink"] = p.URLForSrcQuery 126 } 127 } 128 129 func multiply(a, b int) int { return a * b } 130 131 func filenameFunc(path string) string { 132 _, localname := pathpkg.Split(path) 133 return localname 134 } 135 136 func fileInfoNameFunc(fi os.FileInfo) string { 137 name := fi.Name() 138 if fi.IsDir() { 139 name += "/" 140 } 141 return name 142 } 143 144 func fileInfoTimeFunc(fi os.FileInfo) string { 145 if t := fi.ModTime(); t.Unix() != 0 { 146 return t.Local().String() 147 } 148 return "" // don't return epoch if time is obviously not set 149 } 150 151 // The strings in infoKinds must be properly html-escaped. 152 var infoKinds = [nKinds]string{ 153 PackageClause: "package clause", 154 ImportDecl: "import decl", 155 ConstDecl: "const decl", 156 TypeDecl: "type decl", 157 VarDecl: "var decl", 158 FuncDecl: "func decl", 159 MethodDecl: "method decl", 160 Use: "use", 161 } 162 163 func infoKind_htmlFunc(info SpotInfo) string { 164 return infoKinds[info.Kind()] // infoKind entries are html-escaped 165 } 166 167 func (p *Presentation) infoLineFunc(info SpotInfo) int { 168 line := info.Lori() 169 if info.IsIndex() { 170 index, _ := p.Corpus.searchIndex.Get() 171 if index != nil { 172 line = index.(*Index).Snippet(line).Line 173 } else { 174 // no line information available because 175 // we don't have an index - this should 176 // never happen; be conservative and don't 177 // crash 178 line = 0 179 } 180 } 181 return line 182 } 183 184 func (p *Presentation) infoSnippet_htmlFunc(info SpotInfo) string { 185 if info.IsIndex() { 186 index, _ := p.Corpus.searchIndex.Get() 187 // Snippet.Text was HTML-escaped when it was generated 188 return index.(*Index).Snippet(info.Lori()).Text 189 } 190 return `<span class="alert">no snippet text available</span>` 191 } 192 193 func (p *Presentation) nodeFunc(info *PageInfo, node interface{}) string { 194 var buf bytes.Buffer 195 p.writeNode(&buf, info, info.FSet, node) 196 return buf.String() 197 } 198 199 func (p *Presentation) node_htmlFunc(info *PageInfo, node interface{}, linkify bool) string { 200 var buf1 bytes.Buffer 201 p.writeNode(&buf1, info, info.FSet, node) 202 203 var buf2 bytes.Buffer 204 if n, _ := node.(ast.Node); n != nil && linkify && p.DeclLinks { 205 LinkifyText(&buf2, buf1.Bytes(), n) 206 if st, name := isStructTypeDecl(n); st != nil { 207 addStructFieldIDAttributes(&buf2, name, st) 208 } 209 } else { 210 FormatText(&buf2, buf1.Bytes(), -1, true, "", nil) 211 } 212 213 return buf2.String() 214 } 215 216 // isStructTypeDecl checks whether n is a struct declaration. 217 // It either returns a non-nil StructType and its name, or zero values. 218 func isStructTypeDecl(n ast.Node) (st *ast.StructType, name string) { 219 gd, ok := n.(*ast.GenDecl) 220 if !ok || gd.Tok != token.TYPE { 221 return nil, "" 222 } 223 if gd.Lparen > 0 { 224 // Parenthesized type. Who does that, anyway? 225 // TODO: Reportedly gri does. Fix this to handle that too. 226 return nil, "" 227 } 228 if len(gd.Specs) != 1 { 229 return nil, "" 230 } 231 ts, ok := gd.Specs[0].(*ast.TypeSpec) 232 if !ok { 233 return nil, "" 234 } 235 st, ok = ts.Type.(*ast.StructType) 236 if !ok { 237 return nil, "" 238 } 239 return st, ts.Name.Name 240 } 241 242 // addStructFieldIDAttributes modifies the contents of buf such that 243 // all struct fields of the named struct have <span id='name.Field'> 244 // in them, so people can link to /#Struct.Field. 245 func addStructFieldIDAttributes(buf *bytes.Buffer, name string, st *ast.StructType) { 246 if st.Fields == nil { 247 return 248 } 249 // needsLink is a set of identifiers that still need to be 250 // linked, where value == key, to avoid an allocation in func 251 // linkedField. 252 needsLink := make(map[string]string) 253 254 for _, f := range st.Fields.List { 255 if len(f.Names) == 0 { 256 continue 257 } 258 fieldName := f.Names[0].Name 259 needsLink[fieldName] = fieldName 260 } 261 var newBuf bytes.Buffer 262 foreachLine(buf.Bytes(), func(line []byte) { 263 if fieldName := linkedField(line, needsLink); fieldName != "" { 264 fmt.Fprintf(&newBuf, `<span id="%s.%s"></span>`, name, fieldName) 265 delete(needsLink, fieldName) 266 } 267 newBuf.Write(line) 268 }) 269 buf.Reset() 270 buf.Write(newBuf.Bytes()) 271 } 272 273 // foreachLine calls fn for each line of in, where a line includes 274 // the trailing "\n", except on the last line, if it doesn't exist. 275 func foreachLine(in []byte, fn func(line []byte)) { 276 for len(in) > 0 { 277 nl := bytes.IndexByte(in, '\n') 278 if nl == -1 { 279 fn(in) 280 return 281 } 282 fn(in[:nl+1]) 283 in = in[nl+1:] 284 } 285 } 286 287 // commentPrefix is the line prefix for comments after they've been HTMLified. 288 var commentPrefix = []byte(`<span class="comment">// `) 289 290 // linkedField determines whether the given line starts with an 291 // identifier in the provided ids map (mapping from identifier to the 292 // same identifier). The line can start with either an identifier or 293 // an identifier in a comment. If one matches, it returns the 294 // identifier that matched. Otherwise it returns the empty string. 295 func linkedField(line []byte, ids map[string]string) string { 296 line = bytes.TrimSpace(line) 297 298 // For fields with a doc string of the 299 // conventional form, we put the new span into 300 // the comment instead of the field. 301 // The "conventional" form is a complete sentence 302 // per https://golang.org/s/style#comment-sentences like: 303 // 304 // // Foo is an optional Fooer to foo the foos. 305 // Foo Fooer 306 // 307 // In this case, we want the #StructName.Foo 308 // link to make the browser go to the comment 309 // line "Foo is an optional Fooer" instead of 310 // the "Foo Fooer" line, which could otherwise 311 // obscure the docs above the browser's "fold". 312 // 313 // TODO: do this better, so it works for all 314 // comments, including unconventional ones. 315 line = bytes.TrimPrefix(line, commentPrefix) 316 id := scanIdentifier(line) 317 if len(id) == 0 { 318 // No leading identifier. Avoid map lookup for 319 // somewhat common case. 320 return "" 321 } 322 return ids[string(id)] 323 } 324 325 // scanIdentifier scans a valid Go identifier off the front of v and 326 // either returns a subslice of v if there's a valid identifier, or 327 // returns a zero-length slice. 328 func scanIdentifier(v []byte) []byte { 329 var n int // number of leading bytes of v belonging to an identifier 330 for { 331 r, width := utf8.DecodeRune(v[n:]) 332 if !(isLetter(r) || n > 0 && isDigit(r)) { 333 break 334 } 335 n += width 336 } 337 return v[:n] 338 } 339 340 func isLetter(ch rune) bool { 341 return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= utf8.RuneSelf && unicode.IsLetter(ch) 342 } 343 344 func isDigit(ch rune) bool { 345 return '0' <= ch && ch <= '9' || ch >= utf8.RuneSelf && unicode.IsDigit(ch) 346 } 347 348 func comment_htmlFunc(comment string) string { 349 var buf bytes.Buffer 350 // TODO(gri) Provide list of words (e.g. function parameters) 351 // to be emphasized by ToHTML. 352 doc.ToHTML(&buf, comment, nil) // does html-escaping 353 return buf.String() 354 } 355 356 // sanitizeFunc sanitizes the argument src by replacing newlines with 357 // blanks, removing extra blanks, and by removing trailing whitespace 358 // and commas before closing parentheses. 359 func sanitizeFunc(src string) string { 360 buf := make([]byte, len(src)) 361 j := 0 // buf index 362 comma := -1 // comma index if >= 0 363 for i := 0; i < len(src); i++ { 364 ch := src[i] 365 switch ch { 366 case '\t', '\n', ' ': 367 // ignore whitespace at the beginning, after a blank, or after opening parentheses 368 if j == 0 { 369 continue 370 } 371 if p := buf[j-1]; p == ' ' || p == '(' || p == '{' || p == '[' { 372 continue 373 } 374 // replace all whitespace with blanks 375 ch = ' ' 376 case ',': 377 comma = j 378 case ')', '}', ']': 379 // remove any trailing comma 380 if comma >= 0 { 381 j = comma 382 } 383 // remove any trailing whitespace 384 if j > 0 && buf[j-1] == ' ' { 385 j-- 386 } 387 default: 388 comma = -1 389 } 390 buf[j] = ch 391 j++ 392 } 393 // remove trailing blank, if any 394 if j > 0 && buf[j-1] == ' ' { 395 j-- 396 } 397 return string(buf[:j]) 398 } 399 400 type PageInfo struct { 401 Dirname string // directory containing the package 402 Err error // error or nil 403 404 Mode PageInfoMode // display metadata from query string 405 406 // package info 407 FSet *token.FileSet // nil if no package documentation 408 PDoc *doc.Package // nil if no package documentation 409 Examples []*doc.Example // nil if no example code 410 Notes map[string][]*doc.Note // nil if no package Notes 411 PAst map[string]*ast.File // nil if no AST with package exports 412 IsMain bool // true for package main 413 IsFiltered bool // true if results were filtered 414 415 // analysis info 416 TypeInfoIndex map[string]int // index of JSON datum for type T (if -analysis=type) 417 AnalysisData htmltemplate.JS // array of TypeInfoJSON values 418 CallGraph htmltemplate.JS // array of PCGNodeJSON values (if -analysis=pointer) 419 CallGraphIndex map[string]int // maps func name to index in CallGraph 420 421 // directory info 422 Dirs *DirList // nil if no directory information 423 DirTime time.Time // directory time stamp 424 DirFlat bool // if set, show directory in a flat (non-indented) manner 425 } 426 427 func (info *PageInfo) IsEmpty() bool { 428 return info.Err != nil || info.PAst == nil && info.PDoc == nil && info.Dirs == nil 429 } 430 431 func pkgLinkFunc(path string) string { 432 // because of the irregular mapping under goroot 433 // we need to correct certain relative paths 434 path = strings.TrimPrefix(path, "/") 435 path = strings.TrimPrefix(path, "src/") 436 path = strings.TrimPrefix(path, "pkg/") 437 return "pkg/" + path 438 } 439 440 // srcToPkgLinkFunc builds an <a> tag linking to the package 441 // documentation of relpath. 442 func srcToPkgLinkFunc(relpath string) string { 443 relpath = pkgLinkFunc(relpath) 444 relpath = pathpkg.Dir(relpath) 445 if relpath == "pkg" { 446 return `<a href="/pkg">Index</a>` 447 } 448 return fmt.Sprintf(`<a href="/%s">%s</a>`, relpath, relpath[len("pkg/"):]) 449 } 450 451 // srcBreadcrumbFun converts each segment of relpath to a HTML <a>. 452 // Each segment links to its corresponding src directories. 453 func srcBreadcrumbFunc(relpath string) string { 454 segments := strings.Split(relpath, "/") 455 var buf bytes.Buffer 456 var selectedSegment string 457 var selectedIndex int 458 459 if strings.HasSuffix(relpath, "/") { 460 // relpath is a directory ending with a "/". 461 // Selected segment is the segment before the last slash. 462 selectedIndex = len(segments) - 2 463 selectedSegment = segments[selectedIndex] + "/" 464 } else { 465 selectedIndex = len(segments) - 1 466 selectedSegment = segments[selectedIndex] 467 } 468 469 for i := range segments[:selectedIndex] { 470 buf.WriteString(fmt.Sprintf(`<a href="/%s">%s</a>/`, 471 strings.Join(segments[:i+1], "/"), 472 segments[i], 473 )) 474 } 475 476 buf.WriteString(`<span class="text-muted">`) 477 buf.WriteString(selectedSegment) 478 buf.WriteString(`</span>`) 479 return buf.String() 480 } 481 482 func newPosLink_urlFunc(srcPosLinkFunc func(s string, line, low, high int) string) func(info *PageInfo, n interface{}) string { 483 // n must be an ast.Node or a *doc.Note 484 return func(info *PageInfo, n interface{}) string { 485 var pos, end token.Pos 486 487 switch n := n.(type) { 488 case ast.Node: 489 pos = n.Pos() 490 end = n.End() 491 case *doc.Note: 492 pos = n.Pos 493 end = n.End 494 default: 495 panic(fmt.Sprintf("wrong type for posLink_url template formatter: %T", n)) 496 } 497 498 var relpath string 499 var line int 500 var low, high int // selection offset range 501 502 if pos.IsValid() { 503 p := info.FSet.Position(pos) 504 relpath = p.Filename 505 line = p.Line 506 low = p.Offset 507 } 508 if end.IsValid() { 509 high = info.FSet.Position(end).Offset 510 } 511 512 return srcPosLinkFunc(relpath, line, low, high) 513 } 514 } 515 516 func srcPosLinkFunc(s string, line, low, high int) string { 517 s = srcLinkFunc(s) 518 var buf bytes.Buffer 519 template.HTMLEscape(&buf, []byte(s)) 520 // selection ranges are of form "s=low:high" 521 if low < high { 522 fmt.Fprintf(&buf, "?s=%d:%d", low, high) // no need for URL escaping 523 // if we have a selection, position the page 524 // such that the selection is a bit below the top 525 line -= 10 526 if line < 1 { 527 line = 1 528 } 529 } 530 // line id's in html-printed source are of the 531 // form "L%d" where %d stands for the line number 532 if line > 0 { 533 fmt.Fprintf(&buf, "#L%d", line) // no need for URL escaping 534 } 535 return buf.String() 536 } 537 538 func srcLinkFunc(s string) string { 539 s = pathpkg.Clean("/" + s) 540 if !strings.HasPrefix(s, "/src/") { 541 s = "/src" + s 542 } 543 return s 544 } 545 546 // queryLinkFunc returns a URL for a line in a source file with a highlighted 547 // query term. 548 // s is expected to be a path to a source file. 549 // query is expected to be a string that has already been appropriately escaped 550 // for use in a URL query. 551 func queryLinkFunc(s, query string, line int) string { 552 url := pathpkg.Clean("/"+s) + "?h=" + query 553 if line > 0 { 554 url += "#L" + strconv.Itoa(line) 555 } 556 return url 557 } 558 559 func docLinkFunc(s string, ident string) string { 560 return pathpkg.Clean("/pkg/"+s) + "/#" + ident 561 } 562 563 func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string { 564 var buf bytes.Buffer 565 for _, eg := range info.Examples { 566 name := stripExampleSuffix(eg.Name) 567 568 if name != funcName { 569 continue 570 } 571 572 // print code 573 cnode := &printer.CommentedNode{Node: eg.Code, Comments: eg.Comments} 574 code := p.node_htmlFunc(info, cnode, true) 575 out := eg.Output 576 wholeFile := true 577 578 // Additional formatting if this is a function body. 579 if n := len(code); n >= 2 && code[0] == '{' && code[n-1] == '}' { 580 wholeFile = false 581 // remove surrounding braces 582 code = code[1 : n-1] 583 // unindent 584 code = replaceLeadingIndentation(code, strings.Repeat(" ", p.TabWidth), "") 585 // remove output comment 586 if loc := exampleOutputRx.FindStringIndex(code); loc != nil { 587 code = strings.TrimSpace(code[:loc[0]]) 588 } 589 } 590 591 // Write out the playground code in standard Go style 592 // (use tabs, no comment highlight, etc). 593 play := "" 594 if eg.Play != nil && p.ShowPlayground { 595 var buf bytes.Buffer 596 eg.Play.Comments = filterOutBuildAnnotations(eg.Play.Comments) 597 if err := format.Node(&buf, info.FSet, eg.Play); err != nil { 598 log.Print(err) 599 } else { 600 play = buf.String() 601 } 602 } 603 604 // Drop output, as the output comment will appear in the code. 605 if wholeFile && play == "" { 606 out = "" 607 } 608 609 if p.ExampleHTML == nil { 610 out = "" 611 return "" 612 } 613 614 err := p.ExampleHTML.Execute(&buf, struct { 615 Name, Doc, Code, Play, Output string 616 }{eg.Name, eg.Doc, code, play, out}) 617 if err != nil { 618 log.Print(err) 619 } 620 } 621 return buf.String() 622 } 623 624 func filterOutBuildAnnotations(cg []*ast.CommentGroup) []*ast.CommentGroup { 625 if len(cg) == 0 { 626 return cg 627 } 628 629 for i := range cg { 630 if !strings.HasPrefix(cg[i].Text(), "+build ") { 631 // Found the first non-build tag, return from here until the end 632 // of the slice. 633 return cg[i:] 634 } 635 } 636 637 // There weren't any non-build tags, return an empty slice. 638 return []*ast.CommentGroup{} 639 } 640 641 // example_nameFunc takes an example function name and returns its display 642 // name. For example, "Foo_Bar_quux" becomes "Foo.Bar (Quux)". 643 func (p *Presentation) example_nameFunc(s string) string { 644 name, suffix := splitExampleName(s) 645 // replace _ with . for method names 646 name = strings.Replace(name, "_", ".", 1) 647 // use "Package" if no name provided 648 if name == "" { 649 name = "Package" 650 } 651 return name + suffix 652 } 653 654 // example_suffixFunc takes an example function name and returns its suffix in 655 // parenthesized form. For example, "Foo_Bar_quux" becomes " (Quux)". 656 func (p *Presentation) example_suffixFunc(name string) string { 657 _, suffix := splitExampleName(name) 658 return suffix 659 } 660 661 // implements_html returns the "> Implements" toggle for a package-level named type. 662 // Its contents are populated from JSON data by client-side JS at load time. 663 func (p *Presentation) implements_htmlFunc(info *PageInfo, typeName string) string { 664 if p.ImplementsHTML == nil { 665 return "" 666 } 667 index, ok := info.TypeInfoIndex[typeName] 668 if !ok { 669 return "" 670 } 671 var buf bytes.Buffer 672 err := p.ImplementsHTML.Execute(&buf, struct{ Index int }{index}) 673 if err != nil { 674 log.Print(err) 675 } 676 return buf.String() 677 } 678 679 // methodset_html returns the "> Method set" toggle for a package-level named type. 680 // Its contents are populated from JSON data by client-side JS at load time. 681 func (p *Presentation) methodset_htmlFunc(info *PageInfo, typeName string) string { 682 if p.MethodSetHTML == nil { 683 return "" 684 } 685 index, ok := info.TypeInfoIndex[typeName] 686 if !ok { 687 return "" 688 } 689 var buf bytes.Buffer 690 err := p.MethodSetHTML.Execute(&buf, struct{ Index int }{index}) 691 if err != nil { 692 log.Print(err) 693 } 694 return buf.String() 695 } 696 697 // callgraph_html returns the "> Call graph" toggle for a package-level func. 698 // Its contents are populated from JSON data by client-side JS at load time. 699 func (p *Presentation) callgraph_htmlFunc(info *PageInfo, recv, name string) string { 700 if p.CallGraphHTML == nil { 701 return "" 702 } 703 if recv != "" { 704 // Format must match (*ssa.Function).RelString(). 705 name = fmt.Sprintf("(%s).%s", recv, name) 706 } 707 index, ok := info.CallGraphIndex[name] 708 if !ok { 709 return "" 710 } 711 var buf bytes.Buffer 712 err := p.CallGraphHTML.Execute(&buf, struct{ Index int }{index}) 713 if err != nil { 714 log.Print(err) 715 } 716 return buf.String() 717 } 718 719 func noteTitle(note string) string { 720 return strings.Title(strings.ToLower(note)) 721 } 722 723 func startsWithUppercase(s string) bool { 724 r, _ := utf8.DecodeRuneInString(s) 725 return unicode.IsUpper(r) 726 } 727 728 var exampleOutputRx = regexp.MustCompile(`(?i)//[[:space:]]*(unordered )?output:`) 729 730 // stripExampleSuffix strips lowercase braz in Foo_braz or Foo_Bar_braz from name 731 // while keeping uppercase Braz in Foo_Braz. 732 func stripExampleSuffix(name string) string { 733 if i := strings.LastIndex(name, "_"); i != -1 { 734 if i < len(name)-1 && !startsWithUppercase(name[i+1:]) { 735 name = name[:i] 736 } 737 } 738 return name 739 } 740 741 func splitExampleName(s string) (name, suffix string) { 742 i := strings.LastIndex(s, "_") 743 if 0 <= i && i < len(s)-1 && !startsWithUppercase(s[i+1:]) { 744 name = s[:i] 745 suffix = " (" + strings.Title(s[i+1:]) + ")" 746 return 747 } 748 name = s 749 return 750 } 751 752 // replaceLeadingIndentation replaces oldIndent at the beginning of each line 753 // with newIndent. This is used for formatting examples. Raw strings that 754 // span multiple lines are handled specially: oldIndent is not removed (since 755 // go/printer will not add any indentation there), but newIndent is added 756 // (since we may still want leading indentation). 757 func replaceLeadingIndentation(body, oldIndent, newIndent string) string { 758 // Handle indent at the beginning of the first line. After this, we handle 759 // indentation only after a newline. 760 var buf bytes.Buffer 761 if strings.HasPrefix(body, oldIndent) { 762 buf.WriteString(newIndent) 763 body = body[len(oldIndent):] 764 } 765 766 // Use a state machine to keep track of whether we're in a string or 767 // rune literal while we process the rest of the code. 768 const ( 769 codeState = iota 770 runeState 771 interpretedStringState 772 rawStringState 773 ) 774 searchChars := []string{ 775 "'\"`\n", // codeState 776 `\'`, // runeState 777 `\"`, // interpretedStringState 778 "`\n", // rawStringState 779 // newlineState does not need to search 780 } 781 state := codeState 782 for { 783 i := strings.IndexAny(body, searchChars[state]) 784 if i < 0 { 785 buf.WriteString(body) 786 break 787 } 788 c := body[i] 789 buf.WriteString(body[:i+1]) 790 body = body[i+1:] 791 switch state { 792 case codeState: 793 switch c { 794 case '\'': 795 state = runeState 796 case '"': 797 state = interpretedStringState 798 case '`': 799 state = rawStringState 800 case '\n': 801 if strings.HasPrefix(body, oldIndent) { 802 buf.WriteString(newIndent) 803 body = body[len(oldIndent):] 804 } 805 } 806 807 case runeState: 808 switch c { 809 case '\\': 810 r, size := utf8.DecodeRuneInString(body) 811 buf.WriteRune(r) 812 body = body[size:] 813 case '\'': 814 state = codeState 815 } 816 817 case interpretedStringState: 818 switch c { 819 case '\\': 820 r, size := utf8.DecodeRuneInString(body) 821 buf.WriteRune(r) 822 body = body[size:] 823 case '"': 824 state = codeState 825 } 826 827 case rawStringState: 828 switch c { 829 case '`': 830 state = codeState 831 case '\n': 832 buf.WriteString(newIndent) 833 } 834 } 835 } 836 return buf.String() 837 } 838 839 // writeNode writes the AST node x to w. 840 // 841 // The provided fset must be non-nil. The pageInfo is optional. If 842 // present, the pageInfo is used to add comments to struct fields to 843 // say which version of Go introduced them. 844 func (p *Presentation) writeNode(w io.Writer, pageInfo *PageInfo, fset *token.FileSet, x interface{}) { 845 // convert trailing tabs into spaces using a tconv filter 846 // to ensure a good outcome in most browsers (there may still 847 // be tabs in comments and strings, but converting those into 848 // the right number of spaces is much harder) 849 // 850 // TODO(gri) rethink printer flags - perhaps tconv can be eliminated 851 // with an another printer mode (which is more efficiently 852 // implemented in the printer than here with another layer) 853 854 var pkgName, structName string 855 var apiInfo pkgAPIVersions 856 if gd, ok := x.(*ast.GenDecl); ok && pageInfo != nil && pageInfo.PDoc != nil && 857 p.Corpus != nil && 858 gd.Tok == token.TYPE && len(gd.Specs) != 0 { 859 pkgName = pageInfo.PDoc.ImportPath 860 if ts, ok := gd.Specs[0].(*ast.TypeSpec); ok { 861 if _, ok := ts.Type.(*ast.StructType); ok { 862 structName = ts.Name.Name 863 } 864 } 865 apiInfo = p.Corpus.pkgAPIInfo[pkgName] 866 } 867 868 var out = w 869 var buf bytes.Buffer 870 if structName != "" { 871 out = &buf 872 } 873 874 mode := printer.TabIndent | printer.UseSpaces 875 err := (&printer.Config{Mode: mode, Tabwidth: p.TabWidth}).Fprint(&tconv{p: p, output: out}, fset, x) 876 if err != nil { 877 log.Print(err) 878 } 879 880 // Add comments to struct fields saying which Go version introduced them. 881 if structName != "" { 882 fieldSince := apiInfo.fieldSince[structName] 883 typeSince := apiInfo.typeSince[structName] 884 // Add/rewrite comments on struct fields to note which Go version added them. 885 var buf2 bytes.Buffer 886 buf2.Grow(buf.Len() + len(" // Added in Go 1.n")*10) 887 bs := bufio.NewScanner(&buf) 888 for bs.Scan() { 889 line := bs.Bytes() 890 field := firstIdent(line) 891 var since string 892 if field != "" { 893 since = fieldSince[field] 894 if since != "" && since == typeSince { 895 // Don't highlight field versions if they were the 896 // same as the struct itself. 897 since = "" 898 } 899 } 900 if since == "" { 901 buf2.Write(line) 902 } else { 903 if bytes.Contains(line, slashSlash) { 904 line = bytes.TrimRight(line, " \t.") 905 buf2.Write(line) 906 buf2.WriteString("; added in Go ") 907 } else { 908 buf2.Write(line) 909 buf2.WriteString(" // Go ") 910 } 911 buf2.WriteString(since) 912 } 913 buf2.WriteByte('\n') 914 } 915 w.Write(buf2.Bytes()) 916 } 917 } 918 919 var slashSlash = []byte("//") 920 921 // WriteNode writes x to w. 922 // TODO(bgarcia) Is this method needed? It's just a wrapper for p.writeNode. 923 func (p *Presentation) WriteNode(w io.Writer, fset *token.FileSet, x interface{}) { 924 p.writeNode(w, nil, fset, x) 925 } 926 927 // firstIdent returns the first identifier in x. 928 // This actually parses "identifiers" that begin with numbers too, but we 929 // never feed it such input, so it's fine. 930 func firstIdent(x []byte) string { 931 x = bytes.TrimSpace(x) 932 i := bytes.IndexFunc(x, func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) }) 933 if i == -1 { 934 return string(x) 935 } 936 return string(x[:i]) 937 }