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