ExampleHandler is the htmlcore handler for HTML elements that handles examples.
(ctx *htmlcore.Context)
| 31 | // ExampleHandler is the htmlcore handler for <pages-example> HTML elements |
| 32 | // that handles examples. |
| 33 | func 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 | } |