This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func parseFeed(body []byte) (*Feed, error) { | |
feed := Feed{} | |
err := xml.Unmarshal(body, &feed) | |
if err != nil { | |
return nil, err | |
} | |
return &feed, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
{{ template "style.go.css" }} | |
</style> | |
</head> | |
<body> | |
<div class="feeds"> | |
{{ range . }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func loadTemplate() *template.Template { | |
templates, err := filepath.Glob("templates/*") | |
if err != nil { | |
log.Println(err) | |
} | |
t := template.Must(template.New("layout.go.html").ParseFiles(templates...)) | |
return t | |
} | |
func main() { |