summaryrefslogtreecommitdiff
path: root/cmd/generate/main.go
blob: dd226adb33c6c672cc296179577b62f5cbb5a984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main

import (
	"bytes"
	_ "embed"
	"fmt"
	"os"
	"reflect"
	"sort"
	"strings"
	"time"

	"html/template"

	"github.com/yuin/goldmark"
)

func MarkdownToHTML(md string) (template.HTML, error) {
	var html strings.Builder
	if err := goldmark.Convert([]byte(md), &html); err != nil {
		return template.HTML(""), err
	}
	return template.HTML(html.String()), nil
}

var githubProjects = map[string]string{
	"flori/json": "ruby-json",
}

func main() {
	if err := mainWithError(); err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}
}

//go:embed imworkingon.html.tmpl
var htmlTmplStr string

var timeTagTmpl = template.Must(template.New("time.tag.tmpl").
	Parse(`<time datetime="{{ .Machine }}" title="{{ .HumanVerbose }}">{{ .HumanPretty }}</time>`))

func mainWithError() error {
	standups, err := ReadStandups("https://fosstodon.org", "lukeshu")
	if err != nil {
		return err
	}
	contribs, err := ReadContribs("imworkingon/contribs.yml")
	if err != nil {
		return err
	}
	tags, err := ReadTags("imworkingon/tags.yml")
	if err != nil {
		return err
	}
	upstreams, err := ReadUpstreams("imworkingon/upstreams.yml")
	if err != nil {
		return err
	}

	sort.Slice(contribs, func(i, j int) bool {
		iDate := contribs[i].LastUpdatedAt
		if iDate.IsZero() {
			iDate = contribs[i].SubmittedAt
		}
		jDate := contribs[j].LastUpdatedAt
		if jDate.IsZero() {
			jDate = contribs[j].SubmittedAt
		}
		return iDate.After(jDate)
	})

	tmpl := template.Must(template.New("imworkingon.html.tmpl").
		Funcs(template.FuncMap{
			"time": func() map[string]time.Weekday {
				return map[string]time.Weekday{
					"Sunday":    time.Sunday,
					"Monday":    time.Monday,
					"Tuesday":   time.Tuesday,
					"Wednesday": time.Wednesday,
					"Thursday":  time.Thursday,
					"Friday":    time.Friday,
					"Saturday":  time.Saturday,
				}
			},
			"reverse": func(x any) any {
				in := reflect.ValueOf(x)
				l := in.Len()
				out := reflect.MakeSlice(in.Type(), l, l)
				for i := 0; i < l; i++ {
					out.Index(l - (i + 1)).Set(in.Index(i))
				}
				return out.Interface()
			},
			"timeTag": func(ts time.Time, prettyFmt string) (template.HTML, error) {
				ts = ts.Local()
				var out strings.Builder
				err := timeTagTmpl.Execute(&out, map[string]string{
					"Machine":      ts.Format(time.RFC3339),
					"HumanVerbose": ts.Format("2006-01-02 15:04:05Z07:00"),
					"HumanPretty":  ts.Format(prettyFmt),
				})
				return template.HTML(out.String()), err
			},
			"monthClass": func(m time.Month) string {
				if m%2 == 0 {
					return "even-month"
				} else {
					return "odd-month"
				}
			},
			"md2html": MarkdownToHTML,
			"getUpstream": func(c Contribution) Upstream {
				// First try any of the documented upstreams.
				for _, cURL := range c.URLs {
					for _, upstream := range upstreams {
						for _, uURL := range upstream.URLs {
							prefix := uURL
							if !strings.HasSuffix(prefix, "/") {
								prefix += "/"
							}
							if cURL == uURL || strings.HasPrefix(cURL, prefix) {
								return upstream
							}
						}
					}
				}
				return Upstream{URLs: []string{c.URLs[0]}, Name: "???"}
			},
		}).
		Parse(htmlTmplStr))
	var out bytes.Buffer
	if err := tmpl.Execute(&out, map[string]any{
		"Contribs":        contribs,
		"Tags":            tags,
		"Upstreams":       upstreams,
		"Standups":        standups,
		"StandupCalendar": BuildCalendar(standups, func(status *MastodonStatus) Date { return DateOf(status.CreatedAt) }),
	}); err != nil {
		return err
	}
	if err := os.WriteFile("public/imworkingon/index.new.html", out.Bytes(), 0666); err != nil {
		return err
	}
	if err := os.Rename("public/imworkingon/index.new.html", "public/imworkingon/index.html"); err != nil {
		return err
	}
	return nil
}