diff options
| author | Russ Cox <rsc@golang.org> | 2021-02-26 01:53:00 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2021-06-14 17:41:38 +0000 |
| commit | 5ee419f2eadb4e9213f52ffc1f6fea62bb55fbb7 (patch) | |
| tree | 16a235ec5ef361c53b1ca58034adc77a119882a9 /internal/pkgdoc/doc.go | |
| parent | 4b5486d0e5c44f3e96aea9d5d8a00185872c416f (diff) | |
| download | go-x-website-5ee419f2eadb4e9213f52ffc1f6fea62bb55fbb7.tar.xz | |
all: remove toFS usage
The toFS calls were a stop-gap to convert from old code that wasn't
strict about path forms to the io/fs routines that are more strict.
Arrange to pass io/fs-compatible paths everywhere and remove toFS.
Change-Id: Id69c0f23074ebd3a6dfef2255b2f8185ad1d1249
Reviewed-on: https://go-review.googlesource.com/c/website/+/317659
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Diffstat (limited to 'internal/pkgdoc/doc.go')
| -rw-r--r-- | internal/pkgdoc/doc.go | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/internal/pkgdoc/doc.go b/internal/pkgdoc/doc.go index 02e7b5b3..218228ea 100644 --- a/internal/pkgdoc/doc.go +++ b/internal/pkgdoc/doc.go @@ -32,9 +32,9 @@ type Docs struct { } func NewDocs(fsys fs.FS) *Docs { - src := newDir(fsys, token.NewFileSet(), "/src") + src := newDir(fsys, token.NewFileSet(), "src") root := &Dir{ - Path: "/", + Path: ".", Dirs: []*Dir{src}, } return &Docs{ @@ -112,14 +112,15 @@ func ParseMode(text string) Mode { return mode } -// Doc returns the Page for a package directory abspath. +// Doc returns the Page for a package directory dir. // Package documentation (Page.PDoc) is extracted from the AST. // If there is no corresponding package in the // directory, Page.PDoc is nil. If there are no sub- // directories, Page.Dirs is nil. If an error occurred, PageInfo.Err is // set to the respective error but the error is not logged. -func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page { - info := &Page{Dirname: abspath, Mode: mode} +func Doc(d *Docs, dir string, mode Mode, goos, goarch string) *Page { + dir = path.Clean(dir) + info := &Page{Dirname: dir, Mode: mode} // Restrict to the package files that would be used when building // the package on this system. This makes sure that if there are @@ -130,11 +131,11 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page ctxt := build.Default ctxt.IsAbsPath = path.IsAbs ctxt.IsDir = func(path string) bool { - fi, err := fs.Stat(d.fs, toFS(filepath.ToSlash(path))) + fi, err := fs.Stat(d.fs, filepath.ToSlash(path)) return err == nil && fi.IsDir() } ctxt.ReadDir = func(dir string) ([]os.FileInfo, error) { - f, err := fs.ReadDir(d.fs, toFS(filepath.ToSlash(dir))) + f, err := fs.ReadDir(d.fs, filepath.ToSlash(dir)) filtered := make([]os.FileInfo, 0, len(f)) for _, i := range f { if mode&ModeAll != 0 || i.Name() != "internal" { @@ -147,7 +148,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page return filtered, err } ctxt.OpenFile = func(name string) (r io.ReadCloser, err error) { - data, err := fs.ReadFile(d.fs, toFS(filepath.ToSlash(name))) + data, err := fs.ReadFile(d.fs, filepath.ToSlash(name)) if err != nil { return nil, err } @@ -159,7 +160,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page // linux/amd64 means the wasm syscall/js package was blank. // And you can't run godoc on js/wasm anyway, so host defaults // don't make sense here. - if goos == "" && goarch == "" && relpath == "syscall/js" { + if goos == "" && goarch == "" && dir == "syscall/js" { goos, goarch = "js", "wasm" } if goos != "" { @@ -169,7 +170,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page ctxt.GOARCH = goarch } - pkginfo, err := ctxt.ImportDir(abspath, 0) + pkginfo, err := ctxt.ImportDir(dir, 0) // continue if there are no Go source files; we still want the directory info if _, nogo := err.(*build.NoGoError); err != nil && !nogo { info.Err = err @@ -193,7 +194,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page if len(pkgfiles) > 0 { // build package AST fset := token.NewFileSet() - files, err := parseFiles(d.fs, fset, relpath, abspath, pkgfiles) + files, err := parseFiles(d.fs, fset, dir, pkgfiles) if err != nil { info.Err = err return info @@ -213,7 +214,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page if mode&ModeMethods != 0 { m |= doc.AllMethods } - info.PDoc = doc.New(pkg, path.Clean(relpath), m) // no trailing '/' in importpath + info.PDoc = doc.New(pkg, strings.TrimPrefix(dir, "src/"), m) if mode&ModeBuiltin != 0 { for _, t := range info.PDoc.Types { info.PDoc.Consts = append(info.PDoc.Consts, t.Consts...) @@ -230,7 +231,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page // collect examples testfiles := append(pkginfo.TestGoFiles, pkginfo.XTestGoFiles...) - files, err = parseFiles(d.fs, fset, relpath, abspath, testfiles) + files, err = parseFiles(d.fs, fset, dir, testfiles) if err != nil { log.Println("parsing examples:", err) } @@ -238,7 +239,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page info.Bugs = info.PDoc.Notes["BUG"] } - info.Dirs = d.root.Lookup(abspath).List(func(path string) bool { return d.includePath(path, mode) }) + info.Dirs = d.root.Lookup(dir).List(func(path string) bool { return d.includePath(path, mode) }) info.DirFlat = mode&ModeFlat != 0 return info |
