Source file
src/go/doc/headscan.go
1
2
3
4
5
6
7
16 package main
17
18 import (
19 "bytes"
20 "flag"
21 "fmt"
22 "go/doc"
23 "go/parser"
24 "go/token"
25 "io/fs"
26 "os"
27 "path/filepath"
28 "regexp"
29 "runtime"
30 "strings"
31 )
32
33 var (
34 root = flag.String("root", filepath.Join(runtime.GOROOT(), "src"), "root of filesystem tree to scan")
35 verbose = flag.Bool("v", false, "verbose mode")
36 )
37
38
39 var html_h = regexp.MustCompile(`<h3 id="[^"]*">`)
40
41 const html_endh = "</h3>\n"
42
43 func isGoFile(fi fs.FileInfo) bool {
44 return strings.HasSuffix(fi.Name(), ".go") &&
45 !strings.HasSuffix(fi.Name(), "_test.go")
46 }
47
48 func appendHeadings(list []string, comment string) []string {
49 var buf bytes.Buffer
50 doc.ToHTML(&buf, comment, nil)
51 for s := buf.String(); s != ""; {
52 loc := html_h.FindStringIndex(s)
53 if len(loc) == 0 {
54 break
55 }
56 var inner string
57 inner, s, _ = strings.Cut(s[loc[1]:], html_endh)
58 list = append(list, inner)
59 }
60 return list
61 }
62
63 func main() {
64 flag.Parse()
65 fset := token.NewFileSet()
66 nheadings := 0
67 err := filepath.WalkDir(*root, func(path string, info fs.DirEntry, err error) error {
68 if !info.IsDir() {
69 return nil
70 }
71 pkgs, err := parser.ParseDir(fset, path, isGoFile, parser.ParseComments)
72 if err != nil {
73 if *verbose {
74 fmt.Fprintln(os.Stderr, err)
75 }
76 return nil
77 }
78 for _, pkg := range pkgs {
79 d := doc.New(pkg, path, doc.Mode(0))
80 list := appendHeadings(nil, d.Doc)
81 for _, d := range d.Consts {
82 list = appendHeadings(list, d.Doc)
83 }
84 for _, d := range d.Types {
85 list = appendHeadings(list, d.Doc)
86 }
87 for _, d := range d.Vars {
88 list = appendHeadings(list, d.Doc)
89 }
90 for _, d := range d.Funcs {
91 list = appendHeadings(list, d.Doc)
92 }
93 if len(list) > 0 {
94
95
96 fmt.Printf("%s (package %s)\n", path, pkg.Name)
97 for _, h := range list {
98 fmt.Printf("\t%s\n", h)
99 }
100 nheadings += len(list)
101 }
102 }
103 return nil
104 })
105 if err != nil {
106 fmt.Fprintln(os.Stderr, err)
107 os.Exit(1)
108 }
109 fmt.Println(nheadings, "headings found")
110 }
111
View as plain text