// assemblies.go - hand-built sets of pages drawn from several documents, // loaded from data/assemblies.json. package main import ( "encoding/json" "fmt" "log" "net/http" "net/url" "os" "strconv" "strings" ) type assemblyRef struct { Doc string `json:"doc"` Version string `json:"version,omitempty"` Pages string `json:"pages"` } type assemblySlideSpec struct { assemblyRef Note string `json:"note,omitempty"` Alt []assemblyRef `json:"alt,omitempty"` } type assemblySpec struct { ID string `json:"id"` Title string `json:"title"` Note string `json:"note,omitempty"` Slides []assemblySlideSpec `json:"slides"` } type AssemblyCopy struct { Image string Size imageSize DocID string DocTitle string Version string Page int Source string // the outlets that published this copy Path string // the document or version page it came from } type AssemblySlide struct { AssemblyCopy Pos int Note string Alts []AssemblyCopy } type Assembly struct { ID string Title string Note string Slides []AssemblySlide DocList []string // documents the slides are shown from, in slide order Copies int // copies the slides are shown from: a document or one of its versions } // parsePageList expands "1-3,7" into page numbers, keeping the order given. func parsePageList(s string) ([]int, error) { var out []int for _, part := range strings.Split(s, ",") { part = strings.TrimSpace(part) first, last, isRange := strings.Cut(part, "-") if !isRange { last = first } a, err := strconv.Atoi(strings.TrimSpace(first)) if err != nil { return nil, fmt.Errorf("%q is not a page or a page range", part) } b, err := strconv.Atoi(strings.TrimSpace(last)) if err != nil { return nil, fmt.Errorf("%q is not a page or a page range", part) } if a < 1 || b < a { return nil, fmt.Errorf("%q is not a page or a page range", part) } for n := a; n <= b; n++ { out = append(out, n) } } return out, nil } // copySource names the outlets that published a copy, oldest entry first as // the metadata lists them. Partners that published no documents are left out. func copySource(d *Document, slug string) string { var names []string seen := map[string]bool{} for _, p := range d.Publication { key := valueKey(p.Publisher) if p.ReportingOnly || p.Version != slug || key == "" || seen[key] { continue } seen[key] = true names = append(names, p.Publisher) } return strings.Join(names, ", ") } func (co *Corpus) resolveAssemblyRef(label string, r assemblyRef) ([]*AssemblyCopy, bool) { pages, err := parsePageList(r.Pages) if err != nil { warnf("%s: %s: pages: %v - skipped", label, r.Doc, err) return nil, false } out := make([]*AssemblyCopy, len(pages)) d := co.docIndex[r.Doc] if d == nil { warnf("%s: no document %q - skipped", label, r.Doc) return out, true } images, sizes, base, path := d.Images, d.ImageSizes, imageBase(d.ID, ""), docPath(d.ID) if r.Version != "" { v := d.VersionFor(r.Version) if v == nil { warnf("%s: %s has no version %q - skipped", label, r.Doc, r.Version) return out, true } images, sizes, base, path = v.Images, v.ImageSizes, imageBase(d.ID, v.Slug), versionPath(d.ID, v.Slug) } byNumber := make(map[int]int, len(images)) for i, name := range images { if n, err := pageNumber(name); err == nil { byNumber[n] = i } } source := copySource(d, r.Version) for i, n := range pages { k, ok := byNumber[n] if !ok { warnf("%s: %s %s has no page image %d - skipped", label, r.Doc, r.Version, n) continue } out[i] = &AssemblyCopy{ Image: base + url.PathEscape(images[k]), Size: sizes[k], DocID: d.ID, DocTitle: d.Title, Version: r.Version, Page: n, Source: source, Path: path, } } return out, true } func (co *Corpus) buildAssembly(spec assemblySpec) *Assembly { label := "assemblies.json: " + spec.ID a := &Assembly{ID: spec.ID, Title: strings.TrimSpace(spec.Title), Note: strings.TrimSpace(spec.Note)} seenImage := map[string]bool{} seenDoc := map[string]bool{} seenCopy := map[[2]string]bool{} for _, s := range spec.Slides { copies, ok := co.resolveAssemblyRef(label, s.assemblyRef) if !ok { continue } alts := make([][]*AssemblyCopy, 0, len(s.Alt)) for _, ref := range s.Alt { c, ok := co.resolveAssemblyRef(label, ref) if !ok { continue } if len(c) != len(copies) { warnf("%s: alt %s lists %d pages for a run of %d - skipped", label, ref.Doc, len(c), len(copies)) continue } alts = append(alts, c) } for i, c := range copies { if c == nil { continue } if seenImage[c.Image] { warnf("%s: %s page %d is listed twice", label, c.DocID, c.Page) } seenImage[c.Image] = true if !seenDoc[c.DocID] { seenDoc[c.DocID] = true a.DocList = append(a.DocList, c.DocID) } slide := AssemblySlide{AssemblyCopy: *c, Pos: len(a.Slides) + 1, Note: s.Note} seenCopy[[2]string{c.DocID, c.Version}] = true for _, alt := range alts { if alt[i] != nil { slide.Alts = append(slide.Alts, *alt[i]) } } a.Slides = append(a.Slides, slide) } } a.Copies = len(seenCopy) return a } func (co *Corpus) loadAssemblies() { co.assemblies = nil co.assemblyIndex = map[string]*Assembly{} co.assembliesByDoc = map[string][]*Assembly{} p := projectPath("assemblies.json") b, err := os.ReadFile(p) if err != nil { log.Printf("Note: %s not found - no assemblies.", p) return } var specs []assemblySpec if err := json.Unmarshal(b, &specs); err != nil { warnf("%s is malformed: %v - assemblies disabled.", p, err) return } for _, spec := range specs { spec.ID = strings.TrimSpace(spec.ID) if spec.ID == "" || !docIDSafe(spec.ID) { warnf("assemblies.json: id %q is empty or unsafe in a URL - skipped", spec.ID) continue } if co.assemblyIndex[spec.ID] != nil { warnf("assemblies.json: id %q appears twice - keeping the first", spec.ID) continue } a := co.buildAssembly(spec) if len(a.Slides) == 0 { warnf("assemblies.json: %s has no slides that can be shown - skipped", spec.ID) continue } co.assemblies = append(co.assemblies, a) co.assemblyIndex[a.ID] = a linked := map[string]bool{} link := func(id string) { if !linked[id] { linked[id] = true co.assembliesByDoc[id] = append(co.assembliesByDoc[id], a) } } for _, s := range a.Slides { link(s.DocID) for _, alt := range s.Alts { link(alt.DocID) } } } log.Printf("Assemblies loaded: %d", len(co.assemblies)) } // assemblyPath is an assembly's page; assemblies live under /explore. const assembliesPath = "/explore/assemblies" func assemblyPath(id string) string { return assembliesPath + "/" + url.PathEscape(id) } func (co *Corpus) handleAssemblies(w http.ResponseWriter, r *http.Request) { prefs := readPrefs(r) render(w, r, &assembliesPage{ Base: pageWith(prefs), Assemblies: co.assemblies, TextOnly: prefs.TextOnly, }) } func (co *Corpus) handleAssemblyView(w http.ResponseWriter, r *http.Request) { a := co.assemblyIndex[r.PathValue("id")] if a == nil { notFound(w, r) return } prefs := readPrefs(r) render(w, r, &assemblyPage{ Base: pageWith(prefs), Assembly: a, TextOnly: prefs.TextOnly, }) }