From 97ede2a428faf3a62ce0140e51ed591e261c09a1 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Mon, 25 Jan 2016 16:47:49 -0500 Subject: Updated serverside hashtag regex to ignore more punctuation around words --- model/search_params.go | 18 +++++++++++------- model/search_params_test.go | 10 +++++++--- model/utils.go | 10 ++++++++-- model/utils_test.go | 41 ++++++++++++++++++++++++++++------------- 4 files changed, 54 insertions(+), 25 deletions(-) (limited to 'model') diff --git a/model/search_params.go b/model/search_params.go index 17a64d980..9a7406a07 100644 --- a/model/search_params.go +++ b/model/search_params.go @@ -20,12 +20,7 @@ func splitWordsNoQuotes(text string) []string { words := []string{} for _, word := range strings.Fields(text) { - word = puncStart.ReplaceAllString(word, "") - word = puncEnd.ReplaceAllString(word, "") - - if len(word) != 0 { - words = append(words, word) - } + words = append(words, word) } return words @@ -94,7 +89,16 @@ func parseSearchFlags(input []string) ([]string, [][2]string) { } if !isFlag { - words = append(words, word) + // trim off surrounding punctuation + word = puncStart.ReplaceAllString(word, "") + word = puncEnd.ReplaceAllString(word, "") + + // and remove extra pound #s + word = hashtagStart.ReplaceAllString(word, "#") + + if len(word) != 0 { + words = append(words, word) + } } } diff --git a/model/search_params_test.go b/model/search_params_test.go index af4cbe595..59eb0a113 100644 --- a/model/search_params_test.go +++ b/model/search_params_test.go @@ -118,19 +118,19 @@ func TestParseSearchFlags(t *testing.T) { t.Fatalf("got incorrect flags %v", flags) } - if words, flags := parseSearchFlags(splitWords("fruit: cherry")); len(words) != 2 || words[0] != "fruit:" || words[1] != "cherry" { + if words, flags := parseSearchFlags(splitWords("fruit: cherry")); len(words) != 2 || words[0] != "fruit" || words[1] != "cherry" { t.Fatalf("got incorrect words %v", words) } else if len(flags) != 0 { t.Fatalf("got incorrect flags %v", flags) } - if words, flags := parseSearchFlags(splitWords("channel:")); len(words) != 1 || words[0] != "channel:" { + if words, flags := parseSearchFlags(splitWords("channel:")); len(words) != 1 || words[0] != "channel" { t.Fatalf("got incorrect words %v", words) } else if len(flags) != 0 { t.Fatalf("got incorrect flags %v", flags) } - if words, flags := parseSearchFlags(splitWords("channel: first in: second from:")); len(words) != 1 || words[0] != "from:" { + if words, flags := parseSearchFlags(splitWords("channel: first in: second from:")); len(words) != 1 || words[0] != "from" { t.Fatalf("got incorrect words %v", words) } else if len(flags) != 2 || flags[0][0] != "channel" || flags[0][1] != "first" || flags[1][0] != "in" || flags[1][1] != "second" { t.Fatalf("got incorrect flags %v", flags) @@ -212,4 +212,8 @@ func TestParseSearchParams(t *testing.T) { if sp := ParseSearchParams("testing in:channel from:someone"); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 1 || sp[0].FromUsers[0] != "someone" { t.Fatalf("Incorrect output from parse search params: %v", sp[0]) } + + if sp := ParseSearchParams("##hashtag +#plus+"); len(sp) != 1 || sp[0].Terms != "#hashtag #plus" || sp[0].IsHashtag != true || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp[0]) + } } diff --git a/model/utils.go b/model/utils.go index 70b7e3bbd..719a2ad1b 100644 --- a/model/utils.go +++ b/model/utils.go @@ -298,8 +298,9 @@ func Etag(parts ...interface{}) string { } var validHashtag = regexp.MustCompile(`^(#[A-Za-zäöüÄÖÜß]+[A-Za-z0-9äöüÄÖÜß_\-]*[A-Za-z0-9äöüÄÖÜß])$`) -var puncStart = regexp.MustCompile(`^[.,()&$!\?\[\]{}':;\\]+`) -var puncEnd = regexp.MustCompile(`[.,()&$#!\?\[\]{}';\\]+$`) +var puncStart = regexp.MustCompile(`^[.,()&$!\?\[\]{}':;\\<>\-+=%^*|]+`) +var hashtagStart = regexp.MustCompile(`^#{2,}`) +var puncEnd = regexp.MustCompile(`[.,()&$#!\?\[\]{}':;\\<>\-+=%^*|]+$`) func ParseHashtags(text string) (string, string) { words := strings.Fields(text) @@ -307,8 +308,13 @@ func ParseHashtags(text string) (string, string) { hashtagString := "" plainString := "" for _, word := range words { + // trim off surrounding punctuation word = puncStart.ReplaceAllString(word, "") word = puncEnd.ReplaceAllString(word, "") + + // and remove extra pound #s + word = hashtagStart.ReplaceAllString(word, "#") + if validHashtag.MatchString(word) { hashtagString += " " + word } else { diff --git a/model/utils_test.go b/model/utils_test.go index 24ee4b7a6..b8eabe264 100644 --- a/model/utils_test.go +++ b/model/utils_test.go @@ -83,19 +83,34 @@ func TestEtag(t *testing.T) { } var hashtags map[string]string = map[string]string{ - "#test": "#test", - "test": "", - "#test123": "#test123", - "#123test123": "", - "#test-test": "#test-test", - "#test?": "#test", - "hi #there": "#there", - "#bug #idea": "#bug #idea", - "#bug or #gif!": "#bug #gif", - "#hüllo": "#hüllo", - "#?test": "", - "#-test": "", - "#yo_yo": "#yo_yo", + "#test": "#test", + "test": "", + "#test123": "#test123", + "#123test123": "", + "#test-test": "#test-test", + "#test?": "#test", + "hi #there": "#there", + "#bug #idea": "#bug #idea", + "#bug or #gif!": "#bug #gif", + "#hüllo": "#hüllo", + "#?test": "", + "#-test": "", + "#yo_yo": "#yo_yo", + "(#brakets)": "#brakets", + ")#stekarb(": "#stekarb", + "<#less_than<": "#less_than", + ">#greater_than>": "#greater_than", + "-#minus-": "#minus", + "+#plus+": "#plus", + "=#equals=": "#equals", + "%#pct%": "#pct", + "&#and&": "#and", + "^#hat^": "#hat", + "##brown#": "#brown", + "*#star*": "#star", + "|#pipe|": "#pipe", + ":#colon:": "#colon", + ";#semi;": "#semi", } func TestParseHashtags(t *testing.T) { -- cgit v1.2.3-1-g7c22