-
Notifications
You must be signed in to change notification settings - Fork 1
/
rss.go
50 lines (43 loc) · 954 Bytes
/
rss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package articler
import (
"bytes"
"encoding/xml"
"fmt"
"html"
"regexp"
)
type Rss struct {
Channel Channel `xml:"channel"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
}
type Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Items []Item `xml:"item"`
}
func DecodeRss(in []byte) (Rss, error) {
//in = escapeDescription(in)
rss := Rss{}
err := xml.Unmarshal(in, &rss)
return rss, err
}
func escapeDescription(in []byte) []byte {
return escapeTag("description", in)
}
func escapeTag(tag string, in []byte) []byte {
reg := fmt.Sprintf("<%s>(.*?)</%s>", tag, tag)
re := regexp.MustCompile(reg)
res := re.FindAllSubmatch(in, -1)
for _, v := range res {
if len(v) == 2 {
in = bytes.Replace(in, v[1],
[]byte(html.EscapeString(string(v[1]))), -1)
}
}
return in
}