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

codespan_test.gno

package romannumdemo

import (
	"strings"
	"testing"

	"gno.land/p/nt/uassert/v0"
)

// The not-found branch echoes the visitor's URL path into a code span. Built by
// hand, a backtick in the path closed the span early and everything after it
// rendered as markdown on a gno.land page. md.InlineCode sizes the fence past
// any backtick run in the content, so the payload must come back wrapped in a
// DOUBLE fence, with the backtick intact inside it. Asserting on the payload
// itself rather than on any double fence in the page: an earlier version of
// this test passed on an unrelated span elsewhere in the output.
func TestRenderPathCannotEscapeItsCodeSpan(t *testing.T) {
	out := Render("/a`b")
	fenced := strings.Contains(out, "``a`b``") || strings.Contains(out, "``/a`b``")
	uassert.True(t, fenced,
		"the fence did not outscan the caller's backtick, so the span closes early")
}

gnomod.toml

module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/romannumdemo/v0"
gno = "0.9"

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 755201
  max_deposit = "5000000ugnot"

render_example_test.gno

package romannumdemo

// ExampleRender pins the realm's Render output as a gno testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # Roman numeral converter
	//
	// A tiny demo of the [`p/moul/x/daily/romannum`](/p/moul/x/daily/romannum/v0) library — the classic `ToRoman` / `FromRoman` kata. Pure and deterministic.
	//
	// ## Examples
	//
	// | decimal | roman |
	// |---:|:---|
	// | [1](/r/moul/x/daily/romannumdemo/v0:1) | I |
	// | [4](/r/moul/x/daily/romannumdemo/v0:4) | IV |
	// | [9](/r/moul/x/daily/romannumdemo/v0:9) | IX |
	// | [14](/r/moul/x/daily/romannumdemo/v0:14) | XIV |
	// | [40](/r/moul/x/daily/romannumdemo/v0:40) | XL |
	// | [49](/r/moul/x/daily/romannumdemo/v0:49) | XLIX |
	// | [90](/r/moul/x/daily/romannumdemo/v0:90) | XC |
	// | [400](/r/moul/x/daily/romannumdemo/v0:400) | CD |
	// | [900](/r/moul/x/daily/romannumdemo/v0:900) | CM |
	// | [1994](/r/moul/x/daily/romannumdemo/v0:1994) | MCMXCIV |
	// | [2024](/r/moul/x/daily/romannumdemo/v0:2024) | MMXXIV |
	// | [3888](/r/moul/x/daily/romannumdemo/v0:3888) | MMMDCCCLXXXVIII |
	// | [3999](/r/moul/x/daily/romannumdemo/v0:3999) | MMMCMXCIX |
	//
	// ## Try it
	//
	// - `:/<n>` — integer → Roman, e.g. `:/2024` → MMXXIV
	// - `:/r/<roman>` — Roman → integer, e.g. `:/r/MMXXIV` → 2024
}

romannumdemo.gno

// Package romannumdemo is a small gnoweb demo of the Roman-numeral converter
// provided by the [p/moul/x/daily/romannum](/p/moul/x/daily/romannum/v0)
// library: it renders a table of worked examples and interactive
// integer→Roman and Roman→integer conversions.
//
// It contains no conversion logic of its own — everything comes from
// romannum.ToRoman and romannum.FromRoman.
package romannumdemo

import (
	"strconv"
	"strings"

	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/romannum/v0"
)

// examples are the classic milestones shown by the root Render.
var examples = []int{1, 4, 9, 14, 40, 49, 90, 400, 900, 1994, 2024, 3888, 3999}

// Render drives gnoweb:
//   - ""        or "/"          → a table of example conversions
//   - "/<n>"                    → integer → Roman
//   - "/r/<roman>"              → Roman → integer
func Render(path string) string {
	arg := strings.Trim(strings.TrimSpace(path), "/")

	switch {
	case arg == "":
		return renderTable()
	case strings.HasPrefix(strings.ToLower(arg), "r/"):
		return renderFromRoman(arg[2:])
	default:
		return renderToRoman(arg)
	}
}

func renderToRoman(arg string) string {
	n, err := strconv.Atoi(strings.TrimSpace(arg))
	if err != nil {
		return "# Roman numeral converter\n\nCould not parse " + md.InlineCode(arg) +
			"` as an integer.\n\n" + usage()
	}
	if n < 1 || n > 3999 {
		return "# Roman numeral converter\n\n`" + strconv.Itoa(n) +
			"` is out of range — Roman numerals here cover **1..3999**.\n\n" + usage()
	}
	var b strings.Builder
	b.WriteString("# " + strconv.Itoa(n) + " → " + romannum.ToRoman(n) + "\n\n")
	b.WriteString("| decimal | roman |\n|---:|:---|\n")
	b.WriteString("| " + strconv.Itoa(n) + " | **" + romannum.ToRoman(n) + "** |\n\n")
	if n > 1 {
		b.WriteString("[« " + strconv.Itoa(n-1) + "](/r/moul/x/daily/romannumdemo/v0:" +
			strconv.Itoa(n-1) + ") · ")
	}
	if n < 3999 {
		b.WriteString("[" + strconv.Itoa(n+1) + " »](/r/moul/x/daily/romannumdemo/v0:" +
			strconv.Itoa(n+1) + ")")
	}
	b.WriteString("\n\n" + usage())
	return b.String()
}

func renderFromRoman(arg string) string {
	arg = strings.TrimSpace(arg)
	n, ok := tryFromRoman(arg)
	if !ok {
		return "# Roman numeral converter\n\n" + md.InlineCode(arg) +
			" is not a valid Roman numeral (1..3999).\n\n" + usage()
	}
	up := strings.ToUpper(arg)
	var b strings.Builder
	b.WriteString("# " + up + " → " + strconv.Itoa(n) + "\n\n")
	b.WriteString("| roman | decimal |\n|:---|---:|\n")
	b.WriteString("| **" + up + "** | " + strconv.Itoa(n) + " |\n\n")
	b.WriteString(usage())
	return b.String()
}

// tryFromRoman wraps the panic-on-invalid romannum.FromRoman in a panic-free
// (value, ok) shape, so the demo can report a friendly error for bad input.
func tryFromRoman(s string) (n int, ok bool) {
	defer func() {
		if recover() != nil {
			n, ok = 0, false
		}
	}()
	return romannum.FromRoman(s), true
}

func renderTable() string {
	var b strings.Builder
	b.WriteString("# Roman numeral converter\n\n")
	b.WriteString("A tiny demo of the [`p/moul/x/daily/romannum`](/p/moul/x/daily/romannum/v0) ")
	b.WriteString("library — the classic `ToRoman` / `FromRoman` kata. ")
	b.WriteString("Pure and deterministic.\n\n")
	b.WriteString("## Examples\n\n")
	b.WriteString("| decimal | roman |\n|---:|:---|\n")
	for _, n := range examples {
		b.WriteString("| [" + strconv.Itoa(n) + "](/r/moul/x/daily/romannumdemo/v0:" +
			strconv.Itoa(n) + ") | " + romannum.ToRoman(n) + " |\n")
	}
	b.WriteString("\n" + usage())
	return b.String()
}

func usage() string {
	return "## Try it\n\n" +
		"- `:/<n>` — integer → Roman, e.g. `:/2024` → " + romannum.ToRoman(2024) + "\n" +
		"- `:/r/<roman>` — Roman → integer, e.g. `:/r/MMXXIV` → 2024\n"
}