render.gno
2.00 Kb · 63 lines
1package validator_scores
2
3import (
4 "strconv"
5 "strings"
6)
7
8// formatHundredths renders the canonical decimal "8743" as "87.43".
9func formatHundredths(s string) string {
10 for len(s) < 3 {
11 s = "0" + s
12 }
13 return s[:len(s)-2] + "." + s[len(s)-2:]
14}
15
16// mdEscape backslash-escapes every ASCII punctuation character, so a
17// moniker renders as plain text and never as an author-chosen link, image
18// or HTML. gnoweb still turns a g1 address that follows a space into a link
19// to its /u/ page, with the address itself as the link text.
20func mdEscape(s string) string {
21 var b strings.Builder
22 for i := 0; i < len(s); i++ {
23 c := s[i]
24 if c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~' {
25 b.WriteByte('\\')
26 }
27 b.WriteByte(c)
28 }
29 return b.String()
30}
31
32// Render shows the latest sealed edition as a Markdown table. The path is
33// ignored.
34func Render(_ string) string {
35 var b strings.Builder
36 b.WriteString("# Validator selection scores\n\n")
37 e := latestSealed()
38 if e == nil {
39 b.WriteString("No edition has been published yet.\n")
40 return b.String()
41 }
42 b.WriteString("Edition " + strconv.Itoa(e.number) + ", computed at " + e.computedAt +
43 ", scorecard SHA-256 `" + e.scorecardSHA256 + "`. Scores have two decimals; a sealed edition never changes.\n\n")
44 b.WriteString("| Rank | Moniker | Operator | Total |")
45 for _, a := range e.arenas {
46 b.WriteString(" " + mdEscape(a) + " |")
47 }
48 b.WriteString(" GitHub |\n| ---: | --- | --- | ---: |")
49 for range e.arenas {
50 b.WriteString(" ---: |")
51 }
52 b.WriteString(" ---: |\n")
53 for _, c := range sortedCandidates(e) {
54 b.WriteString("| " + c.rankText + " | " + mdEscape(c.moniker) + " | " + c.operator +
55 " | " + formatHundredths(c.total) + " |")
56 for _, s := range strings.Split(c.arenaScores, ",") {
57 b.WriteString(" " + formatHundredths(s) + " |")
58 }
59 b.WriteString(" " + formatHundredths(c.github) + " |\n")
60 }
61 b.WriteString("\nMachine-readable: `LatestJSON()`, `EditionJSON(n)`, `CandidateJSON(operator)`.\n")
62 return b.String()
63}