Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unescaping for newline #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions gocal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,28 @@ func Test_DurationEvent(t *testing.T) {
assert.Equal(t, gc.Events[0].End.Second(), 30)
}
}

const newlineICS = `BEGIN:VCALENDAR
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20170419T172300Z
DTSTART;VALUE=DATE:20190419
DTEND;VALUE=DATE:20190419
DESCRIPTION:line1\nline2\Nline3
END:VEVENT
END:VCALENDAR`

func Test_Newline(t *testing.T) {
gc := NewParser(strings.NewReader(newlineICS))
tz, _ := time.LoadLocation("Asia/Omsk")
start := time.Date(2019, 4, 19, 0, 0, 0, 0, tz)
gc.Start = &start
end := time.Date(2019, 4, 19, 23, 59, 59, 0, tz)
gc.End = &end
err := gc.Parse()

assert.Nil(t, err)
t.Log(gc.Events)
assert.Equal(t, 1, len(gc.Events))
assert.Equal(t, "line1\nline2\nline3", gc.Events[0].Description)
}
4 changes: 4 additions & 0 deletions parser/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ func ParseParameters(p string) (string, map[string]string) {
return tokens[0], parameters
}

// Unescapes strings according to section 3.3.11 of RFC 5545
// https://tools.ietf.org/html/rfc5545#section-3.3.11
func UnescapeString(l string) string {
l = strings.Replace(l, `\\`, `\`, -1)
l = strings.Replace(l, `\;`, `;`, -1)
l = strings.Replace(l, `\,`, `,`, -1)
l = strings.Replace(l, `\n`, "\n", -1)
l = strings.Replace(l, `\N`, "\n", -1)

return l
}
7 changes: 7 additions & 0 deletions parser/line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ func Test_UnescapeString(t *testing.T) {

assert.Equal(t, `Hello, world; lorem \ipsum.`, l)
}

func Test_UnescapeStringWithNewline(t *testing.T) {
l := `line1\nline2\Nline3`
l = UnescapeString(l)

assert.Equal(t, "line1\nline2\nline3", l)
}