newline battles continue
parent
8c7f52eb70
commit
be4a879e50
12
rss/item.go
12
rss/item.go
|
|
@ -95,7 +95,11 @@ func (i *Item) Load(key, ns1 string, ns ...string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return config.Decode(b, i)
|
||||
err = config.Decode(b, i)
|
||||
if err == nil {
|
||||
i.Content = clearBlankLines(i.Content)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (is Items) Len() int {
|
||||
|
|
@ -113,8 +117,10 @@ func (is Items) Swap(i, j int) {
|
|||
}
|
||||
|
||||
func clearBlankLines(s string) string {
|
||||
r := regexp.MustCompile(`(?m)^\s*<br>\s*$`)
|
||||
s = r.ReplaceAllLiteralString(s, "")
|
||||
r := regexp.MustCompile(`<br/>`)
|
||||
s = r.ReplaceAllLiteralString(s, "<br>")
|
||||
r = regexp.MustCompile(`(?m)<br>\s*(<br>\s*)*`)
|
||||
s = r.ReplaceAllLiteralString(s, "<br>")
|
||||
r = regexp.MustCompile(`(?m)\s\s*`)
|
||||
return r.ReplaceAllLiteralString(s, "\n")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,15 +92,61 @@ func TestClearBlankLines(t *testing.T) {
|
|||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br/><br/>
|
||||
<br/> <br/>
|
||||
</body>
|
||||
</html>`,
|
||||
outLines: 8,
|
||||
outLines: 1,
|
||||
},
|
||||
"sample content": {
|
||||
in: `<html>
|
||||
Linux
|
||||
Blog
|
||||
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
|
||||
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
|
||||
<img src="https://www.jeremymorgan.com/images/programming-blog.png" alt="Programming
|
||||
Blog"/><h1><a href="https://www.jeremymorgan.com/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Jeremy
|
||||
Morgan</a></h1><h2>Mostly
|
||||
Coherent
|
||||
Ramblings
|
||||
of
|
||||
a
|
||||
Silicon
|
||||
Forest
|
||||
Software
|
||||
Developer</h2><br/><br/><br/>
|
||||
<ul><br/>
|
||||
<li>RSS</li><br/>
|
||||
<br/></ul><br/>
|
||||
<br/><br/>
|
||||
<br/>
|
||||
<br/>
|
||||
<br/>
|
||||
<br/><br/>
|
||||
<br/><ul><li><a href="https://www.jeremymorgan.com/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Home</a></li><li><a href="https://www.jeremymorgan.com/tutorials/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Tutorials</a></li><li><a href="https://www.jeremymorgan.com/blog/programming/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Programming</a></li><li><a href="https://www.jeremymorgan.com/blog/raspberry-pi/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Raspberry
|
||||
Pi</a></li><li><a href="https://www.jeremymorgan.com/blog/linux/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Linux</a></li><li><a href="https://www.jeremymorgan.com/blog/dotnet/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">.Net</a></li><li><a href="https://github.com/JeremyMorgan" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">My
|
||||
Projects</a></li><li><a href="https://www.jeremymorgan.com/contact/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Contact
|
||||
Me</a></li></ul><br/><br/>
|
||||
<br/>
|
||||
<br/>
|
||||
<br/><br/>
|
||||
<br/>
|
||||
<br/>
|
||||
<br/>
|
||||
<h1>Trying
|
||||
Out
|
||||
</html>`,
|
||||
outLines: 6,
|
||||
},
|
||||
}
|
||||
|
||||
for name, c := range cases {
|
||||
out := clearBlankLines(c.in)
|
||||
if v := len(strings.Split(out, "\n")); v != c.outLines {
|
||||
cnts := []int{
|
||||
strings.Count(out, "<br>"),
|
||||
strings.Count(out, "<br/>"),
|
||||
}
|
||||
if v := cnts[0] + cnts[1]; v != c.outLines {
|
||||
t.Errorf("%v: want %v lines, got %v from %q: %q", name, c.outLines, v, c.in, out)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue