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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

badge.gno

3.67 Kb · 115 lines
  1// Package badge builds small flat SVG badges ("label | message") and turns
  2// them into data: URIs and Markdown images that gnoweb renders inline.
  3//
  4// It is pure: no state, no realm imports. Text is XML-escaped and capped,
  5// and colors come from a fixed palette, so any input gives a well-formed,
  6// bounded SVG.
  7package badge
  8
  9import (
 10	"encoding/base64"
 11	"strconv"
 12	"strings"
 13)
 14
 15// Colors a badge message can use.
 16const (
 17	Green  = "green"
 18	Orange = "orange"
 19	Red    = "red"
 20	Grey   = "grey"
 21)
 22
 23var palette = map[string]string{
 24	Green:  "#2e7d32",
 25	Orange: "#c25e00",
 26	Red:    "#c62828",
 27	Grey:   "#616161",
 28}
 29
 30const (
 31	labelColor = "#333333"
 32	maxText    = 40  // runes, per side
 33	maxAlt     = 120 // runes of alt text
 34	charWidth  = 7   // px per character at 11px Verdana, rounded up
 35	padding    = 8   // px either side of each text
 36	height     = 20
 37)
 38
 39// SVG returns a badge with label on the left and message on the right, the
 40// message on color (one of the palette names; anything else is Grey).
 41func SVG(label, message, color string) string {
 42	label, message = clip(label), clip(message)
 43	fill, ok := palette[color]
 44	if !ok {
 45		fill = palette[Grey]
 46	}
 47	lw := textWidth(label) + 2*padding
 48	mw := textWidth(message) + 2*padding
 49	w := lw + mw
 50	title := escape(label + ": " + message)
 51
 52	var b strings.Builder
 53	b.WriteString(`<svg xmlns="http://www.w3.org/2000/svg" width="` + itoa(w) + `" height="` + itoa(height) + `" role="img" aria-label="` + title + `">`)
 54	b.WriteString(`<title>` + title + `</title>`)
 55	b.WriteString(`<rect width="` + itoa(lw) + `" height="` + itoa(height) + `" rx="3" fill="` + labelColor + `"/>`)
 56	b.WriteString(`<rect x="` + itoa(lw) + `" width="` + itoa(mw) + `" height="` + itoa(height) + `" rx="3" fill="` + fill + `"/>`)
 57	// Square off the inner corners where the halves meet.
 58	b.WriteString(`<rect x="` + itoa(lw-3) + `" width="6" height="` + itoa(height) + `" fill="` + fill + `"/>`)
 59	b.WriteString(`<rect x="` + itoa(lw-3) + `" width="3" height="` + itoa(height) + `" fill="` + labelColor + `"/>`)
 60	b.WriteString(`<g fill="#ffffff" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11" text-anchor="middle">`)
 61	b.WriteString(`<text x="` + itoa(lw/2) + `" y="14">` + escape(label) + `</text>`)
 62	b.WriteString(`<text x="` + itoa(lw+mw/2) + `" y="14">` + escape(message) + `</text>`)
 63	b.WriteString(`</g></svg>`)
 64	return b.String()
 65}
 66
 67// DataURI returns svg as a base64 data:image/svg+xml URI.
 68func DataURI(svg string) string {
 69	return "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte(svg))
 70}
 71
 72// Markdown returns a Markdown image of svg with alt text alt. If link is
 73// not empty, the image links there. link must already be a safe URL.
 74func Markdown(alt, svg, link string) string {
 75	img := "![" + escapeAlt(clipN(alt, maxAlt)) + "](" + DataURI(svg) + ")"
 76	if link == "" {
 77		return img
 78	}
 79	return "[" + img + "](" + link + ")"
 80}
 81
 82// clip keeps the first maxText runes of s, with control characters dropped.
 83func clip(s string) string { return clipN(s, maxText) }
 84
 85// clipN keeps the first max runes of s, with control characters dropped.
 86func clipN(s string, max int) string {
 87	var b strings.Builder
 88	n := 0
 89	for _, r := range s {
 90		if r < 0x20 || r == 0x7f {
 91			continue
 92		}
 93		if n == max {
 94			break
 95		}
 96		b.WriteRune(r)
 97		n++
 98	}
 99	return b.String()
100}
101
102func textWidth(s string) int {
103	return len([]rune(s)) * charWidth
104}
105
106func escape(s string) string {
107	return strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;", `"`, "&quot;", "'", "&#39;").Replace(s)
108}
109
110// escapeAlt keeps alt text from closing the Markdown image early.
111func escapeAlt(s string) string {
112	return strings.NewReplacer(`\`, `\\`, "[", `\[`, "]", `\]`).Replace(s)
113}
114
115func itoa(n int) string { return strconv.Itoa(n) }