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

contract.gno

1.36 Kb · 46 lines
 1package render
 2
 3import (
 4	"chain/runtime/unsafe"
 5	"strings"
 6
 7	"gno.land/p/moul/kit/ui/v0"
 8	reg "gno.land/r/moul/releases/v0"
 9)
10
11// self is this version's own package path, computed once at init, which is
12// the one moment the current realm is this one for certain. It is how this
13// version asks gno.land/r/moul/releases/v0 whether it is still the latest.
14var self = unsafe.CurrentRealm().PkgPath()
15
16func Hello() string {
17	return "Hello World!"
18}
19
20func greetings(name string) string { // private
21	return "Welcome, " + ui.Inline(name) + "!"
22}
23
24// Render prepends the registry's banner, "" while this is the latest version
25// of the "render" project and a notice pointing at the newer one afterwards.
26// This realm has no write path, so the banner is the whole integration; a
27// realm with writes adds reg.AssertWritable("render", self) to each of them.
28func Render(path string) string {
29	return reg.Banner("render", self) + route(path)
30}
31
32func route(path string) string {
33	switch {
34	case path == "hello":
35		return Hello()
36	case strings.HasPrefix(path, "greetings/"):
37		name := strings.Split(path, "/")[1] // take string after slash
38		name = strings.Title(name)          // capitalize first letter
39		return greetings(name)
40	case path == "":
41		out := "- [hello](/r/moul_basics_004:hello)\n"
42		out += "- [greetings/Manfred](/r/moul_basics_004:greetings/Manfred)\n"
43		return out
44	}
45	return "404"
46}