Add improved formatting for post types
This commit is contained in:
parent
9e8777b66c
commit
3f06fd8aad
164
main.go
164
main.go
|
@ -11,17 +11,21 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jinzhu/now"
|
"github.com/jinzhu/now"
|
||||||
|
"p83.nl/go/ekster/pkg/jf2"
|
||||||
"p83.nl/go/ekster/pkg/microsub"
|
"p83.nl/go/ekster/pkg/microsub"
|
||||||
"willnorris.com/go/microformats"
|
"willnorris.com/go/microformats"
|
||||||
|
|
||||||
"p83.nl/go/ekster/pkg/jf2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Summary struct {
|
type Summary struct {
|
||||||
Items []microsub.Item
|
PostTypes map[string]string
|
||||||
|
Name string
|
||||||
|
Published string
|
||||||
|
Items []microsub.Item
|
||||||
|
Grouped map[string][]microsub.Item
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -40,7 +44,7 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := openAndCreate(filename)
|
f, err := forceCreate(filename)
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(f)
|
data, err := ioutil.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -55,9 +59,10 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
items = filterEntriesAfter(items, now.With(time.Now()).Monday())
|
monday := now.With(time.Now()).Monday().AddDate(0, 0, -10)
|
||||||
|
items = filterEntriesAfter(items, monday)
|
||||||
|
|
||||||
summary := Summary{Items: items}
|
summary := getSummary(monday, items)
|
||||||
|
|
||||||
err = outputEntries(summary, &buf)
|
err = outputEntries(summary, &buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -71,36 +76,74 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func openAndCreate(filename string) (io.ReadCloser, error) {
|
func getSummary(monday time.Time, items []microsub.Item) Summary {
|
||||||
f, err := os.Open(filename)
|
year, week := monday.ISOWeek()
|
||||||
if err != nil && !os.IsExist(err) {
|
grouped := make(map[string][]microsub.Item)
|
||||||
t, err := template.ParseFiles("templates/full.html")
|
for _, item := range items {
|
||||||
if err != nil {
|
itemType := postTypeDiscovery(&item)
|
||||||
return nil, err
|
grouped[itemType] = append(grouped[itemType], item)
|
||||||
}
|
}
|
||||||
f, err = os.Create(filename)
|
summary := Summary{
|
||||||
if err != nil {
|
Name: fmt.Sprintf("Digest for Week %d-%d", week, year),
|
||||||
return nil, err
|
Published: monday.AddDate(0, 0, 7).Format("2 Jan 2006"),
|
||||||
}
|
Items: items,
|
||||||
err = t.Execute(f, nil)
|
Grouped: grouped,
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
return summary
|
||||||
}
|
}
|
||||||
err = f.Close()
|
|
||||||
if err != nil {
|
func forceCreate(filename string) (io.ReadCloser, error) {
|
||||||
return nil, err
|
t, err := template.ParseFiles("templates/full.html")
|
||||||
}
|
if err != nil {
|
||||||
f, err = os.Open(filename)
|
return nil, err
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
f, err := os.Create(filename)
|
||||||
}
|
if err != nil {
|
||||||
} else if err != nil {
|
return nil, err
|
||||||
|
}
|
||||||
|
err = t.Execute(f, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
f, err = os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func openAndCreate(filename string) (io.ReadCloser, error) {
|
||||||
|
var f io.ReadCloser
|
||||||
|
var err error
|
||||||
|
|
||||||
|
f, err = os.Open(filename)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if !os.IsExist(err) {
|
||||||
|
f, err = forceCreate(filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return f, err
|
||||||
|
}
|
||||||
|
|
||||||
func outputEntries(summary Summary, w io.Writer) error {
|
func outputEntries(summary Summary, w io.Writer) error {
|
||||||
|
summary.PostTypes = map[string]string{
|
||||||
|
"checkin": "Checkins",
|
||||||
|
"event": "Events",
|
||||||
|
"repost-of": "Reposts",
|
||||||
|
"like-of": "Likes",
|
||||||
|
"in-reply-to": "Replies",
|
||||||
|
"bookmark": "Bookmarks",
|
||||||
|
"photo": "Photos",
|
||||||
|
"note": "Notes",
|
||||||
|
"article": "Articles",
|
||||||
|
}
|
||||||
|
|
||||||
t, err := template.ParseFiles("templates/weekly.html")
|
t, err := template.ParseFiles("templates/weekly.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -112,6 +155,67 @@ func outputEntries(summary Summary, w io.Writer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func postTypeDiscovery(item *microsub.Item) string {
|
||||||
|
if item == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if item.Checkin != nil {
|
||||||
|
return "checkin"
|
||||||
|
}
|
||||||
|
if item.Type == "event" {
|
||||||
|
return "event"
|
||||||
|
}
|
||||||
|
if len(item.RepostOf) > 0 && validUrl(item.RepostOf[0]) {
|
||||||
|
return "repost-of"
|
||||||
|
}
|
||||||
|
if len(item.LikeOf) > 0 && validUrl(item.LikeOf[0]) {
|
||||||
|
return "like-of"
|
||||||
|
}
|
||||||
|
if len(item.InReplyTo) > 0 && validUrl(item.InReplyTo[0]) {
|
||||||
|
return "in-reply-to"
|
||||||
|
}
|
||||||
|
if len(item.BookmarkOf) > 0 && validUrl(item.BookmarkOf[0]) {
|
||||||
|
return "bookmark"
|
||||||
|
}
|
||||||
|
if len(item.Photo) > 0 && validUrl(item.Photo[0]) {
|
||||||
|
return "photo"
|
||||||
|
}
|
||||||
|
|
||||||
|
var content, name string
|
||||||
|
if item.Content != nil {
|
||||||
|
content = item.Content.Text
|
||||||
|
}
|
||||||
|
if content == "" {
|
||||||
|
if item.Summary != "" {
|
||||||
|
content = item.Summary
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if content == "" {
|
||||||
|
return "note"
|
||||||
|
}
|
||||||
|
|
||||||
|
name = item.Name
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
return "note"
|
||||||
|
}
|
||||||
|
|
||||||
|
name = strings.Join(strings.Fields(strings.TrimSpace(name)), " ")
|
||||||
|
content = strings.Join(strings.Fields(strings.TrimSpace(content)), " ")
|
||||||
|
|
||||||
|
if !strings.HasPrefix(content, name) {
|
||||||
|
return "article"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "note"
|
||||||
|
}
|
||||||
|
|
||||||
|
func validUrl(u string) bool {
|
||||||
|
_, err := url.Parse(u)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
func getEntriesForFeed(u *url.URL) ([]microsub.Item, error) {
|
func getEntriesForFeed(u *url.URL) ([]microsub.Item, error) {
|
||||||
resp, err := http.Get(u.String())
|
resp, err := http.Get(u.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,14 +1,24 @@
|
||||||
<span class="next-entry"></span>
|
<span class="next-entry"></span>
|
||||||
<div class="h-entry">
|
<div class="h-entry">
|
||||||
{{ range .Items }}
|
<h2 class="p-name">{{ .Name }}</h2>
|
||||||
<h2 class="p-name">{{ .Name }}</h2>
|
{{ range $k, $items := .Grouped }}
|
||||||
{{ if .Content }}
|
<h3>{{ index $.PostTypes $k }}</h3>
|
||||||
{{ if .Content.HTML }}
|
<ul class="{{ $k }}">
|
||||||
<div class="e-content">{{ .Content.HTML }}</div>
|
{{ range $items }}
|
||||||
{{ else }}
|
{{ if eq $k "like-of" }}
|
||||||
<div class="p-content">{{ .Content.Text }}</div>
|
{{ range .LikeOf }}
|
||||||
|
<li><a href="{{ . }}">{{ . }}</a></li>
|
||||||
|
{{ end }}
|
||||||
|
{{ else }}
|
||||||
|
{{ if .Checkin }}
|
||||||
|
<li><a href="{{ .URL }}">Checkin at {{ .Checkin.Name }} in {{ .Checkin.Locality }}, {{ .Checkin.Region }}</a></li>
|
||||||
|
{{ else if .Content }}
|
||||||
|
<li><a href="{{ .URL }}">{{ .Content.Text }}</a></li>
|
||||||
|
{{ else }}
|
||||||
|
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
|
||||||
|
{{ end }}
|
||||||
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
</ul>
|
||||||
<time datetime="{{ .Published }}">{{ .Published }}</time>
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
Loading…
Reference in New Issue
Block a user