MCPcopy Create free account
hub / github.com/cogentcore/core / ExampleHandler

Function ExampleHandler

pages/examples.go:33–57  ·  view source on GitHub ↗

ExampleHandler is the htmlcore handler for HTML elements that handles examples.

(ctx *htmlcore.Context)

Source from the content-addressed store, hash-verified

31// ExampleHandler is the htmlcore handler for <pages-example> HTML elements
32// that handles examples.
33func ExampleHandler(ctx *htmlcore.Context) bool {
34 // the node we actually care about is our first child, the <pre> element
35 ctx.Node = ctx.Node.FirstChild
36
37 core.NewText(ctx.Parent()).SetText(htmlcore.ExtractText(ctx)).Styler(func(s *styles.Style) {
38 s.Text.WhiteSpace = styles.WhiteSpacePreWrap
39 s.Background = colors.Scheme.SurfaceContainer
40 s.Border.Radius = styles.BorderRadiusMedium
41 })
42
43 url := ctx.PageURL
44 if url == "" {
45 url = "index"
46 }
47 id := url + "-" + strconv.Itoa(NumExamples[ctx.PageURL])
48 NumExamples[ctx.PageURL]++
49 fn := Examples[id]
50 if fn == nil {
51 slog.Error("programmer error: pages example not found in pages.Examples (you probably need to run go generate again)", "id", id)
52 } else {
53 fn(ctx.Parent())
54 }
55
56 return true
57}

Callers

nothing calls this directly

Calls 6

NewTextFunction · 0.92
ExtractTextFunction · 0.92
StylerMethod · 0.80
ErrorMethod · 0.65
SetTextMethod · 0.45
ParentMethod · 0.45

Tested by

no test coverage detected