Skip to content

Instantly share code, notes, and snippets.

func (c Content) HTML() template.HTML {
// Remove CDATA tags
cdata := regexp.MustCompile(`(?s)<!\[CDATA\[(.*?)\]\]>`)
body := cdata.ReplaceAllString(c.Body, "$1")
// Make path to images absolute
pat := regexp.MustCompile(`<img src="\/`)
body = pat.ReplaceAllString(body, "<img src=\"" + c.BaseURI + "/")
return template.HTML(html.UnescapeString(body))
}
<!DOCTYPE html>
<html>
<head>
<style>
{{ template "style.go.css" }}
</style>
</head>
<body>
<div class="feeds">
{{ range . }}
<div class="feed">
<h1>{{.Title}}</h1>
{{ range .FilteredEntries }}
<div class="entry">
<h3>
{{if not .Links}}
{{.Title}}
{{else}}
<a href="{{(index .Links 0).HREF}}">{{.Title}}</a>
{{end}}
func filterFeeds(feeds []*Feed) ([]*Feed) {
cutoff := time.Now().Add(-24*time.Hour)
filtered := make([]*Feed, 0, len(feeds))
for _, f := range feeds {
if time.Time(f.Updated).After(cutoff) {
filtered = append(filtered, f)
}
}
return filtered
}
func parseFeed(body []byte) (*Feed, error) {
feed := Feed{}
err := xml.Unmarshal(body, &feed)
if err != nil {
return nil, err
}
return &feed, nil
}
func (a *atomTime) Time() time.Time {
return time.Time(*a)
}
func (a *atomTime) LocalString() string {
loc, err := time.LoadLocation("Local")
if err != nil {
return a.Time().String()
}
return a.Time().In(loc).Format("Jan 02, 2006 3:04PM")
type atomTime time.Time
func (a *atomTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
parsed, err := time.Parse(time.RFC3339, v)
if err != nil {
return err
}
*a = atomTime(parsed)
type Content struct {
Type string `xml:"type,attr"`
BaseURI string `xml:"base,attr"`
Body string `xml:",innerxml"`
}
type Link struct {
HREF string `xml:"href,attr"`
Rel string `xml:"rel,attr"`
Type string `xml:"type,attr"`
type Feed struct {
ID string `xml:"id"`
Title string `xml:"title"`
Links []Link `xml:"link"`
Entries []Entry `xml:"entry"`
Updated atomTime `xml:"updated"`
}
type Entry struct {
ID string `xml:"id"`
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<subtitle>A subtitle.</subtitle>
<link href="http://example.org/feed/" rel="self" />
<link href="http://example.org/" />
<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
<updated>2003-12-13T18:30:02Z</updated>