package site import ( "context" "encoding/json" "path" "sort" "strconv" ) type repositoryNavigation struct { DefaultBranch string `json:"defaultBranch"` Branches []string `json:"branches"` } type fileNavigation struct { Files []fileNavigationEntry `json:"files"` } type fileNavigationEntry struct { Path string `json:"path"` URL string `json:"url"` } func branchStylePage(branch string, commitCount int) Page { return Page{ Route: path.Join(".dumbforge/branches", branchRouteSegment(branch)+".css"), Body: []byte(`[data-commit-count]::before { content: "` + strconv.Itoa(commitCount) + `"; }` + "\n"), ContentType: "text/css; charset=utf-8", Cache: siteCache, } } func (g *Generator) SetBranches(branches []string, defaultBranch string) { seen := map[string]struct{}{} g.Branches = g.Branches[:0] for _, branch := range branches { if branch == "" { continue } if _, exists := seen[branch]; exists { continue } seen[branch] = struct{}{} g.Branches = append(g.Branches, branch) } sort.Strings(g.Branches) g.DefaultBranch = defaultBranch if g.DefaultBranch == "" && len(g.Branches) > 0 { g.DefaultBranch = g.Branches[0] } } func (g *Generator) ensureBranch(branch string, isHead bool) { if isHead || g.DefaultBranch == "" { g.DefaultBranch = branch } for _, existing := range g.Branches { if existing == branch { return } } g.Branches = append(g.Branches, branch) sort.Strings(g.Branches) } func (g *Generator) filesPage(ctx context.Context, branch, oid string) (Page, error) { entries, err := g.listTreeRecursive(ctx, oid) if err != nil { return Page{}, err } g.decorateSubmodules(ctx, oid, "", entries) manifest := fileNavigation{} for _, entry := range entries { if entry.Type == "tree" { continue } entryURL := routeURL(g.BaseURL, path.Join("blob", branchRouteSegment(branch), entry.Name)) if entry.SubmoduleURL != "" { entryURL = entry.SubmoduleURL } manifest.Files = append(manifest.Files, fileNavigationEntry{Path: entry.Name, URL: entryURL}) } sort.Slice(manifest.Files, func(i, j int) bool { return manifest.Files[i].Path < manifest.Files[j].Path }) body, err := json.Marshal(manifest) if err != nil { return Page{}, err } return Page{ Route: path.Join(".dumbforge/files", branchRouteSegment(branch)+".json"), Body: body, ContentType: "application/json; charset=utf-8", Cache: siteCache, }, nil } func (g *Generator) RepositoryNavigationBuild(deletedBranches []string) (Build, error) { manifest := repositoryNavigation{ DefaultBranch: g.DefaultBranch, Branches: append([]string(nil), g.Branches...), } body, err := json.Marshal(manifest) if err != nil { return Build{}, err } build := Build{Pages: []Page{{ Route: ".dumbforge/repository.json", Body: body, ContentType: "application/json; charset=utf-8", Cache: siteCache, }}} for _, branch := range deletedBranches { build.Deletes = append(build.Deletes, path.Join(".dumbforge/files", branchRouteSegment(branch)+".json"), path.Join(".dumbforge/branches", branchRouteSegment(branch)+".css"), ) } return build, nil }