1package home
2
3import (
4 "std"
5 "strings"
6
7 "gno.land/p/n2p5/chonk"
8
9 "gno.land/r/leon/hof"
10 "gno.land/r/n2p5/config"
11)
12
13var (
14 active = chonk.New()
15 preview = chonk.New()
16)
17
18func init() {
19 hof.Register()
20}
21
22// Add appends a string to the preview Chonk.
23func Add(chunk string) {
24 assertAdmin()
25 preview.Add(chunk)
26}
27
28// Flush clears the preview Chonk.
29func Flush() {
30 assertAdmin()
31 preview.Flush()
32}
33
34// Promote promotes the preview Chonk to the active Chonk
35// and creates a new preview Chonk.
36func Promote() {
37 assertAdmin()
38 active = preview
39 preview = chonk.New()
40}
41
42// Render returns the contents of the scanner for the active or preview Chonk
43// based on the path provided.
44func Render(path string) string {
45 var result string
46 scanner := getScanner(path)
47 for scanner.Scan() {
48 result += scanner.Text()
49 }
50 return result
51}
52
53// assertAdmin panics if the caller is not an admin as defined in the config realm.
54func assertAdmin() {
55 caller := std.PrevRealm().Addr()
56 if !config.IsAdmin(caller) {
57 panic("forbidden: must be admin")
58 }
59}
60
61// getScanner returns the scanner for the active or preview Chonk based
62// on the path provided.
63func getScanner(path string) *chonk.Scanner {
64 if isPreview(path) {
65 return preview.Scanner()
66 }
67 return active.Scanner()
68}
69
70// isPreview returns true if the path prefix is "preview".
71func isPreview(path string) bool {
72 return strings.HasPrefix(path, "preview")
73}
home.gno
1.37 Kb ยท 73 lines