-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
231 additions
and
130 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ feeds.yml | |
gin-bin | ||
goread | ||
goread-*-* | ||
public |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Package feed extends the package gofeed by adding a Date field to gofeed.Item | ||
// which contains either the published or the updated date, since some feeds | ||
// only offer a update time, as well as a sorting implementation based on the | ||
// field. | ||
package feed | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/mmcdole/gofeed" | ||
) | ||
|
||
type Feed struct { | ||
gofeed.Feed | ||
Items []*Item | ||
} | ||
|
||
type Item struct { | ||
*gofeed.Item | ||
Feed *gofeed.Feed | ||
Category string | ||
} | ||
|
||
func (i *Item) Time() time.Time { | ||
if i.PublishedParsed != nil { | ||
return *i.PublishedParsed | ||
} | ||
if i.UpdatedParsed != nil { | ||
return *i.UpdatedParsed | ||
} | ||
return time.Time{} | ||
} | ||
|
||
type SortByDate []*Item | ||
|
||
func (is SortByDate) Len() int { return len(is) } | ||
func (is SortByDate) Less(i, j int) bool { return is[i].Time().Before(is[j].Time()) } | ||
func (is SortByDate) Swap(i, j int) { is[i], is[j] = is[j], is[i] } | ||
|
||
type Parser struct{ gofeed.Parser } | ||
|
||
func NewParser(c *http.Client) *Parser { | ||
p := gofeed.NewParser() | ||
p.Client = c | ||
return &Parser{*p} | ||
} | ||
|
||
func (p *Parser) ParseURL(url string) (*Feed, error) { | ||
f, err := p.Parser.ParseURL(url) | ||
items := make([]*Item, len(f.Items)) | ||
for i, item := range f.Items { | ||
items[i] = &Item{Item: item, Feed: f} | ||
} | ||
return &Feed{*f, items}, err | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Package funcs contains a collection of generic templating functions. | ||
package funcs | ||
|
||
import ( | ||
"crypto/sha1" | ||
"fmt" | ||
"html/template" | ||
"strings" | ||
"time" | ||
|
||
"github.com/microcosm-cc/bluemonday" | ||
) | ||
|
||
func FuncMap(truncateLen int) template.FuncMap { | ||
return template.FuncMap{ | ||
"hash": Hash(), | ||
"sanitize": Sanitize(), | ||
"time": Time(), | ||
"title": Title(), | ||
"trim": Trim(), | ||
"truncate": Truncate(truncateLen), | ||
} | ||
} | ||
|
||
func Hash() func(string) string { | ||
return func(s string) string { return fmt.Sprintf("%x", sha1.Sum([]byte(s))) } | ||
} | ||
|
||
func Sanitize() func(string) string { return bluemonday.StrictPolicy().Sanitize } | ||
|
||
func Time() func() time.Time { return func() time.Time { return time.Now() } } | ||
|
||
func Title() func(string) string { return strings.Title } | ||
|
||
func Trim() func(string) string { return strings.TrimSpace } | ||
|
||
func Truncate(n int) func(string) string { | ||
ellipsis := " …" | ||
return func(s string) string { | ||
if len(s)-len(ellipsis) <= n { | ||
return s | ||
} | ||
return s[:n] + ellipsis | ||
} | ||
} |
This file contains 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
Oops, something went wrong.