summaryrefslogtreecommitdiff
path: root/cmd/generate/main.go
blob: 7fa842fb6c8d11f9f00462c417cb435c6037968e (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
package main

import (
	"bytes"
	_ "embed"
	"fmt"
	"os"
	"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 {
	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{
			"timeTag": func(ts time.Time, prettyFmt string) (template.HTML, error) {
				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
			},
			"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,
	}); 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
}