// Package colophon is the suite's Colophon: every open-source project an // app is made with, said in a sentence or two, with who made it, the // licence it is under and where it lives. // // The words are the catalog's (projects.json), one entry per project, so // every app that ships Yjs describes Yjs the same way. What an app uses a // project for is the entry's usage note for that app. The list is not // written by hand: an app collects what it actually ships (Go modules from // its production build, JavaScript packages from its bundles, Rust crates // from its WebAssembly, fonts, icons and vendored files) and Build matches // that against the catalog. Build refuses a shipped package the catalog // does not know, a usage note for something the app no longer ships, and // a package without its licence text, so an app's own test of Build is // the test that the Colophon and the shipped set cannot differ. // // The page needs no script and downloads no font: it is chrome, set in the // system UI face it inherits (AGENTS.md row 9). package colophon import ( "bytes" "embed" "encoding/json" "errors" "fmt" "net/url" "regexp" "sort" "strings" "unicode" "unicode/utf8" "amadan.net/oficina/estilo.git/credits" ) //go:embed projects.json var projectsJSON []byte //go:embed templates/*.html var templateFS embed.FS //go:embed colophon.css var stylesheet []byte // Templates holds "estilo/colophon" (Page) and "estilo/colophon-licence" // (LicencePage). func Templates() embed.FS { return templateFS } // CSS is the page's stylesheet, linked after estilo's base. func CSS() []byte { return append([]byte(nil), stylesheet...) } // Kind is what sort of thing a project is, and so which section it is in. type Kind string const ( Typeface Kind = "typeface" Library Kind = "library" Icons Kind = "icons" Data Kind = "data" Tool Kind = "tool" Service Kind = "service" ) // Ecosystems: where a package's name comes from. const ( Go = "go" // a Go module path NPM = "npm" // an npm package name Cargo = "cargo" // a crates.io crate name Swift = "swift" // a Swift package's identity in Package.resolved Font = "font" // a font family's name Asset = "asset" // a vendored file or set of files Toolchain = "toolchain" // a language's compiler and standard library Pip = "pip" // a Python package used by a script ) // NoAssertion is the licence of a package published with no licence file // (SPDX's own word for it). It is the one licence a package may ship // without a notice, and the page says so. const NoAssertion = "NOASSERTION" // Licence is a project's licence: its SPDX expression and its name as a // person reads it. type Licence struct { SPDX string `json:"spdx"` Name string `json:"name"` } // Ref names a package a project publishes. type Ref struct { Ecosystem string `json:"ecosystem"` Name string `json:"name"` } func (r Ref) String() string { return r.Ecosystem + ":" + r.Name } // Project is one catalog entry. type Project struct { ID string `json:"id"` Name string `json:"name"` Kind Kind `json:"kind"` // Blurb says what the project is, in one or two sentences. Blurb string `json:"blurb"` // Author is who holds the copyright, as its licence file names them, // not a package manifest's list of maintainers. Where the licence names // no one (many MIT and Apache texts), it is the project's own authors. Author string `json:"author"` Licence Licence `json:"licence"` Home string `json:"home"` Source string `json:"source"` // Packages are every package of the project an app might ship. Packages []Ref `json:"packages"` // Uses is what each app uses it for, keyed by the app's colophon key // ("docs", "docs-native"), one or two sentences each. "*" is the note // for every app without its own ("Yrs uses it."), so a transitive // dependency is written once, not once per app. Only an app's own // note can be stale. Uses map[string]string `json:"uses"` // Via is the projects (catalog ids) a "*" note names as what uses this // one ("serde_json uses it." is via serde-json). A crate's default note // is true in an app only if every one of them uses the crate there, // through a package that ships, so Build checks it against the // dependents CollectCargo records. Via []string `json:"via,omitempty"` } var projects = func() []Project { var file struct { Comment string `json:"comment"` Projects []Project `json:"projects"` } decoder := json.NewDecoder(bytes.NewReader(projectsJSON)) decoder.DisallowUnknownFields() if err := decoder.Decode(&file); err != nil { panic("colophon: projects.json: " + err.Error()) } if err := Validate(file.Projects); err != nil { panic(err) } return file.Projects }() // Catalog is a copy of every project in the catalog. func Catalog() []Project { out := make([]Project, len(projects)) for i, p := range projects { p.Packages = append([]Ref(nil), p.Packages...) uses := make(map[string]string, len(p.Uses)) for k, v := range p.Uses { uses[k] = v } p.Uses = uses out[i] = p } return out } var ( idPattern = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`) sentenceEnd = regexp.MustCompile(`[.!?]["”’)]?(\s+|$)`) knownKinds = map[Kind]bool{Typeface: true, Library: true, Icons: true, Data: true, Tool: true, Service: true} knownSystems = map[string]bool{Go: true, NPM: true, Cargo: true, Swift: true, Font: true, Asset: true, Toolchain: true, Pip: true} ) // sentences counts the sentences in s, roughly: a full stop, question or // exclamation mark followed by a space or the end. Abbreviations count // too, which errs towards shorter blurbs. func sentences(s string) int { return len(sentenceEnd.FindAllStringIndex(s, -1)) } // sentence reports whether s is written as sentences: it ends with a full // stop. A usage note may open with a project's own lower-case name // ("wasmi uses it."); a blurb opens with a capital (capitalised). func sentence(s string) bool { s = strings.TrimSpace(s) return s != "" && sentenceEnd.MatchString(s) && strings.HasSuffix(strings.TrimRight(s, `"”’)`), ".") } // capitalised reads the first letter, not the first byte: "Łukasz" is // capitalised, and Validate runs at init. func capitalised(s string) bool { r, _ := utf8.DecodeRuneInString(strings.TrimSpace(s)) return r != utf8.RuneError && !unicode.IsLower(r) } func httpsURL(s string) bool { u, err := url.Parse(s) return err == nil && u.Scheme == "https" && u.Host != "" } // Validate reports every way a list of projects falls short of a catalog: // an entry without a blurb of one or two sentences, an author, a licence, // an https home and source, or a package; a package two projects claim; // and a usage note that is not a sentence. func Validate(projects []Project) error { var problems []string ids := map[string]bool{} owner := map[Ref]string{} for _, p := range projects { bad := func(format string, args ...any) { problems = append(problems, p.ID+": "+fmt.Sprintf(format, args...)) } if !idPattern.MatchString(p.ID) { bad("id is not lower-case words joined by hyphens") } if ids[p.ID] { bad("id is used twice") } for _, s := range sectionOrder { if p.ID == s { bad("id is a section's, and the page anchors both") } } ids[p.ID] = true if strings.TrimSpace(p.Name) == "" { bad("no name") } if !knownKinds[p.Kind] { bad("unknown kind %q", p.Kind) } if !sentence(p.Blurb) || !capitalised(p.Blurb) || sentences(p.Blurb) > 2 { bad("the blurb must be one or two sentences") } if strings.TrimSpace(p.Author) == "" { bad("no author") } if strings.TrimSpace(p.Licence.SPDX) == "" || strings.TrimSpace(p.Licence.Name) == "" { bad("no licence") } if !httpsURL(p.Home) || !httpsURL(p.Source) { bad("home and source must be https URLs") } if len(p.Packages) == 0 { bad("no packages") } for _, r := range p.Packages { if !knownSystems[r.Ecosystem] || r.Name == "" { bad("unknown package %s", r) continue } if other, taken := owner[r]; taken { bad("%s is also %s's", r, other) } owner[r] = p.ID } cargo := false for _, r := range p.Packages { cargo = cargo || r.Ecosystem == Cargo } if p.Uses["*"] != "" && cargo && len(p.Via) == 0 { bad("a crate's default note must say, in via, which dependents it names") } for app, use := range p.Uses { if !sentence(use) || sentences(use) > 2 { bad("the usage note for %s must be one or two sentences", app) } } } for _, p := range projects { for _, v := range p.Via { if !ids[v] { problems = append(problems, p.ID+": via names "+v+", which is not in the catalog") } } } if len(problems) > 0 { sort.Strings(problems) return errors.New("colophon: catalog:\n " + strings.Join(problems, "\n ")) } return nil } // Package is one package an app ships, as its collector found it. type Package struct { Ecosystem string `json:"ecosystem"` Name string `json:"name"` Version string `json:"version,omitempty"` // Tool marks a package the app is built or tested with that is not in // what it ships. Tool bool `json:"tool,omitempty"` // Platforms limits a package to some of the app's platforms ("mac"); // empty is all of them. Platforms []string `json:"platforms,omitempty"` // Via is the packages (by name, same ecosystem) that use this one // directly in the build that ships it, as CollectCargo records them. Via []string `json:"via,omitempty"` // Notices are the package's own licence files, unaltered. Notices []credits.Notice `json:"notices"` } func (p Package) ref() Ref { return Ref{p.Ecosystem, p.Name} } // Entry is one project as an app's Colophon shows it. type Entry struct { ID string `json:"id"` Name string `json:"name"` Blurb string `json:"blurb"` Usage string `json:"usage"` Author string `json:"author"` Licence Licence `json:"licence"` Home string `json:"home"` Source string `json:"source"` Packages []Package `json:"packages"` } // VersionText is the entry's version, or each package's when they differ. func (e Entry) VersionText() string { versions := map[string]bool{} for _, p := range e.Packages { versions[p.Version] = true } if len(versions) == 1 { return e.Packages[0].Version } var parts []string for _, p := range e.Packages { if p.Version != "" { parts = append(parts, p.Name+" "+p.Version) } } return strings.Join(parts, ", ") } // Section is one heading's entries. ID is one of sectionOrder. type Section struct { ID string `json:"id"` Entries []Entry `json:"entries"` } // Colophon is an app's Colophon, its sections in sectionOrder and their // entries by name. type Colophon struct { Sections []Section `json:"sections"` } // sectionOrder is the order sections are shown in. var sectionOrder = []string{"typefaces", "go", "javascript", "rust", "swift", "icons", "data", "tools", "services"} // Entry finds the entry with id. func (c Colophon) Entry(id string) (Entry, bool) { for _, s := range c.Sections { for _, e := range s.Entries { if e.ID == id { return e, true } } } return Entry{}, false } func (c Colophon) sectionOf(id string) string { for _, s := range c.Sections { for _, e := range s.Entries { if e.ID == id { return s.ID } } } return "" } // Count is the number of entries in each section, and "total". func (c Colophon) Count() map[string]int { n := map[string]int{} for _, s := range c.Sections { n[s.ID] = len(s.Entries) n["total"] += len(s.Entries) } return n } // Build is app's Colophon for what it ships, from the catalog. app is the // key of the usage notes it reads ("docs", "docs-native"). func Build(app string, shipped []Package) (Colophon, error) { return build(projects, app, shipped) } func build(projects []Project, app string, shipped []Package) (Colophon, error) { var problems []string owner := map[Ref]int{} for i, p := range projects { for _, r := range p.Packages { owner[r] = i } } // One package per ecosystem, name and version. A package seen as a tool // in one build and shipped by another ships; a package limited to some // platforms in one and not in another is on all of them. merged := map[string]*Package{} var keys []string for _, p := range shipped { if p.Ecosystem == "" || p.Name == "" { problems = append(problems, "a package without an ecosystem or name") continue } key := p.Ecosystem + ":" + p.Name + "@" + p.Version if have, ok := merged[key]; ok { have.Tool = have.Tool && p.Tool have.Via = union(have.Via, p.Via) if len(have.Platforms) == 0 || len(p.Platforms) == 0 { have.Platforms = nil } else { have.Platforms = union(have.Platforms, p.Platforms) } continue } p.Platforms = append([]string(nil), p.Platforms...) merged[key] = &p keys = append(keys, key) } sort.Strings(keys) byProject := map[int][]Package{} for _, key := range keys { p := *merged[key] i, known := owner[p.ref()] if !known { problems = append(problems, p.ref().String()+" has no catalog entry") continue } if len(p.Notices) == 0 && !p.Tool && projects[i].Licence.SPDX != NoAssertion { problems = append(problems, fmt.Sprintf("%s %s carries no licence text", p.ref(), p.Version)) } byProject[i] = append(byProject[i], p) } sections := map[string][]Entry{} for i, p := range projects { packages, ships := byProject[i] own := p.Uses[app] use := own if use == "" { use = p.Uses["*"] } switch { case ships && use == "": problems = append(problems, p.ID+" has no usage note for "+app) continue case !ships && own != "": problems = append(problems, p.ID+" has a usage note for "+app+" but "+app+" does not ship it") continue case !ships: continue } if own == "" && len(p.Via) > 0 && !reachedVia(p, packages, projects, owner, byProject) { problems = append(problems, p.ID+"'s default note names "+strings.Join(p.Via, ", ")+", which "+app+" does not reach it through") continue } section, err := sectionFor(p, packages) if err != nil { problems = append(problems, err.Error()) continue } sections[section] = append(sections[section], Entry{ID: p.ID, Name: p.Name, Blurb: p.Blurb, Usage: use, Author: p.Author, Licence: p.Licence, Home: p.Home, Source: p.Source, Packages: packages}) } if len(problems) > 0 { sort.Strings(problems) return Colophon{}, errors.New("colophon: " + app + ":\n " + strings.Join(problems, "\n ")) } var c Colophon for _, id := range sectionOrder { entries := sections[id] if len(entries) == 0 { continue } sort.SliceStable(entries, func(a, b int) bool { return strings.ToLower(entries[a].Name) < strings.ToLower(entries[b].Name) }) c.Sections = append(c.Sections, Section{ID: id, Entries: entries}) } return c, nil } // reachedVia reports whether every project p's default note names uses // one of packages directly ("DashMap and aHash use it." needs both), each // through a package it ships. A dependent the app only builds or tests with // counts only when the crate itself is only a tool there too: a shipped // crate's note must not rest on something that does not ship. func reachedVia(p Project, packages []Package, projects []Project, owner map[Ref]int, byProject map[int][]Package) bool { onlyTool := true for _, pkg := range packages { onlyTool = onlyTool && pkg.Tool } for _, named := range p.Via { uses := false for _, pkg := range packages { for _, user := range pkg.Via { i, ok := owner[Ref{pkg.Ecosystem, user}] if !ok || projects[i].ID != named { continue } for _, shipped := range byProject[i] { if shipped.Name == user && (!shipped.Tool || onlyTool) { uses = true } } } } if !uses { return false } } return true } func union(a, b []string) []string { seen := map[string]bool{} var out []string for _, s := range append(append([]string(nil), a...), b...) { if !seen[s] { seen[s] = true out = append(out, s) } } sort.Strings(out) return out } func sectionFor(p Project, packages []Package) (string, error) { tool := true ecosystem := "" for _, pkg := range packages { if !pkg.Tool { tool = false if ecosystem == "" { ecosystem = pkg.Ecosystem } } } if tool { return "tools", nil } switch p.Kind { case Typeface: return "typefaces", nil case Icons: return "icons", nil case Data: return "data", nil case Tool: return "tools", nil case Service: return "services", nil } switch ecosystem { case Go: return "go", nil case NPM: return "javascript", nil case Cargo: return "rust", nil case Swift: return "swift", nil } return "", fmt.Errorf("%s is a library with no runtime to list it under", p.ID) } // Labels are the page's words, in the caller's locale. Sections is each // section's heading, keyed by its ID. type Labels struct { Contents string Sections map[string]string By string Licence string Version string Home string Source string HomeAndSource string // Back is the licence page's link back to the Colophon, Missing what // it says for a package published without a licence file, and // NotIncluded what it says for a tool whose text is not copied here. Back string Missing string NotIncluded string } // English is the page's words in English. var English = Labels{ Contents: "On this page", Sections: map[string]string{ "typefaces": "Typefaces", "go": "Go libraries", "javascript": "JavaScript libraries", "rust": "Rust libraries", "swift": "Swift libraries", "icons": "Icons and artwork", "data": "Data", "tools": "Tools", "services": "Services", }, By: "By", Licence: "License", Version: "Version", Home: "Home page", Source: "Source", HomeAndSource: "Home page and source", Back: "Back to the colophon", Missing: "This package is published without a license file, so there is no text to show.", NotIncluded: "This is a tool the app is built or tested with, not part of what it ships, so its license text is not copied here. It is in the project's source.", } // Page is "estilo/colophon"'s data. Intro is the paragraphs under the // heading. LicenceBase is the path an entry's licence page is under: the // entry's ID is appended to it. type Page struct { Title string Intro []string Labels Labels LicenceBase string Colophon Colophon } // LicencePage is "estilo/colophon-licence"'s data: one entry's licence // texts, every package's own. Back is the Colophon's URL. type LicencePage struct { Title string Back string Labels Labels Entry Entry }