Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

render.gno

5.53 Kb · 185 lines
  1package releases
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"gno.land/p/moul/kit/ui/v0"
  8	"gno.land/p/moul/md/v0"
  9)
 10
 11// Every string an owner typed (description, notes, reason, URL) is escaped
 12// here, at the one place markdown is built, because a public registry lets
 13// anyone open a project. Names, versions and package paths are charset-checked
 14// at write time and escaped anyway.
 15
 16func block(h int64) string { return "block " + strconv.FormatInt(h, 10) }
 17
 18// Banner is the markdown a release prepends to its own Render, or "" when it
 19// is the Latest or not listed at all. It ends in a blank line, so
 20//
 21//	return reg.Banner(self) + body
 22//
 23// is the whole integration. stage is the link to the project's stage page, or
 24// "" to leave it out.
 25func (p *Project) Banner(pkgPath string, height int64, stage string) string {
 26	r, ok := p.ByPath(pkgPath)
 27	if !ok {
 28		return ""
 29	}
 30	st := p.StatusOf(r)
 31	if st == Latest {
 32		return ""
 33	}
 34
 35	var b strings.Builder
 36	b.WriteString("> " + st.Emoji() + " **This is " + ui.Inline(r.Version) + " of " + ui.Inline(p.name))
 37	switch st {
 38	case Supported:
 39		b.WriteString(", still supported.**")
 40	case Draft:
 41		b.WriteString(", a draft.**")
 42	case Prerelease:
 43		b.WriteString(", a prerelease.**")
 44	default:
 45		b.WriteString(", " + st.String() + ".**")
 46	}
 47	if r.Reason != "" {
 48		b.WriteString(" " + ui.Inline(r.Reason) + ".")
 49	}
 50
 51	switch pol := Policy(r, height); {
 52	case !pol.AllowsRead():
 53		b.WriteString(" It accepts nothing.")
 54	case st == Deprecated && r.Sunset > 0 && height >= r.Sunset:
 55		b.WriteString(" Read-only since " + block(r.Sunset) + ".")
 56	case st == Deprecated && r.Sunset > 0:
 57		b.WriteString(" Read-only from " + block(r.Sunset) + ".")
 58	}
 59
 60	if next, ok := p.next(r); ok {
 61		word := "The latest is "
 62		if r.Successor != "" {
 63			word = "Its successor is "
 64		}
 65		b.WriteString(" " + word + md.Link(next.Version, next.Link()) + ".")
 66	}
 67	if stage != "" {
 68		b.WriteString(" " + md.Link("All versions", stage) + ".")
 69	}
 70	b.WriteString("\n\n")
 71	return b.String()
 72}
 73
 74// next is where a banner sends the reader: the successor when one is set,
 75// otherwise the Latest, and nowhere when that is the release itself.
 76func (p *Project) next(r Release) (Release, bool) {
 77	if r.Successor != "" {
 78		if s, ok := p.ByVersion(r.Successor); ok {
 79			return s, true
 80		}
 81	}
 82	l, ok := p.Latest()
 83	if !ok || l.Version == r.Version {
 84		return Release{}, false
 85	}
 86	return l, true
 87}
 88
 89// RenderStage is the project's homepage: the latest release up front, every
 90// version with its status, then the changelog, newest first. link turns a
 91// version into the URL of its RenderRelease page, or is nil for none.
 92func (p *Project) RenderStage(height int64, link func(version string) string) string {
 93	var b strings.Builder
 94	b.WriteString(md.H1(ui.Inline(p.name)) + "\n")
 95	if p.description != "" {
 96		b.WriteString(ui.Inline(p.description) + "\n\n")
 97	}
 98
 99	if l, ok := p.Latest(); ok {
100		b.WriteString(Latest.Emoji() + " **Latest: " + md.Link(l.Version, l.Link()) + "** " + md.InlineCode(l.PkgPath) + "\n\n")
101	} else {
102		b.WriteString(ui.Empty("No supported release yet."))
103		b.WriteString("\n")
104	}
105
106	b.WriteString(md.H2("Versions") + "\n")
107	t := ui.NewTable("version", "status", "package", "listed", "writes", "reason")
108	for _, r := range p.Releases() {
109		pol := Policy(r, height)
110		writes := "yes"
111		if !pol.AllowsWrite() {
112			writes = "no"
113		} else if r.Status == Deprecated && r.Sunset > 0 {
114			writes = "until " + block(r.Sunset)
115		}
116		version := ui.Cell(r.Version)
117		if link != nil {
118			version = md.Link(r.Version, link(r.Version))
119		}
120		t.Row(version, p.StatusOf(r).Label(), md.Link(strings.TrimPrefix(r.PkgPath, pathDomain), r.Link()), block(r.ReleasedAt), writes, ui.Cell(r.Reason))
121	}
122	b.WriteString(t.OrEmpty("No releases listed."))
123	b.WriteString("\n")
124
125	b.WriteString(md.H2("Changelog") + "\n")
126	if p.Size() == 0 {
127		b.WriteString(ui.Empty("Nothing yet."))
128	}
129	for _, r := range p.Releases() {
130		b.WriteString(md.H3(ui.Inline(r.Version) + " " + p.StatusOf(r).Emoji()) + "\n")
131		b.WriteString(renderNotes(r.Notes))
132	}
133	return b.String()
134}
135
136// RenderRelease is one version's page: its status, what that means for
137// writes at this height, where it lives, and its notes.
138func (p *Project) RenderRelease(version string, height int64) string {
139	r, ok := p.ByVersion(version)
140	if !ok {
141		return ui.Empty("No such version.")
142	}
143	st := p.StatusOf(r)
144	pol := Policy(r, height)
145
146	var b strings.Builder
147	b.WriteString(md.H1(ui.Inline(p.name) + " " + ui.Inline(r.Version)) + "\n")
148	items := []string{
149		"status: " + st.Label(),
150		"package: " + md.Link(strings.TrimPrefix(r.PkgPath, pathDomain), r.Link()),
151		"listed at " + block(r.ReleasedAt),
152		"writes: " + pol.Level.String(),
153	}
154	if r.Reason != "" {
155		items = append(items, "reason: "+ui.Inline(r.Reason))
156	}
157	if r.Status == Deprecated && r.Sunset > 0 {
158		items = append(items, "read-only from "+block(r.Sunset))
159	}
160	if next, ok := p.next(r); ok {
161		items = append(items, "next: "+md.Link(next.Version, next.Link()))
162	}
163	b.WriteString(md.BulletList(items))
164	b.WriteString("\n")
165	b.WriteString(md.H2("Notes") + "\n")
166	b.WriteString(renderNotes(r.Notes))
167	return b.String()
168}
169
170// renderNotes turns a changelog entry into a bullet list, one item per
171// non-empty line, a leading "- " or "* " dropped since the list adds its own.
172func renderNotes(notes string) string {
173	var items []string
174	for _, line := range strings.Split(notes, "\n") {
175		line = strings.TrimSpace(line)
176		line = strings.TrimPrefix(strings.TrimPrefix(line, "- "), "* ")
177		if line != "" {
178			items = append(items, ui.Inline(line))
179		}
180	}
181	if len(items) == 0 {
182		return ui.Empty("No notes.") + "\n"
183	}
184	return md.BulletList(items) + "\n"
185}