Skip to content

Commit

Permalink
change CleanText to return not more symbols than provided max
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Mar 25, 2022
1 parent 1195146 commit ffff99e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func makeTwitter(opts options) *proc.TwitterClient {
// template failed to parse record, backup predefined format
return fmt.Sprintf("%s - %s", item.Title, item.Link)
}
return strings.Replace(proc.CleanText(b1.String(), 275), `\n`, "\n", -1) // \n in template
return strings.Replace(proc.CleanText(b1.String(), 280), `\n`, "\n", -1) // \n in template
}

twiAuth := proc.TwitterAuth{
Expand Down
5 changes: 3 additions & 2 deletions app/proc/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ func (t *TwitterClient) Send(item feed.Item) error {
return nil
}

// CleanText removes html tags and shrinks result, adding 4 symbols on top
// CleanText removes html tags and shrinks result
func CleanText(inp string, max int) string {
res := striphtmltags.StripTags(inp)
if len([]rune(res)) > max {
snippet := []rune(res)[:max]
// 4 symbols reserved for space and three dots on the end
snippet := []rune(res)[:max-4]
// go back in snippet and found first space
for i := len(snippet) - 1; i >= 0; i-- {
if snippet[i] == ' ' {
Expand Down
6 changes: 3 additions & 3 deletions app/proc/twitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func TestCleanText(t *testing.T) {
max int
}{
{"test", "test", 10},
{"test 12345 aaaa", "test ...", 6},
{"<b>test 12345 aaaa</b>", "test ...", 6},
{"<b>test12345 aaaa</b>", "test12 ...", 6},
{"test 12345 aaaa", "test ...", 10},
{"<b>test 12345 aaaa</b>", "test ...", 10},
{"<b>test12345 aaaa</b>", "test12 ...", 10},
}

for i, tt := range tbl {
Expand Down

0 comments on commit ffff99e

Please sign in to comment.