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.

render.gno

7.86 Kb · 240 lines
  1package erratabank
  2
  3import (
  4	"strconv"
  5
  6	"gno.land/p/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/errata/v0"
  7	"gno.land/p/nt/markdown/sanitize/v0"
  8	"gno.land/p/nt/mux/v0"
  9	"gno.land/p/nt/ufmt/v0"
 10)
 11
 12const pageSize = 20
 13
 14var router = mux.NewRouter()
 15
 16func init() {
 17	router.HandleFunc("", renderHome)
 18	router.HandleFunc("book/{id}", renderBook)
 19	router.HandleFunc("book/{id}/pending", renderPending)
 20	router.HandleFunc("book/{id}/all", renderAll)
 21	router.HandleFunc("erratum/{id}", renderErratum)
 22	router.HandleFunc("finder/{addr}", renderFinder)
 23	router.SetNotFoundHandler(func(w *mux.ResponseWriter, _ *mux.Request) {
 24		notFound(w, "Page")
 25	})
 26}
 27
 28// Render is the public Markdown entry point for gnoweb.
 29func Render(path string) string {
 30	return router.Render(path)
 31}
 32
 33func finderURL(addr address) string {
 34	return realmURL + "finder/" + addr.String()
 35}
 36
 37func bookRoute(id int) string {
 38	return "book/" + strconv.Itoa(id)
 39}
 40
 41func erratumURL(id int) string {
 42	return realmURL + "erratum/" + strconv.Itoa(id)
 43}
 44
 45func bookURL(id int) string {
 46	return realmURL + bookRoute(id)
 47}
 48
 49func pageOffset(req *mux.Request, total int) (int, bool) {
 50	value := req.Query.Get("page")
 51	if value == "" {
 52		return 0, true
 53	}
 54	page, err := strconv.Atoi(value)
 55	if err != nil || page < 1 || (page > 1 && page-1 > (total-1)/pageSize) {
 56		return 0, false
 57	}
 58	return (page - 1) * pageSize, true
 59}
 60
 61func pagination(w *mux.ResponseWriter, route string, offset, total int) {
 62	if total <= pageSize {
 63		return
 64	}
 65	page := offset/pageSize + 1
 66	last := (total-1)/pageSize + 1
 67	w.Write(ufmt.Sprintf("\nPage %d of %d", page, last))
 68	if page > 1 {
 69		w.Write(ufmt.Sprintf(" · [Previous](%s%s?page=%d)", realmURL, route, page-1))
 70	}
 71	if page < last {
 72		w.Write(ufmt.Sprintf(" · [Next](%s%s?page=%d)", realmURL, route, page+1))
 73	}
 74	w.Write("\n")
 75}
 76
 77func notFound(w *mux.ResponseWriter, item string) {
 78	w.Write("# " + item + " not found\n\n[Erratabank home](" + realmURL + ")\n")
 79}
 80
 81func renderHome(w *mux.ResponseWriter, req *mux.Request) {
 82	offset, ok := pageOffset(req, books.Size())
 83	if !ok {
 84		notFound(w, "Page")
 85		return
 86	}
 87	w.Write("# Erratabank\n\n")
 88	w.Write("Erratabank is a public errata page for books, courses, and technical docs.\n\n")
 89	w.Write("Each accepted correction credits its finder. Authors can escrow a reward for accepted reports.\n\n")
 90	w.Write("Use the Actions tab to register a book, submit a correction, or review reports for a book you own.\n\n")
 91	w.Write("## Books\n\n")
 92	if books.Size() == 0 {
 93		w.Write("No books registered yet.\n")
 94	} else {
 95		w.Write("| Book | Owner | Reward per erratum | Escrow | Accepted |\n")
 96		w.Write("| --- | --- | ---: | ---: | ---: |\n")
 97		for _, book := range ListBooks(offset, pageSize) {
 98			w.Write(ufmt.Sprintf("| [%s](%s) | %s | %s | %s | %d |\n",
 99				sanitize.TableCell(book.Title), bookURL(book.ID), errata.ShortAddr(book.Owner.String()),
100				errata.FormatUgnot(book.RewardPerErratum), errata.FormatUgnot(book.Escrow),
101				requireBook(book.ID).accepted.Size()))
102		}
103		pagination(w, "", offset, books.Size())
104	}
105	w.Write("\nStarted 23 Nov 2026 for Gnome Tavern.\n")
106}
107
108func routeBook(w *mux.ResponseWriter, req *mux.Request) *bookRecord {
109	if id, err := strconv.Atoi(req.GetVar("id")); err == nil {
110		if record := bookByID(id); record != nil {
111			return record
112		}
113	}
114	notFound(w, "Book")
115	return nil
116}
117
118func renderBook(w *mux.ResponseWriter, req *mux.Request) {
119	record := routeBook(w, req)
120	if record == nil {
121		return
122	}
123	offset, ok := pageOffset(req, record.accepted.Size())
124	if !ok {
125		notFound(w, "Page")
126		return
127	}
128	book := record.book
129	state := "Closed"
130	if book.Open {
131		state = "Open"
132	}
133	w.Write(ufmt.Sprintf("# %s\n\n- Edition: %s\n- Owner: %s\n- Submissions: %s\n- Reward per erratum: %s\n- Escrow left: %s\n",
134		sanitize.InlineText(book.Title), sanitize.InlineText(book.Edition), sanitize.InlineCode(book.Owner.String()),
135		state, errata.FormatUgnot(book.RewardPerErratum), errata.FormatUgnot(book.Escrow)))
136	if url := sanitize.URL(book.Link); url != "" {
137		w.Write("- [Read the book](" + url + ")\n")
138	}
139	w.Write("\n")
140	w.Write(errata.RenderSection("Accepted errata", AcceptedEntriesPage(book.ID, offset, pageSize)))
141	pagination(w, bookRoute(book.ID), offset, record.accepted.Size())
142	w.Write(ufmt.Sprintf("\n[Pending (%d)](%s/pending) · [All reports (%d)](%s/all) · Rejected: %d · Duplicates: %d · [All books](%s)\n",
143		record.pending.Size(), bookURL(book.ID), record.all.Size(), bookURL(book.ID),
144		record.rejected.Size(), record.duplicate.Size(), realmURL))
145}
146
147func renderPending(w *mux.ResponseWriter, req *mux.Request) {
148	renderReportList(w, req, Pending, "Pending reports", "/pending")
149}
150
151func renderAll(w *mux.ResponseWriter, req *mux.Request) {
152	renderReportList(w, req, "", "All reports", "/all")
153}
154
155func renderReportList(w *mux.ResponseWriter, req *mux.Request, status, heading, suffix string) {
156	record := routeBook(w, req)
157	if record == nil {
158		return
159	}
160	tree := record.reports(status)
161	offset, ok := pageOffset(req, tree.Size())
162	if !ok {
163		notFound(w, "Page")
164		return
165	}
166	book := record.book
167	w.Write(ufmt.Sprintf("# %s\n\n[Back to %s](%s)\n\n",
168		heading, sanitize.InlineText(book.Title), bookURL(book.ID)))
169	if tree.Size() == 0 {
170		w.Write("No reports here yet.\n")
171		return
172	}
173	w.Write("| Report | Location | Status | Finder | Height found |\n| --- | --- | --- | --- | ---: |\n")
174	for _, report := range listReports(tree, offset, pageSize) {
175		w.Write(ufmt.Sprintf("| [#%d](%s) | %s | %s | [%s](%s) | %d |\n",
176			report.ID, erratumURL(report.ID), sanitize.TableCell(report.Location), report.Status,
177			errata.ShortAddr(report.Finder.String()), finderURL(report.Finder), report.SubmittedAt))
178	}
179	pagination(w, bookRoute(book.ID)+suffix, offset, tree.Size())
180}
181
182func renderErratum(w *mux.ResponseWriter, req *mux.Request) {
183	id, err := strconv.Atoi(req.GetVar("id"))
184	report, ok := GetErratum(id)
185	if err != nil || !ok {
186		notFound(w, "Erratum")
187		return
188	}
189	book, _ := GetBook(report.BookID)
190	w.Write(ufmt.Sprintf("# Erratum #%d\n\n[Back to %s](%s)\n\n",
191		id, sanitize.InlineText(book.Title), bookURL(book.ID)))
192	w.Write(ufmt.Sprintf("- Location: %s\n- Finder: [%s](%s)\n- Status: %s\n- Paid: %s\n\n## Correction\n\n%s\n\n",
193		sanitize.InlineText(report.Location), report.Finder.String(), finderURL(report.Finder),
194		report.Status, errata.FormatUgnot(report.Paid), sanitize.InlineText(report.Text)))
195	w.Write(ufmt.Sprintf("## Status history\n\n- Height %d — Pending (submitted)\n", report.SubmittedAt))
196	if report.Status != Pending {
197		w.Write(ufmt.Sprintf("- Height %d — %s\n", report.ReviewedAt, report.Status))
198	}
199	if report.Note != "" {
200		w.Write("- Review note: " + sanitize.InlineText(report.Note) + "\n")
201	}
202	if report.DuplicateOf != 0 {
203		w.Write(ufmt.Sprintf("- Earlier report: [#%d](%s)\n", report.DuplicateOf, erratumURL(report.DuplicateOf)))
204	}
205}
206
207func renderFinder(w *mux.ResponseWriter, req *mux.Request) {
208	raw := req.GetVar("addr")
209	cleaned := sanitize.BechString(raw, "g")
210	if cleaned == "" || cleaned != raw || !address(cleaned).IsValid() {
211		notFound(w, "Finder")
212		return
213	}
214	finder, _ := finders.Get(cleaned).(*finderRecord)
215	count, paid := FinderStats(address(cleaned))
216	offset, ok := pageOffset(req, count)
217	if !ok {
218		notFound(w, "Page")
219		return
220	}
221	w.Write(ufmt.Sprintf("# Finder account\n\n%s\n\n- Accepted finds: %d\n- Total earned: %s\n\n",
222		sanitize.InlineCode(cleaned), count, errata.FormatUgnot(paid)))
223	if finder == nil {
224		w.Write("No accepted finds yet.\n")
225		return
226	}
227	w.Write("| Book | Find | Height found | Paid |\n| --- | --- | ---: | ---: |\n")
228	makeFinderRows(w, listReports(&finder.ids, offset, pageSize))
229	pagination(w, "finder/"+cleaned, offset, count)
230}
231
232func makeFinderRows(w *mux.ResponseWriter, reports []Erratum) {
233	for _, report := range reports {
234		book, _ := GetBook(report.BookID)
235		w.Write(ufmt.Sprintf("| [%s](%s) | [%s](%s) | %d | %s |\n",
236			sanitize.TableCell(book.Title), bookURL(book.ID),
237			sanitize.TableCell(report.Location), erratumURL(report.ID), report.SubmittedAt,
238			errata.FormatUgnot(report.Paid)))
239	}
240}