piglatindemo.gno
2.83 Kb · 66 lines
1// Package piglatindemo is a small gnoweb demo of the Pig Latin translator
2// provided by the [p/moul/x/daily/piglatin](/p/moul/x/daily/piglatin/v0)
3// library: it renders the rules, a gallery of worked examples, and an
4// interactive sentence translator.
5//
6// It contains no translation logic of its own — everything comes from
7// `piglatin.Translate` and `piglatin.TranslateToken`.
8package piglatindemo
9
10import (
11 "strings"
12
13 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
14 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/piglatin/v0"
15)
16
17// Render is the gnoweb entrypoint (Markdown). Root explains the rules; any
18// other path is treated as a sentence to translate, e.g. Render("/Hello world").
19func Render(path string) string {
20 var b strings.Builder
21
22 sentence := strings.TrimPrefix(path, "/")
23 sentence = strings.TrimSpace(sentence)
24
25 if sentence == "" {
26 b.WriteString("# Pig Latin\n\n")
27 b.WriteString("Demo of the [`p/moul/x/daily/piglatin`](/p/moul/x/daily/piglatin/v0) ")
28 b.WriteString("library — a tiny port of the classic **Pig Latin** translator ")
29 b.WriteString("(the go-to beginner exercise). Pure `strings`/`unicode`, fully deterministic.\n\n")
30 b.WriteString("## Rules\n\n")
31 b.WriteString("1. Word starts with a **vowel** → append `way`. `apple` → `appleway`\n")
32 b.WriteString("2. Word starts with **consonant(s)** → move the leading consonant cluster to the end, then add `ay`. `string` → `ingstray`\n")
33 b.WriteString("3. `y` is a consonant only as the first letter (`yellow` → `ellowyay`); otherwise a vowel (`myth` → `ythmay`).\n")
34 b.WriteString("4. **Capitalization** is preserved: `Hello` → `Ellohay`, `NASA` → `ASANAY`.\n")
35 b.WriteString("5. **Punctuation** glued to a word stays put: `Hello,` → `Ellohay,`.\n\n")
36 b.WriteString("## Try it\n\n")
37 b.WriteString("Append a sentence to the path — spaces are fine:\n\n")
38 for _, ex := range []string{"hello world", "The quick brown fox", "I love Gno!"} {
39 b.WriteString("- [`/")
40 b.WriteString(ex)
41 b.WriteString("`](/r/moul/x/daily/piglatindemo/v1:/")
42 b.WriteString(ex)
43 b.WriteString(") → `")
44 b.WriteString(piglatin.Translate(ex))
45 b.WriteString("`\n")
46 }
47 b.WriteString("\n## Examples\n\n")
48 b.WriteString("| Input | Pig Latin |\n|---|---|\n")
49 for _, w := range []string{"pig", "banana", "smile", "eat", "yellow", "myth", "Hello,", "GNO"} {
50 b.WriteString("| `")
51 b.WriteString(w)
52 b.WriteString("` | `")
53 b.WriteString(piglatin.TranslateToken(w))
54 b.WriteString("` |\n")
55 }
56 return b.String()
57 }
58
59 b.WriteString("# Pig Latin\n\n")
60 b.WriteString("**Original:**\n\n> ")
61 b.WriteString(ui.Inline(sentence))
62 b.WriteString("\n\n**Pig Latin:**\n\n> ")
63 b.WriteString(ui.Inline(piglatin.Translate(sentence)))
64 b.WriteString("\n\n---\n\n[← rules & examples](/r/moul/x/daily/piglatindemo/v1)\n")
65 return b.String()
66}