Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

render.gno

2.62 Kb · 92 lines
 1package toolbox
 2
 3import (
 4	"net/url"
 5	"strings"
 6
 7	"gno.land/p/moul/md/v0"
 8	"gno.land/p/moul/realmpath/v0"
 9	tb "gno.land/p/moul/toolbox/v0"
10)
11
12// Render routes gnoweb paths:
13//
14//	/                 the directory
15//	/?tag=identity    the directory, filtered to one tag
16//	/<slug>           one tool
17func Render(path string) string {
18	req := realmpath.Parse(path)
19	parts := req.PathParts()
20
21	if len(parts) == 0 || parts[0] == "" {
22		return renderIndex(req.Query.Get("tag"))
23	}
24	if len(parts) > 1 {
25		return renderUnknown(parts[0] + "/" + parts[1])
26	}
27	t, ok := catalog.Get(parts[0])
28	if !ok {
29		return renderUnknown(parts[0])
30	}
31	return renderTool(t)
32}
33
34func renderIndex(tag string) string {
35	var b strings.Builder
36	b.WriteString(md.H1("Toolbox"))
37	b.WriteString("\nSmall utilities on gno.land. Each one is its own realm, composing contracts that are already on chain and showing the answer as a page instead of as a query you have to compose.\n")
38
39	list := catalog.List()
40	if tag != "" {
41		list = catalog.ByTag(tag)
42		b.WriteString("\nFiltered to " + md.InlineCode(tag) + ". " +
43			md.Link("Show all", hubURL) + "\n")
44	}
45	b.WriteString("\n" + tb.Index(list))
46
47	if tag == "" {
48		if links := tagLinks(catalog, hubURL); links != "" {
49			b.WriteString("\nTags: " + links + "\n")
50		}
51	}
52
53	b.WriteString("\n" + md.H2("What a tool is"))
54	b.WriteString("\n" + md.BulletList([]string{
55		"A realm at " + md.InlineCode("gno.land/r/moul/toolbox/<slug>") + ", reachable on its own.",
56		"Read-only unless it says otherwise: a page you open, not a transaction you sign.",
57		"Composed from contracts already on chain, so the toolbox adds the view and not the data.",
58	}))
59	return b.String()
60}
61
62// tagLinks renders the tag filter row, or "" when nothing is tagged.
63//
64// QueryEscape, not plain concatenation: a tag is an arbitrary string on Tool,
65// and "a&b" would otherwise become a second query parameter while "a#b" would
66// become a fragment, in both cases linking to a filter nobody asked for.
67func tagLinks(c *tb.Catalog, base string) string {
68	tags := c.Tags()
69	if len(tags) == 0 {
70		return ""
71	}
72	links := make([]string, 0, len(tags))
73	for _, name := range tags {
74		links = append(links, md.Link(name, base+"?tag="+url.QueryEscape(name)))
75	}
76	return strings.Join(links, " · ")
77}
78
79func renderTool(t tb.Tool) string {
80	var b strings.Builder
81	b.WriteString(tb.Nav(hubURL, "Toolbox"))
82	b.WriteString("\n" + tb.Card(t))
83	return b.String()
84}
85
86func renderUnknown(slug string) string {
87	var b strings.Builder
88	b.WriteString(md.H1("Not found"))
89	b.WriteString("\nNo tool called " + md.InlineCode(slug) + ".\n")
90	b.WriteString("\n" + md.Link("Back to the toolbox", hubURL) + "\n")
91	return b.String()
92}