Remove blank lines

master
bel 2019-12-29 11:18:26 -07:00
parent 37408af647
commit 98adb53caf
2 changed files with 39 additions and 0 deletions

View File

@ -63,6 +63,8 @@ func newItem(i *gofeed.Item, contentFilter, copyright string) (*Item, error) {
item.Content = fmt.Sprintf(`<a href="%s">%s</a><br>%s`, item.Link, item.Title, item.Content)
item.Content = clearBlankLines(item.Content)
return item, nil
}
@ -109,3 +111,10 @@ func (is Items) Swap(i, j int) {
is[i] = is[j]
is[j] = k
}
func clearBlankLines(s string) string {
r := regexp.MustCompile(`(?m)^\s*<br>\s*$`)
s = r.ReplaceAllLiteralString(s, "")
r = regexp.MustCompile(`(?m)\s\s*`)
return r.ReplaceAllLiteralString(s, "\n")
}

View File

@ -3,6 +3,7 @@ package rss
import (
"fmt"
"net/http"
"strings"
"testing"
"github.com/mmcdole/gofeed"
@ -75,3 +76,32 @@ func TestRSSItemNewEncodeDecode(t *testing.T) {
t.Fatal(err)
}
}
func TestClearBlankLines(t *testing.T) {
cases := map[string]struct {
in string
outLines int
}{
"remove with and without whitespace": {
in: `<html>
<head>
<script>something</script>
<style>body{hello:"blue";}</style>
</head>
<body>
<br>
<br>
<br>
</body>
</html>`,
outLines: 8,
},
}
for name, c := range cases {
out := clearBlankLines(c.in)
if v := len(strings.Split(out, "\n")); v != c.outLines {
t.Errorf("%v: want %v lines, got %v from %q: %q", name, c.outLines, v, c.in, out)
}
}
}