chore: finished links and mvp a0dc11bc
Steve · 2026-11-14 16:26 6 file(s) · +95 −11
babbler.go +41 −4
11 11
12 12
type MarkovChain struct {
13 13
	chain map[string][]string
14 +
	words []string
14 15
}
15 16
16 17
func NewBabbler() *MarkovChain {
18 19
	if err != nil {
19 20
		log.Fatal("Could not read source text for babbler", err)
20 21
	}
21 -
	words := strings.Fields(string(text))
22 +
	fields := strings.Fields(string(text))
23 +
22 24
	chain := make(map[string][]string)
23 -
	for i := 0; i < len(words)-1; i++ {
24 -
		chain[words[i]] = append(chain[words[i]], words[i+1])
25 +
	for i := 0; i < len(fields)-1; i++ {
26 +
		chain[fields[i]] = append(chain[fields[i]], fields[i+1])
27 +
	}
28 +
29 +
	seen := make(map[string]struct{})
30 +
	words := make([]string, 0, len(chain))
31 +
	for _, field := range fields {
32 +
		slug := Slugify(field)
33 +
		if slug == "" {
34 +
			continue
35 +
		}
36 +
		if _, dup := seen[slug]; dup {
37 +
			continue
38 +
		}
39 +
		seen[slug] = struct{}{}
40 +
		words = append(words, slug)
25 41
	}
26 -
	return &MarkovChain{chain: chain}
42 +
43 +
	return &MarkovChain{chain: chain, words: words}
27 44
}
28 45
29 46
func (m *MarkovChain) Generate(maxWords int) string {
58 75
59 76
	return strings.Join(result, " ")
60 77
}
78 +
79 +
func (m *MarkovChain) Links(n int) []string {
80 +
	if len(m.words) == 0 {
81 +
		return nil
82 +
	}
83 +
	if n > len(m.words) {
84 +
		n = len(m.words)
85 +
	}
86 +
	seen := make(map[string]struct{}, n)
87 +
	links := make([]string, 0, n)
88 +
	for len(links) < n {
89 +
		word := m.words[rand.Intn(len(m.words))]
90 +
		if _, dup := seen[word]; dup {
91 +
			continue
92 +
		}
93 +
		seen[word] = struct{}{}
94 +
		links = append(links, word)
95 +
	}
96 +
	return links
97 +
}
go.mod +2 −0
1 1
module github.com/stevedylandev/gargantua
2 2
3 3
go 1.27.3
4 +
5 +
require golang.org/x/text v0.44.0
go.sum (added) +2 −0
1 +
golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo=
2 +
golang.org/x/text v0.44.0/go.mod h1:S4FR1waQ9LbjQTls97SyRjDaUUVkaM+cXwwuG/G+MwY=
main.go +12 −5
11 11
12 12
// Plan
13 13
// - [x] Serve robots.txt with noindex
14 -
// - [/] Serve root path html with babbler and links to new paths
15 -
// - [ ] Serve dynamic path that takes in path param to serve babbler and more links
14 +
// - [x] Serve root path html with babbler and links to new paths
15 +
// - [x] Serve dynamic path that takes in path param to serve babbler and more links
16 16
// - [ ] Slow down responses
17 17
18 18
var portEnv = os.Getenv("PORT")
27 27
type PageData struct {
28 28
	Title   string
29 29
	Content string
30 -
	Items   []string
30 +
	Links   []string
31 31
}
32 32
33 33
func robotsHandler(writer http.ResponseWriter, req *http.Request) {
37 37
38 38
func indexHandler(writer http.ResponseWriter, req *http.Request) {
39 39
	babbler := NewBabbler()
40 -
	title := babbler.Generate(2)
40 +
	var title string
41 +
	if req.PathValue("slug") != "" {
42 +
		title = req.PathValue("slug")
43 +
	} else {
44 +
		title = babbler.Generate(2)
45 +
	}
41 46
	content := babbler.Generate(500)
47 +
	links := babbler.Links(5)
42 48
	data := PageData{
43 49
		Title:   title,
44 50
		Content: content,
45 -
		Items:   []string{"First link", "second link", "third link"},
51 +
		Links:   links,
46 52
	}
47 53
	if err := tmpl.ExecuteTemplate(writer, "index.html", data); err != nil {
48 54
		http.Error(writer, "Internal server error", http.StatusInternalServerError)
64 70
65 71
	http.HandleFunc("/robots.txt", robotsHandler)
66 72
	http.HandleFunc("/", indexHandler)
73 +
	http.HandleFunc("/{slug}", indexHandler)
67 74
68 75
	port := fmt.Sprintf(":%s", portEnv)
69 76
	log.Println("Listening on", port)
templates/index.html +6 −2
6 6
		<title>{{.Title}}</title>
7 7
	</head>
8 8
	<body>
9 -
    <h1>{{.Title}}</title>
9 +
    <h1>{{.Title}}</h1>
10 10
    <p>{{.Content}}</p>
11 -
		{{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}}
11 +
    <ol>
12 +
    {{range .Links}}
13 +
      <a href='/{{ . }}'>{{ . }}</a>
14 +
    {{end}}
15 +
    </ol>
12 16
	</body>
13 17
</html>
utils.go (added) +32 −0
1 +
package main
2 +
3 +
import (
4 +
	"regexp"
5 +
	"strings"
6 +
	"unicode"
7 +
8 +
	norm "golang.org/x/text/unicode/norm"
9 +
)
10 +
11 +
var nonAlnum = regexp.MustCompile(`[^a-z0-9]+`)
12 +
13 +
var translit = strings.NewReplacer(
14 +
	"’", "", "'", "",
15 +
	"“", "", "”", "", "‘", "",
16 +
	"—", " ", "–", " ", "‑", " ",
17 +
	"æ", "ae", "ø", "o", "ð", "d", "þ", "th", "ß", "ss", "œ", "oe",
18 +
)
19 +
20 +
func Slugify(s string) string {
21 +
	s = strings.ToLower(s)
22 +
	s = translit.Replace(s)
23 +
	s = norm.NFD.String(s)
24 +
	s = strings.Map(func(r rune) rune {
25 +
		if unicode.Is(unicode.Mn, r) {
26 +
			return -1
27 +
		}
28 +
		return r
29 +
	}, s)
30 +
	s = nonAlnum.ReplaceAllString(s, "-")
31 +
	return strings.Trim(s, "-")
32 +
}