Skip to content

Commit

Permalink
when making a message preview, also recognize []-enclosed "horizontal…
Browse files Browse the repository at this point in the history
… ellipsis" unicode character as a snip
  • Loading branch information
mjl- committed Sep 11, 2023
1 parent fc7b0cc commit 4a4ccb8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
17 changes: 11 additions & 6 deletions webmail/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func formatFirstLine(r io.Reader) (string, error) {
ensureLines()

isSnipped := func(s string) bool {
return s == "[...]" || s == "..."
return s == "[...]" || s == "[…]" || s == "..."
}

nextLineQuoted := func(i int) bool {
Expand All @@ -55,7 +55,8 @@ func formatFirstLine(r io.Reader) (string, error) {
return i+1 < len(lines) && (strings.HasPrefix(lines[i+1], ">") || isSnipped(lines[i+1]))
}

// remainder is signature if we see a line with only and minimum 2 dashes, and there are no more empty lines, and there aren't more than 5 lines left
// Remainder is signature if we see a line with only and minimum 2 dashes, and
// there are no more empty lines, and there aren't more than 5 lines left.
isSignature := func() bool {
if len(lines) == 0 || !strings.HasPrefix(lines[0], "--") || strings.Trim(strings.TrimSpace(lines[0]), "-") != "" {
return false
Expand All @@ -77,6 +78,10 @@ func formatFirstLine(r io.Reader) (string, error) {

result := ""

resultSnipped := func() bool {
return strings.HasSuffix(result, "[...]\n") || strings.HasSuffix(result, "[…]")
}

// Quick check for initial wrapped "On ... wrote:" line.
if len(lines) > 3 && strings.HasPrefix(lines[0], "On ") && !strings.HasSuffix(lines[0], "wrote:") && strings.HasSuffix(lines[1], ":") && nextLineQuoted(1) {
result = "[...]\n"
Expand All @@ -87,7 +92,7 @@ func formatFirstLine(r io.Reader) (string, error) {
for ; len(lines) > 0 && !isSignature(); ensureLines() {
line := lines[0]
if strings.HasPrefix(line, ">") {
if !strings.HasSuffix(result, "[...]\n") {
if !resultSnipped() {
result += "[...]\n"
}
lines = lines[1:]
Expand All @@ -101,14 +106,14 @@ func formatFirstLine(r io.Reader) (string, error) {
// line, with an optional empty line in between. If we don't have any text yet, we
// don't require the digits.
if strings.HasSuffix(line, ":") && (strings.ContainsAny(line, "0123456789") || result == "") && nextLineQuoted(0) {
if !strings.HasSuffix(result, "[...]\n") {
if !resultSnipped() {
result += "[...]\n"
}
lines = lines[1:]
continue
}
// Skip snipping by author.
if !(isSnipped(line) && strings.HasSuffix(result, "[...]\n")) {
// Skip possibly duplicate snipping by author.
if !isSnipped(line) || !resultSnipped() {
result += line + "\n"
}
lines = lines[1:]
Expand Down
28 changes: 28 additions & 0 deletions webmail/message_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
package webmail

import (
"strings"
"testing"

"github.com/mjl-/mox/dns"
)

func TestFormatFirstLine(t *testing.T) {
check := func(body, expLine string) {
t.Helper()

line, err := formatFirstLine(strings.NewReader(body))
tcompare(t, err, nil)
if line != expLine {
t.Fatalf("got %q, expected %q, for body %q", line, expLine, body)
}
}

check("", "")
check("single line", "single line\n")
check("single line\n", "single line\n")
check("> quoted\n", "[...]\n")
check("> quoted\nresponse\n", "[...]\nresponse\n")
check("> quoted\n[...]\nresponse after author snip\n", "[...]\nresponse after author snip\n")
check("[...]\nresponse after author snip\n", "[...]\nresponse after author snip\n")
check("[…]\nresponse after author snip\n", "[…]\nresponse after author snip\n")
check(">> quoted0\n> quoted1\n>quoted2\n[...]\nresponse after author snip\n", "[...]\nresponse after author snip\n")
check(">quoted\n\n>quoted\ncoalesce line-separated quotes\n", "[...]\ncoalesce line-separated quotes\n")
check("On <date> <user> wrote:\n> hi\nresponse", "[...]\nresponse\n")
check("On <longdate>\n<user> wrote:\n> hi\nresponse", "[...]\nresponse\n")
check("> quote\nresponse\n--\nsignature\n", "[...]\nresponse\n")
check("> quote\nline1\nline2\nline3\n", "[...]\nline1\nline2\nline3\n")
}

func TestParseListPostAddress(t *testing.T) {
check := func(s string, exp *MessageAddress) {
t.Helper()
Expand Down

0 comments on commit 4a4ccb8

Please sign in to comment.