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.

Playground

erratademo.gno

// Package erratademo embeds the accepted corrections for a documentation page.
package erratademo

import (
	"chain"
	"strconv"
	"strings"

	"gno.land/p/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/errata/v0"
	"gno.land/p/nt/mux/v0"
	"gno.land/r/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/erratabank/v0"
)

const pageSize = 20

var (
	owner    address
	bookID   int
	realmURL string
	router   = mux.NewRouter()
)

func init(cur realm) {
	owner = cur.Previous().Address()
	realmURL = strings.TrimPrefix(cur.PkgPath(), "gno.land") + ":"
	router.HandleFunc("", renderGuide)
}

// SetBook connects this page to an existing book. The deployer can do this once.
func SetBook(cur realm, id int) {
	if !cur.IsCurrent() {
		panic("invalid realm context")
	}
	if cur.Previous().Address() != owner {
		panic("demo deployer only")
	}
	if bookID != 0 {
		panic("demo book already set")
	}
	if _, ok := erratabank.GetBook(id); !ok {
		panic("book not found")
	}
	bookID = id
	chain.Emit("DemoBookSet", "book", strconv.Itoa(id))
}

// BookID returns the configured book, or zero if the page is unlinked.
func BookID() int {
	return bookID
}

func Render(path string) string {
	return router.Render(path)
}

func renderGuide(w *mux.ResponseWriter, req *mux.Request) {
	w.Write("# Erratabank Quickstart\n\n")
	w.Write("To report an error, give the page or section, explain what is wrong, and suggest a correction.\n\n")
	w.Write("The maintainer reviews submissions on Erratabank. Accepted corrections appear here automatically.\n\n")
	w.Write("## Section 1. Formatting examples\n\nOpen a fenced code block with two backticks on their own line.\n\n")
	w.Write("## Section 2. Notes\n\nInclude the expected output when a correction changes an example.\n\n")
	if bookID == 0 {
		w.Write("The maintainer has yet to link this guide to a book.\n\n")
		w.Write(errata.RenderSection("Errata", nil))
		return
	}
	page := 1
	if value := req.Query.Get("page"); value != "" {
		var err error
		page, err = strconv.Atoi(value)
		if err != nil || page < 1 || page-1 > int(^uint(0)>>1)/pageSize {
			w.Write("Errata page not found.\n")
			return
		}
	}

	entries := erratabank.AcceptedEntriesPage(bookID, (page-1)*pageSize, pageSize+1)
	if page > 1 && len(entries) == 0 {
		w.Write("Errata page not found.\n")
		return
	}
	more := len(entries) > pageSize
	if more {
		entries = entries[:pageSize]
	}
	w.Write(errata.RenderSection("Errata", entries))
	if page > 1 {
		w.Write("\n[Previous](" + realmURL + "?page=" + strconv.Itoa(page-1) + ")")
	}
	if more {
		w.Write("\n[Next](" + realmURL + "?page=" + strconv.Itoa(page+1) + ")")
	}
}

erratademo_test.gno

package erratademo

import (
	"strings"
	"testing"

	"gno.land/r/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/erratabank/v0"
)

func TestEmbedAcceptedErrata(cur realm, t *testing.T) {
	if BookID() != 0 || !strings.Contains(Render(""), "No errata yet.") {
		t.Fatal("an unlinked guide should render its empty state")
	}
	// The unit-test loader has no deployment transaction or creator.
	owner = address("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0")
	reader := address("g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c")
	testing.SetRealm(testing.NewUserRealm(owner))
	id := erratabank.CreateBook(cross(cur), "Quickstart", "First", "https://example.org/guide", 0)
	testing.SetRealm(testing.NewUserRealm(reader))
	reportID := erratabank.Submit(cross(cur), id, "Section 1", "Use the new endpoint.")
	if got := revive(func() { SetBook(cross(cur), id) }); got != "demo deployer only" {
		t.Fatalf("unexpected unauthorized result: %v", got)
	}

	testing.SetRealm(testing.NewUserRealm(owner))
	if got := revive(func() { SetBook(cross(cur), 0) }); got != "book not found" {
		t.Fatalf("unexpected missing book result: %v", got)
	}
	SetBook(cross(cur), id)
	if got := revive(func() { SetBook(cross(cur), id) }); got != "demo book already set" {
		t.Fatalf("unexpected second link result: %v", got)
	}
	if BookID() != id || strings.Contains(Render(""), "Use the new endpoint") {
		t.Fatal("pending reports should stay out of the embedded section")
	}

	erratabank.Accept(cross(cur), reportID, "Confirmed")
	rendered := Render("")
	if !strings.Contains(rendered, "## Errata") || !strings.Contains(rendered, "Use the new endpoint") {
		t.Fatalf("accepted correction missing from guide: %s", rendered)
	}
	if got := Render("?page=2"); !strings.Contains(got, "Errata page not found.") {
		t.Fatalf("unexpected out-of-range page: %s", got)
	}
}

gnomod.toml

module = "gno.land/r/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/erratademo/v0"
gno = "0.9"

[addpkg]
  creator = "g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807"
  height = 1384848
  max_deposit = "10000000ugnot"