showcase
- v0.2.4
- 37 commits
- 1 branch
- 6 tags
- updated
- CI 2/2
extra.go
4.4 KB · 168 lines
1package build23import (4 "context"5 "html/template"6 "net/url"7 "strings"89 "daum.dev/showcase/internal/gitea"10 "daum.dev/showcase/internal/gitx"11 "daum.dev/showcase/internal/render"12)1314type ReleaseView struct {15 gitea.Release16 Notes template.HTML17 Assets []Crumb // Name and Href; files are served by the release proxy route18}1920type PullView struct {21 gitea.PullRequest22 Body template.HTML23 Thread []CommentView24 Hidden int // older comments left out (maxThread)25}2627type IssueView struct {28 gitea.Issue29 Body template.HTML30 Thread []CommentView31 Hidden int32}3334type CommentView struct {35 gitea.Comment36 Body template.HTML37}3839type PackageView struct {40 gitea.Package41}4243type ExtraPage struct {44 Repo *repo45 Tab string46 Releases []ReleaseView47 Pulls []PullView48 Issues []IssueView49 Open int // issues, by state50 Closed int51 Packages []PackageView // other package types, from the API52 Images []ImageView // container images, from the registry53 Kind string // "branches" or "tags" on the refs pages54 Refs []gitx.Ref55}5657// Limits on text anyone with a Gitea account can write: a body longer58// than maxUserText is cut, and a thread shows its newest maxThread59// comments. They keep a spammer from turning one page into megabytes.60const (61 maxUserText = 32 << 1062 maxThread = 10063)6465// userMarkdown renders Markdown from issues, pulls and comments. It goes66// through the same allowlist as READMEs; the cap keeps pages small.67func userMarkdown(s, base string) (template.HTML, error) {68 if len(s) > maxUserText {69 s = strings.ToValidUTF8(s[:maxUserText], "") + "\n\n*(cut: too long to show here)*"70 }71 return render.Markdown([]byte(s), base, "")72}7374// thread renders the newest maxThread comments on issue or pull n and75// says how many older ones it left out.76func thread(r *repo, n int, base string) ([]CommentView, int, error) {77 cs := r.Comments[n]78 hidden := max(0, len(cs)-maxThread)79 var out []CommentView80 for _, c := range cs[hidden:] {81 body, err := userMarkdown(c.Body, base)82 if err != nil {83 return nil, 0, err84 }85 out = append(out, CommentView{c, body})86 }87 return out, hidden, nil88}8990// renderExtras writes the releases, issues, pulls and packages pages from API and91// registry data.92func (b *Builder) renderExtras(ctx context.Context, rd *render.Renderer, dir string, r *repo) error {93 base := "/" + r.Name94 // Downloads go through the proxy at the root of site_url. Under a95 // base_path the pages may be served elsewhere, so link there directly.96 dl := base97 if b.Cfg.BasePath != "" {98 dl = b.Cfg.SiteURL + base99 }100 rel := ExtraPage{Repo: r, Tab: "releases"}101 for _, x := range r.Releases {102 v := ReleaseView{Release: x}103 var err error104 if v.Notes, err = render.Markdown([]byte(x.Body), base, ""); err != nil {105 return err106 }107 for _, a := range x.Assets {108 v.Assets = append(v.Assets, Crumb{a.Name, dl + "/releases/download/" + url.PathEscape(x.TagName) + "/" + url.PathEscape(a.Name)})109 }110 rel.Releases = append(rel.Releases, v)111 }112113 pulls := ExtraPage{Repo: r, Tab: "pulls"}114 for _, x := range r.Pulls {115 body, err := userMarkdown(x.Body, base)116 if err != nil {117 return err118 }119 th, hidden, err := thread(r, x.Number, base)120 if err != nil {121 return err122 }123 pulls.Pulls = append(pulls.Pulls, PullView{x, body, th, hidden})124 }125126 issues := ExtraPage{Repo: r, Tab: "issues"}127 for _, x := range r.Issues {128 body, err := userMarkdown(x.Body, base)129 if err != nil {130 return err131 }132 th, hidden, err := thread(r, x.Number, base)133 if err != nil {134 return err135 }136 issues.Issues = append(issues.Issues, IssueView{x, body, th, hidden})137 if x.State == "open" {138 issues.Open++139 } else {140 issues.Closed++141 }142 }143144 pkgs := ExtraPage{Repo: r, Tab: "packages", Images: r.Images}145 for _, x := range r.Packages {146 if x.Type == "container" {147 continue // images come from the registry, and only allowlisted ones148 }149 pkgs.Packages = append(pkgs.Packages, PackageView{Package: x})150 }151152 for _, p := range []ExtraPage{rel, issues, pulls, pkgs} {153 page := render.Page{Title: p.Tab + " · " + r.Name, D: &p}154 if err := rd.Write(dir, r.Name+"/"+p.Tab+"/index.html", p.Tab, page); err != nil {155 return err156 }157 }158 for _, p := range []ExtraPage{159 {Repo: r, Tab: "code", Kind: "branches", Refs: r.Branches},160 {Repo: r, Tab: "code", Kind: "tags", Refs: r.Tags},161 } {162 page := render.Page{Title: p.Kind + " · " + r.Name, D: &p}163 if err := rd.Write(dir, r.Name+"/"+p.Kind+"/index.html", "refs", page); err != nil {164 return err165 }166 }167 return nil168}