diff --git a/go.mod b/go.mod index b374ee6..a8d8dba 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,9 @@ go 1.12 require ( github.com/jinzhu/now v1.1.1 + github.com/kylelemons/godebug v1.1.0 // indirect github.com/pkg/errors v0.8.1 // indirect + github.com/stretchr/testify v1.4.0 // indirect golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b willnorris.com/go/microformats v1.0.0 diff --git a/go.sum b/go.sum index 9ef4814..6141061 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,24 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E= github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b h1:MzIItcq83xZn1apSjCNovs55CZE/R/7GUnlOeIJ9HRI= p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b/go.mod h1:NDCrcUlnixnkQ4u36jm3Hhvq0WcayEjg8SpAsFdQWLE= willnorris.com/go/microformats v1.0.0 h1:II6uDIJBPp6RpJQqRWm+6IN9lI00mN/jQAC5OHuF4HA= diff --git a/main.go b/main.go index ae97fb1..8511833 100644 --- a/main.go +++ b/main.go @@ -2,16 +2,16 @@ package main import ( "bytes" + "flag" "fmt" - "html/template" "io" - "io/ioutil" "log" "net/http" "net/url" "os" - "regexp" + "os/exec" "strings" + tt "text/template" "time" "github.com/jinzhu/now" @@ -20,6 +20,12 @@ import ( "willnorris.com/go/microformats" ) +var ( + show = flag.Bool("show", false, "Show the output") + server = flag.String("endpoint", "", "Micropub endpoint specified in shpub") +) + +// Summary is a summary of the last few posts on a blog type Summary struct { PostTypes map[string]string Name string @@ -30,27 +36,20 @@ type Summary struct { func init() { log.SetFlags(log.LstdFlags | log.Lshortfile) } - func main() { - u, err := url.Parse(os.Args[1]) + flag.Parse() + + if !*show && *server == "" { + log.Println("Specify -endpoint when using micropub") + return + } + + args := flag.Args() + + u, err := url.Parse(args[0]) if err != nil { log.Fatal(err) } - filename := os.Args[2] - - re, err := regexp.Compile(``) - if err != nil { - log.Fatal(err) - } - - f, err := forceCreate(filename) - - data, err := ioutil.ReadAll(f) - if err != nil { - log.Fatal(err) - } - - f.Close() var buf bytes.Buffer items, err := getEntriesForFeed(u) @@ -68,10 +67,14 @@ func main() { log.Fatal(err) } - output := re.ReplaceAll(data, buf.Bytes()) - err = ioutil.WriteFile(filename, output, 0666) - if err != nil { - log.Fatal(err) + if *show { + io.Copy(os.Stdout, &buf) + } else { + fmt.Println("micropub") + err = micropub(summary.Name, buf.String(), *server) + if err != nil { + log.Fatal(err) + } } } @@ -83,73 +86,36 @@ func getSummary(monday time.Time, items []microsub.Item) Summary { grouped[itemType] = append(grouped[itemType], item) } summary := Summary{ - Name: fmt.Sprintf("Digest for Week %d-%d", week, year), - Items: items, - Grouped: grouped, + Name: fmt.Sprintf("Digest for Week %d-%d", week, year), + Items: items, + Grouped: grouped, } return summary } -func forceCreate(filename string) (io.ReadCloser, error) { - t, err := template.ParseFiles("templates/full.html") - if err != nil { - return nil, err - } - f, err := os.Create(filename) - 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 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 { 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", + "checkin": "Checkins", + "event": "Events", + "repost": "Reposts", + "like": "Likes", + "reply": "Replies", + "bookmark": "Bookmarks", + "photo": "Photos", + "note": "Notes", + "article": "Articles", } - t, err := template.ParseFiles("templates/weekly.html") + t, err := tt.ParseFiles("templates/weekly.md.tmpl") if err != nil { log.Fatal(err) } + err = t.Execute(w, summary) if err != nil { return err } + return nil } @@ -163,19 +129,19 @@ func postTypeDiscovery(item *microsub.Item) string { if item.Type == "event" { return "event" } - if len(item.RepostOf) > 0 && validUrl(item.RepostOf[0]) { - return "repost-of" + if len(item.RepostOf) > 0 && validURL(item.RepostOf[0]) { + return "repost" } - if len(item.LikeOf) > 0 && validUrl(item.LikeOf[0]) { - return "like-of" + if len(item.LikeOf) > 0 && validURL(item.LikeOf[0]) { + return "like" } - if len(item.InReplyTo) > 0 && validUrl(item.InReplyTo[0]) { - return "in-reply-to" + if len(item.InReplyTo) > 0 && validURL(item.InReplyTo[0]) { + return "reply" } - if len(item.BookmarkOf) > 0 && validUrl(item.BookmarkOf[0]) { + if len(item.BookmarkOf) > 0 && validURL(item.BookmarkOf[0]) { return "bookmark" } - if len(item.Photo) > 0 && validUrl(item.Photo[0]) { + if len(item.Photo) > 0 && validURL(item.Photo[0]) { return "photo" } @@ -209,7 +175,7 @@ func postTypeDiscovery(item *microsub.Item) string { return "note" } -func validUrl(u string) bool { +func validURL(u string) bool { _, err := url.Parse(u) return err == nil } @@ -236,9 +202,31 @@ func filterEntriesAfter(items []microsub.Item, from time.Time) []microsub.Item { if err != nil { continue } - if published.After(from) { - result = append(result, item) + if !published.After(from) { + continue } + found := false + for _, cat := range item.Category { + if cat == "weekly-digest" { + found = true + } + } + if found { + continue + } + result = append(result, item) } return result } + +func micropub(name, content, server string) error { + var args []string + if server != "" { + args = append(args, "-s", server) + } + args = append(args, "-d", "article", "-c", "weekly-digest", "--json", "-x", "p3k-content-type=text/markdown", name, content) + cmd := exec.Command("shpub", args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} diff --git a/templates/weekly.html b/templates/weekly.html index fd6e663..3fba952 100644 --- a/templates/weekly.html +++ b/templates/weekly.html @@ -1,32 +1,36 @@ - -
-

{{ .Name }}

- {{ range $k, $items := .Grouped }} -

{{ index $.PostTypes $k }}

-
\ No newline at end of file + {{ end }} + +{{ end }} diff --git a/templates/weekly.md.tmpl b/templates/weekly.md.tmpl new file mode 100644 index 0000000..a8e2694 --- /dev/null +++ b/templates/weekly.md.tmpl @@ -0,0 +1,24 @@ +{{ range $k, $items := .Grouped }} +### {{ index $.PostTypes $k }} + +{{ range $items -}} +{{- $item := . -}} +{{- if eq $k "like" -}} + {{ range .LikeOf }}* [{{ . }}]({{ . }}){{ end }} +{{ else if eq $k "repost" -}} + {{ range .RepostOf }}* [{{ . }}]({{ . }}){{ end }} +{{ else if eq $k "bookmark" -}} + {{ range .BookmarkOf }}* [{{ $item.Name }}]({{ . }}){{ end }} +{{ else if eq $k "reply" -}} + {{ range .InReplyTo }}* [{{ . }}]({{ . }}){{ end }} +{{ else if eq $k "article" -}} + * [{{ .Name }}]({{ .URL }}) +{{ else if .Checkin -}} + * [Checkin at {{ .Checkin.Name }} in {{ .Checkin.Locality }}, {{ .Checkin.Region }}]({{ .URL }}) +{{ else if .Content -}} + * {{ .Content.Text }} [link]({{ .URL }}) +{{ else -}} + * [{{ .Name }}]({{ .URL }}) +{{- end -}} +{{ end -}} +{{ end -}} \ No newline at end of file