22// Use of this source code is governed by a BSD-style
33// license that can be found in the LICENSE file.
44
5- //go:build generatehtml
6-
75package content
86
97import (
10- "fmt"
11- "os"
128 "strings"
139
1410 "cogentcore.org/core/base/errors"
@@ -18,51 +14,50 @@ import (
1814 "cogentcore.org/core/tree"
1915)
2016
21- // This file is activated by the core tool to pre-render Cogent Core apps
22- // as HTML that can be used as a preview and for SEO purposes.
23-
2417func init () {
25- // We override the OnChildAdded set in core/generatehtml.go
26- core .ExternalParent .AsWidget ().SetOnChildAdded (func (n tree.Node ) {
27- var ct * Content
28- n .AsTree ().WalkDown (func (n tree.Node ) bool {
29- if ct != nil {
18+ core .GenerateHTML = generateHTML
19+ }
20+
21+ // GenerateHTML is called by the core tool to pre-render Cogent Core apps
22+ // as HTML that can be used as a preview and for SEO purposes.
23+ func generateHTML (w core.Widget ) string {
24+ var ct * Content
25+ w .AsTree ().WalkDown (func (n tree.Node ) bool {
26+ if ct != nil {
27+ return tree .Break
28+ }
29+ if c , ok := n .(* Content ); ok {
30+ ct = c
31+ return tree .Break
32+ }
33+ return tree .Continue
34+ })
35+ if ct == nil {
36+ return core .GenerateHTMLCore (w ) // basic fallback
37+ }
38+ prps := []* bcontent.PreRenderPage {}
39+ ct .UpdateTree () // need initial update first
40+ for _ , pg := range ct .pages {
41+ ct .Open (pg .URL )
42+ prp := & bcontent.PreRenderPage {
43+ Page : * pg ,
44+ HTML : core .GenerateHTMLCore (ct ),
45+ }
46+ // The first non-emphasized paragraph is used as the description
47+ // (<em> typically indicates a note or caption, not an introduction).
48+ ct .WalkDown (func (n tree.Node ) bool {
49+ if prp .Description != "" {
3050 return tree .Break
3151 }
32- if c , ok := n .(* Content ); ok {
33- ct = c
34- return tree .Break
52+ if tx , ok := n .(* core.Text ); ok {
53+ if tx .Property ("tag" ) == "p" && ! strings .HasPrefix (tx .Text , "<em>" ) {
54+ prp .Description = tx .Text
55+ return tree .Break
56+ }
3557 }
3658 return tree .Continue
3759 })
38- if ct == nil {
39- fmt .Println (core .GenerateHTML (n .(core.Widget ))) // basic fallback
40- os .Exit (0 )
41- }
42- prps := []* bcontent.PreRenderPage {}
43- ct .UpdateTree () // need initial update first
44- for _ , pg := range ct .pages {
45- ct .Open (pg .URL )
46- prp := & bcontent.PreRenderPage {
47- Page : * pg ,
48- HTML : core .GenerateHTML (ct ),
49- }
50- // The first non-emphasized paragraph is used as the description
51- // (<em> typically indicates a note or caption, not an introduction).
52- ct .WalkDown (func (n tree.Node ) bool {
53- if prp .Description != "" {
54- return tree .Break
55- }
56- if tx , ok := n .(* core.Text ); ok {
57- if tx .Property ("tag" ) == "p" && ! strings .HasPrefix (tx .Text , "<em>" ) {
58- prp .Description = tx .Text
59- return tree .Break
60- }
61- }
62- return tree .Continue
63- })
64- prps = append (prps , prp )
65- }
66- fmt .Println (string (errors .Log1 (jsonx .WriteBytes (prps ))))
67- })
60+ prps = append (prps , prp )
61+ }
62+ return string (errors .Log1 (jsonx .WriteBytes (prps )))
6863}
0 commit comments