diff --git a/internal/fourchan/parse.go b/internal/fourchan/parse.go index 845e50e..65f1278 100644 --- a/internal/fourchan/parse.go +++ b/internal/fourchan/parse.go @@ -71,7 +71,14 @@ func explodePost(post Post) (posts []Post) { } // Add simple 1/1 reply/comment messages - if len(replies) == len(findings) && (len(replies) == 1 || !containsEmptyString(replies)) { + if len(replies) == len(findings) && + (len(replies) == 1 || allEmptyButLast(replies) || !containsEmptyString(replies)) { + + // squash many/1 reply/comment into 1/1 + if allEmptyButLast(replies) { + replies = []string{replies[len(replies)-1]} + } + for i, reply := range replies { parentId := getParentId(findings[i]) if parentId == 0 { @@ -100,6 +107,18 @@ func explodePost(post Post) (posts []Post) { return } +func allEmptyButLast(replies []string) bool { + if len(replies) == 0 { + return false + } + for _, reply := range replies[:len(replies)-1] { + if reply != "" { + return false + } + } + return replies[len(replies)-1] != "" +} + func fixComments(comments []string) (ret []string) { for _, comment := range comments { ret = append(ret, plaintextComment(cleanComment(comment))) diff --git a/internal/fourchan/parse_test.go b/internal/fourchan/parse_test.go index 820d501..62b2968 100644 --- a/internal/fourchan/parse_test.go +++ b/internal/fourchan/parse_test.go @@ -152,7 +152,7 @@ func TestExplodeNPosts(t *testing.T) { func TestNParents(t *testing.T) { thread := toThread(testThread) got := len(thread.posts) - expected := 8 + expected := 7 if got != expected { t.Errorf("got %d expected %d", got, expected) } @@ -190,3 +190,28 @@ func TestCleanComment(t *testing.T) { } } } + +func TestAllEmptyButLast(t *testing.T) { + testReplies := []struct { + replies []string + expected bool + }{ + {[]string{"", "", "foo"}, true}, + {[]string{"foo", "bar"}, false}, + {[]string{"", "foo"}, true}, + {[]string{}, false}, + } + for _, reply := range testReplies { + got := allEmptyButLast(reply.replies) + expected := reply.expected + if expected != got { + t.Errorf( + "got %t expected %t - (%d)%+v", + got, + expected, + len(reply.replies), + reply.replies, + ) + } + } +}