xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / project / describe.go

2.8 KB
1package project
2
3import (
4	"regexp"
5	"strings"
6
7	"xonecas.io/site/internal/repo"
8	"xonecas.io/site/internal/store"
9)
10
11// PostAI is the day the earliest AI instruction file appeared (the-champs-timer's AGENTS.md).
12// A project whose first commit is on or after it is post-AI.
13const PostAI = "2025-09-05"
14
15// Big is how many of my commits make a project big.
16const Big = 50
17
18// Info is everything the pages show about a project, computed from git and the hosts.
19type Info struct {
20	Description string         `json:"description"`
21	First       string         `json:"first"`
22	Last        string         `json:"last"`
23	Commits     int            `json:"commits"` // mine
24	Total       int            `json:"total"`   // everyone's
25	Months      map[string]int `json:"months"`  // my commits per YYYY-MM
26	Files       int            `json:"files"`
27	Languages   []Language     `json:"languages"`
28	Readme      string         `json:"readme"`
29	Tags        []string       `json:"tags"`
30	Topics      []string       `json:"topics"`           // specific tags, from topics.go
31	Hidden      string         `json:"hidden,omitempty"` // why it is not listed: fork, empty or override
32}
33
34// Describe computes p's Info from its sources, the hosts' metadata and the overrides.
35func Describe(p *Project, metas map[string]store.Meta, o Overrides) error {
36	h, err := ReadHistory(p.Sources)
37	if err != nil {
38		return err
39	}
40	f, err := ReadFiles(p.Sources[0])
41	if err != nil {
42		return err
43	}
44	in := &p.Info
45	*in = Info{First: h.First, Last: h.Last, Commits: h.Mine, Total: h.Total, Months: h.Months, Files: f.Count, Languages: f.Languages, Readme: f.Readme, Topics: f.Topics}
46	fork := false
47	for _, s := range p.Sources {
48		m := metas[s.URL]
49		fork = fork || m.Fork
50		if in.Description == "" {
51			in.Description = m.Description
52		}
53	}
54	if in.Description == "" && f.Readme != "" {
55		in.Description = summary(p.Sources[0].Dir, f.Readme)
56	}
57
58	in.Tags = []string{"pre-AI", "small"}
59	if h.AI || h.First >= PostAI {
60		in.Tags[0] = "post-AI"
61	}
62	if h.Mine >= Big {
63		in.Tags[1] = "big"
64	}
65	if f.Front {
66		in.Tags = append(in.Tags, "frontend")
67	}
68	if f.Back {
69		in.Tags = append(in.Tags, "backend")
70	}
71	in.Tags = o.retag(p.Name, in.Tags, known)
72	in.Topics = o.retag(p.Name, in.Topics, topicNames)
73
74	switch {
75	case o.Show[p.Name]:
76	case o.Hide[p.Name]:
77		in.Hidden = "override"
78	case fork:
79		in.Hidden = "fork"
80	case h.Mine == 0:
81		in.Hidden = "empty"
82	}
83	return nil
84}
85
86var mdLink = regexp.MustCompile(`\[([^\]]*)\]\([^)]*\)`)
87
88// summary is the README's first line of prose, for projects with no description.
89func summary(dir, readme string) string {
90	text, err := repo.Git(dir, "show", "HEAD:"+readme)
91	if err != nil {
92		return ""
93	}
94	for _, line := range strings.Split(text, "\n") {
95		line = strings.TrimSpace(line)
96		if line != "" && !strings.ContainsAny(line[:1], "#[!<=-|`>*") {
97			return mdLink.ReplaceAllString(line, "$1")
98		}
99	}
100	return ""
101}