Skip to content

Commit d3a8571

Browse files
authored
fix(internal/godocfx): rename README files to pkg-readme (#3185)
1 parent e07801c commit d3a8571

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

internal/godocfx/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ func write(outDir string, r *result) error {
212212
}
213213
}
214214

215-
for _, path := range r.extraFiles {
216-
src, err := os.Open(filepath.Join(r.module.Dir, path))
215+
for _, ef := range r.extraFiles {
216+
src, err := os.Open(filepath.Join(r.module.Dir, ef.srcRelativePath))
217217
if err != nil {
218218
return err
219219
}
220-
dst, err := os.Create(filepath.Join(outDir, path))
220+
dst, err := os.Create(filepath.Join(outDir, ef.dstRelativePath))
221221
if err != nil {
222222
return err
223223
}

internal/godocfx/parse.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,23 @@ func (i *item) addChild(c child) {
9595

9696
var onlyGo = []string{"go"}
9797

98+
type extraFile struct{ srcRelativePath, dstRelativePath, name string }
99+
98100
type result struct {
99101
pages map[string]*page
100102
toc tableOfContents
101103
module *packages.Module
102-
extraFiles []string
104+
extraFiles []extraFile
103105
}
104106

105107
// parse parses the directory into a map of import path -> page and a TOC.
106108
//
107109
// glob is the path to parse, usually ending in `...`. glob is passed directly
108110
// to packages.Load as-is.
109111
//
110-
// extraFiles is a list of paths relative to the module root to include.
112+
// workingDir is the directory to use to run go commands.
113+
//
114+
// optionalExtraFiles is a list of paths relative to the module root to include.
111115
func parse(glob string, workingDir string, optionalExtraFiles []string) (*result, error) {
112116
pages := map[string]*page{}
113117

@@ -119,10 +123,22 @@ func parse(glob string, workingDir string, optionalExtraFiles []string) (*result
119123

120124
// Filter out extra files that don't exist because some modules don't have a
121125
// README.
122-
extraFiles := []string{}
126+
extraFiles := []extraFile{}
123127
for _, f := range optionalExtraFiles {
124128
if _, err := os.Stat(filepath.Join(module.Dir, f)); err == nil {
125-
extraFiles = append(extraFiles, f)
129+
dst := f
130+
dir := filepath.Dir(f)
131+
base := filepath.Base(f)
132+
name := strings.TrimSuffix(base, filepath.Ext(base))
133+
name = strings.Title(name)
134+
if name == "README" {
135+
dst = filepath.Join(dir, "pkg-readme.md")
136+
}
137+
extraFiles = append(extraFiles, extraFile{
138+
srcRelativePath: f,
139+
dstRelativePath: dst,
140+
name: name,
141+
})
126142
}
127143
}
128144

@@ -317,20 +333,17 @@ func processExamples(exs []*doc.Example, fset *token.FileSet) []example {
317333
return result
318334
}
319335

320-
func buildTOC(mod string, pis []pkgInfo, extraFiles []string) tableOfContents {
336+
func buildTOC(mod string, pis []pkgInfo, extraFiles []extraFile) tableOfContents {
321337
toc := tableOfContents{}
322338

323339
modTOC := &tocItem{
324340
UID: mod, // Assume the module root has a package.
325341
Name: mod,
326342
}
327-
for _, path := range extraFiles {
328-
base := filepath.Base(path)
329-
name := strings.TrimSuffix(base, filepath.Ext(base))
330-
name = strings.Title(name)
343+
for _, ef := range extraFiles {
331344
modTOC.addItem(&tocItem{
332-
Href: path,
333-
Name: name,
345+
Href: ef.dstRelativePath,
346+
Name: ef.name,
334347
})
335348
}
336349

File renamed without changes.

internal/godocfx/testdata/golden/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
name: cloud.google.com/go/storage
44
items:
55
- name: README
6-
href: README.md
6+
href: pkg-readme.md

0 commit comments

Comments
 (0)