@@ -0,0 +1,116 @@
1+package site
2+
3+import (
4+ "slices"
5+ "strings"
6+ "time"
7+ "unicode"
8+
9+ "xonecas.io/site/internal/project"
10+)
11+
12+// heat is the fewest commits for each heat level after h0.
13+var heat = []int{1, 15, 40, 100, 200, 500}
14+
15+// Rows turns the shown projects, oldest first, into list rows. Num is the order of discovery,
16+// symbols are unique and go to the oldest project first, and the bar spans the whole timeline.
17+func Rows(projects []project.Project) []ProjectRow {
18+ first, last := projects[0].First, projects[0].Last
19+ for _, p := range projects {
20+ first, last = min(first, p.First), max(last, p.Last)
21+ }
22+ start, end := day(first), day(last)
23+ span := max(end.Sub(start).Hours(), 24)
24+ taken := map[string]bool{}
25+ rows := make([]ProjectRow, len(projects))
26+ for i, p := range projects {
27+ r := ProjectRow{
28+ El: Element{Num: i + 1, Weight: p.Commits, Sym: symbol(p.Name, taken), Name: p.Name, Group: group(p.Tags), Post: slices.Contains(p.Tags, "post-AI")},
29+ Desc: p.Description,
30+ From: 100 * day(p.First).Sub(start).Hours() / span,
31+ Width: 100 * day(p.Last).Sub(day(p.First)).Hours() / span,
32+ Tags: p.Tags,
33+ First: p.First,
34+ Last: p.Last,
35+ }
36+ for _, n := range heat {
37+ if p.Commits >= n {
38+ r.Heat++
39+ }
40+ }
41+ if len(p.Languages) > 0 {
42+ r.Lang = p.Languages[0].Name
43+ }
44+ rows[i] = r
45+ }
46+ return rows
47+}
48+
49+func day(s string) time.Time {
50+ t, _ := time.Parse(time.DateOnly, s)
51+ return t
52+}
53+
54+func group(tags []string) string {
55+ fe, be := slices.Contains(tags, "frontend"), slices.Contains(tags, "backend")
56+ switch {
57+ case fe && be:
58+ return "both"
59+ case fe:
60+ return "fe"
61+ case be:
62+ return "be"
63+ }
64+ return "none"
65+}
66+
67+// symbol is the name's first letter and the first later letter that makes an unused pair,
68+// trying the first letters of words before the rest.
69+func symbol(name string, taken map[string]bool) string {
70+ var initials, rest []rune
71+ first, word := rune(0), false
72+ for _, c := range strings.ToLower(name) {
73+ switch {
74+ case !unicode.IsLetter(c) && !unicode.IsDigit(c):
75+ word = true
76+ case first == 0:
77+ first = c
78+ case word:
79+ initials, word = append(initials, c), false
80+ default:
81+ rest = append(rest, c)
82+ }
83+ }
84+ for _, c := range append(initials, rest...) {
85+ if s := string(unicode.ToUpper(first)) + string(c); !taken[s] {
86+ taken[s] = true
87+ return s
88+ }
89+ }
90+ s := string(unicode.ToUpper(first))
91+ taken[s] = true
92+ return s
93+}
94+
95+// TagCount is a tag and how many shown projects have it.
96+type TagCount struct {
97+ Name string
98+ Count int
99+}
100+
101+// projectsData is the /projects page: the rows by last commit, and the tag pairs with their counts.
102+func projectsData(shown []project.Project) map[string]any {
103+ rows := Rows(shown)
104+ slices.SortStableFunc(rows, func(a, b ProjectRow) int { return strings.Compare(b.Last, a.Last) })
105+ pairs := [][]TagCount{{{"frontend", 0}, {"backend", 0}}, {{"small", 0}, {"big", 0}}, {{"pre-AI", 0}, {"post-AI", 0}}}
106+ for _, r := range rows {
107+ for _, pair := range pairs {
108+ for i := range pair {
109+ if slices.Contains(r.Tags, pair[i].Name) {
110+ pair[i].Count++
111+ }
112+ }
113+ }
114+ }
115+ return map[string]any{"Rows": rows, "Pairs": pairs}
116+}