Skip to content

Commit

Permalink
fix comment stealing of subsequent keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ionous committed Dec 23, 2023
1 parent d4d126e commit c4ef958
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
39 changes: 29 additions & 10 deletions note/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type bookState struct {

func (b *content) BeginCollection(buf *strings.Builder) {
b.buf = buf
// if there is a (footer or prefix) comment pending
// if there is a comment pending
// steal it from the shared buffer, and use it as
// the header of the first element.
if buf.Len() > 0 {
Expand Down Expand Up @@ -70,9 +70,23 @@ func (b *content) Comment(n Type, str string) (err error) {
}
b.lastNote = n
}
if n != Footer {
switch n {
default:
appendLine(b.buf, str)
} else {

case Header:
// write headers for following terms straight to the output
// so that they appear with the *current* collection
// and dont get stolen by the next begin collection.
if b.totalKeys == 0 {
appendLine(b.buf, str)
} else if b.writeKeys() {
b.out.WriteString(str)
} else {
appendLine(&b.out, str)
}

case Footer:
if was != Footer {
b.out.WriteRune(runes.NextTerm)
} else {
Expand All @@ -88,13 +102,7 @@ func (b *content) flushLast() {
if str := b.buf.String(); len(str) > 0 {
b.buf.Reset()
// form feeds
if b.nextKeys > 0 {
for i := 0; i < b.nextKeys; i++ {
b.out.WriteRune(runes.NextTerm)
}
b.nextKeys = 0
b.markerCount = 0
}
b.writeKeys()
// markers
if mark := b.lastNote.mark(); mark > 0 {
if b.markerCount < mark {
Expand All @@ -113,6 +121,17 @@ func (b *content) flushLast() {
}
}

func (b *content) writeKeys() (okay bool) {
if okay = b.nextKeys > 0; okay {
for i := 0; i < b.nextKeys; i++ {
b.out.WriteRune(runes.NextTerm)
}
b.nextKeys = 0
b.markerCount = 0
}
return
}

func appendLine(out *strings.Builder, str string) {
if out.Len() > 0 {
out.WriteRune(runes.Newline)
Expand Down
2 changes: 1 addition & 1 deletion tellFile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
var focus string

func TestFiles(t *testing.T) {
// focus = "trailingComments2"
// focus = "smallCatalogComments"
if files, e := testdata.Tell.ReadDir("."); e != nil {
t.Fatal(e)
} else {
Expand Down
6 changes: 3 additions & 3 deletions testdata/smallCatalogComments.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"content": {
"": "# Catalog header\f# Something here at the end.",
"catalog:": [
"# First item header",
"# First item header\f# Second item\n# header",
{
"": "\r\r# Inline suffix\n# Aligned comment",
"name:": "First item",
Expand All @@ -16,10 +16,10 @@
]
},
{
"": "# Second item\n# header",
"": "\f# Options header",
"name:": "Second item",
"options:": [
"# Options header\r\n# A non-inline prefix",
"\r\n# A non-inline prefix",
"Big",
"Big description for a big entry."
],
Expand Down

0 comments on commit c4ef958

Please sign in to comment.