snowchive-server/changelog.go
2026-11-19 04:40:59 +02:00

102 lines
2.7 KiB
Go

// changelog.go - the archive changelog (/changelog).
//
// The changelog is a HAND-MAINTAINED file: data/changelog.json. Shape:
//
// [
// {
// "date": "2026-06-11", // YYYY-MM-DD (or full RFC3339 if you care)
// "type": "added", // added | removed | updated | note
// "id": "some-doc-id", // which document (omit for type=note)
// "title": "Document title", // shown in the headline (omit for note)
// "components": ["pdf"], // optional, for updated: what changed
// "note": "free text" // the body for type=note, optional otherwise
// }
// ]
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"sort"
"strings"
"time"
)
type ChangeEntry struct {
Date string `json:"date"`
Type string `json:"type"`
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Components []string `json:"components,omitempty"`
Note string `json:"note,omitempty"`
}
func (co *Corpus) loadChangelog() {
p := projectPath("changelog.json")
b, err := os.ReadFile(p)
if err != nil {
log.Printf("Note: %s not found - the changelog will be empty (see the format at the top of changelog.go).", p)
return
}
if err := json.Unmarshal(b, &co.changelog); err != nil {
warnf("%s is malformed: %v - changelog disabled.", p, err)
co.changelog = nil
return
}
sort.SliceStable(co.changelog, func(i, j int) bool {
return co.changelog[i].Date > co.changelog[j].Date
})
for _, e := range co.changelog {
if !validChangelogDate(e.Date) {
warnf("changelog entry %q has date %q - must be YYYY-MM-DD or RFC3339", e.Headline(), e.Date)
}
if e.Type != "removed" && e.ID != "" && co.docIndex[e.ID] == nil {
warnf("changelog entry %q names document %q, which is not in the corpus", e.Headline(), e.ID)
}
}
log.Printf("Changelog loaded: %d entries", len(co.changelog))
}
func validChangelogDate(d string) bool {
if _, err := time.Parse("2006-01-02", d); err == nil {
return true
}
_, err := time.Parse(time.RFC3339, d)
return err == nil
}
func (e ChangeEntry) Headline() string {
switch e.Type {
case "added":
return "Added: " + e.Title
case "removed":
return "Removed: " + e.Title
case "updated":
s := "Updated: " + e.Title
if len(e.Components) > 0 {
s += " (" + strings.Join(e.Components, ", ") + ")"
}
return s
default: // note
if e.Note != "" {
return e.Note
}
return "Archive note"
}
}
func (e ChangeEntry) DocURL() string {
if e.ID != "" && e.Type != "removed" {
return docPath(e.ID)
}
return "/changelog"
}
func (co *Corpus) handleChangelog(w http.ResponseWriter, r *http.Request) {
render(w, r, &changelogPage{
Entries: co.changelog,
Total: len(co.changelog),
})
}