Skip to content

Commit

Permalink
relnote: remove empty sections when merging
Browse files Browse the repository at this point in the history
Remove sections that have no content from the merged document.

For golang/go#64169.

Change-Id: Ic78b1e2dc46d14dcf885cef8ce0e7dd4a257298a
Reviewed-on: https://go-review.googlesource.com/c/build/+/556160
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
jba committed Jan 18, 2024
1 parent a383c72 commit fdfdde0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
57 changes: 55 additions & 2 deletions relnote/relnote.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,54 @@ func Merge(fsys fs.FS) (*md.Document, error) {
addLines(b, delta)
}
}
doc.Blocks = append(doc.Blocks, fd.Blocks...)
// Append non-empty blocks to the result document.
for _, b := range fd.Blocks {
if _, ok := b.(*md.Empty); !ok {
doc.Blocks = append(doc.Blocks, b)
}
}
// TODO(jba): merge links
// TODO(jba): add headings for package sections under "Minor changes to the library".
}
// TODO(jba): remove headings with empty contents
// Remove headings with empty contents.
doc.Blocks = removeEmptySections(doc.Blocks)
return doc, nil
}

// removeEmptySections removes headings with no content. A heading has no content
// if there are no blocks between it and the next heading at the same level, or the
// end of the document.
func removeEmptySections(bs []md.Block) []md.Block {
res := bs[:0]
delta := 0 // number of lines by which to adjust positions

// Remove preceding headings at same or higher level; they are empty.
rem := func(level int) {
for len(res) > 0 {
last := res[len(res)-1]
if lh, ok := last.(*md.Heading); ok && lh.Level >= level {
res = res[:len(res)-1]
// Adjust subsequent block positions by the size of this block
// plus 1 for the blank line between headings.
delta += lh.EndLine - lh.StartLine + 2
} else {
break
}
}
}

for _, b := range bs {
if h, ok := b.(*md.Heading); ok {
rem(h.Level)
}
addLines(b, -delta)
res = append(res, b)
}
// Remove empty headings at the end of the document.
rem(1)
return res
}

func sortedMarkdownFilenames(fsys fs.FS) ([]string, error) {
var filenames []string
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -203,6 +243,8 @@ func lastBlock(doc *md.Document) md.Block {
return doc.Blocks[len(doc.Blocks)-1]
}

// addLines adds n lines to the position of b.
// n can be negative.
func addLines(b md.Block, n int) {
pos := position(b)
pos.StartLine += n
Expand Down Expand Up @@ -248,3 +290,14 @@ func parseFile(fsys fs.FS, path string) (*md.Document, error) {
doc := NewParser().Parse(in)
return doc, nil
}

// headingIndex returns the index in bs of the first heading at the given level,
// or len(bs) if there isn't one.
func headingIndex(bs []md.Block, level int) int {
for i, b := range bs {
if h, ok := b.(*md.Heading); ok && h.Level == level {
return i
}
}
return len(bs)
}
39 changes: 39 additions & 0 deletions relnote/relnote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,44 @@ func TestSortedMarkdownFilenames(t *testing.T) {
if !slices.Equal(got, want) {
t.Errorf("\ngot %v\nwant %v", got, want)
}
}

func TestRemoveEmptySections(t *testing.T) {
doc := NewParser().Parse(`
# h1
not empty
# h2
## h3
### h4
#### h5
### h6
### h7
## h8
something
## h9
# h10
`)
bs := removeEmptySections(doc.Blocks)
got := md.ToMarkdown(&md.Document{Blocks: bs})
want := md.ToMarkdown(NewParser().Parse(`
# h1
not empty
# h2
## h8
something
`))
if got != want {
t.Errorf("\ngot:\n%s\nwant:\n%s", got, want)
}
}
18 changes: 18 additions & 0 deletions relnote/testdata/empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- 0-heading.md --
# A heading
-- a.md --
## A
-- b.md --
## B
Something.
-- c.md --
## C

### C1
-- d.md --
## D
-- want --
# A heading

## B
Something.

0 comments on commit fdfdde0

Please sign in to comment.