package history import ( "fmt" "html" "log/slog" "net/url" "os" "regexp" "strconv" "strings" "time" "github.com/bouncepaw/mycorrhiza/internal/cfg" "github.com/bouncepaw/mycorrhiza/internal/files" ) // WithRevisions returns an HTML representation of `revs` that is meant to be inserted in a history page. func WithRevisions(hyphaName string, revs []Revision) string { var buf strings.Builder for _, grp := range groupRevisionsByMonth(revs) { currentYear := grp[0].Time.Year() currentMonth := grp[0].Time.Month() sectionId := fmt.Sprintf("%04d-%02d", currentYear, currentMonth) buf.WriteString(fmt.Sprintf( `

%d %s

`) } return buf.String() } // Revision represents a revision of a hypha. type Revision struct { // Hash is usually short. Hash string // Username is extracted from email. Username string Time time.Time Message string filesAffectedBuf []string hyphaeAffectedBuf []string } // HyphaeDiffsHTML returns a comma-separated list of diffs links of current revision for every affected file as HTML string. func (rev Revision) HyphaeDiffsHTML() string { entries := rev.hyphaeAffected() if len(entries) == 1 { return fmt.Sprintf( `%s`, rev.Hash, entries[0], rev.Hash, ) } var buf strings.Builder for i, hyphaName := range entries { if i > 0 { buf.WriteString(``) } buf.WriteString(``) if i == 0 { buf.WriteString(rev.Hash) buf.WriteString(" ") } buf.WriteString(hyphaName) buf.WriteString(``) } return buf.String() } // descriptionForFeed generates a good enough HTML contents for a web feed. func (rev *Revision) descriptionForFeed() string { return fmt.Sprintf( `

%s (by %s at %s)

Hyphae affected: %s

%s
`, rev.Message, rev.Username, rev.TimeString(), rev.HyphaeLinksHTML(), rev.textDiff(), ) } // HyphaeLinksHTML returns a comma-separated list of hyphae that were affected by this revision as HTML string. func (rev Revision) HyphaeLinksHTML() string { var buf strings.Builder for i, hyphaName := range rev.hyphaeAffected() { if i > 0 { buf.WriteString(`