From ef05990480b579d73668cb7b6643f4fa2946b747 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 22 Nov 2023 11:37:08 +0800 Subject: [PATCH] [add] sui page Compile --- sui/core/compile.go | 31 ++++++++++++++++++++++++++++++- sui/core/compile_test.go | 20 ++++++++++++++++++++ sui/core/page_test.go | 18 ++++++++++++++++++ sui/core/sui.go | 7 ++++--- sui/storages/local/build.go | 15 ++------------- 5 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 sui/core/compile_test.go diff --git a/sui/core/compile.go b/sui/core/compile.go index 46457075b..a6e9a9499 100644 --- a/sui/core/compile.go +++ b/sui/core/compile.go @@ -1,14 +1,43 @@ package core import ( + "fmt" "regexp" "github.com/evanw/esbuild/pkg/api" "github.com/yaoapp/gou/runtime/transform" + "github.com/yaoapp/kun/log" ) // Compile the page -func (page *Page) Compile() {} +func (page *Page) Compile(option *BuildOption) (string, error) { + + doc, warnings, err := page.Build(option) + if err != nil { + return "", err + } + + if warnings != nil && len(warnings) > 0 { + for _, warning := range warnings { + log.Warn("Compile page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, warning) + } + } + + if page.Codes.DATA.Code != "" { + doc.Find("body").AppendHtml(`\n", + ) + } + + html, err := doc.Html() + if err != nil { + return "", err + } + + // @todo: Minify the html + return html, nil +} // CompileJS compile the javascript func (page *Page) CompileJS(source []byte, minify bool) ([]byte, error) { diff --git a/sui/core/compile_test.go b/sui/core/compile_test.go new file mode 100644 index 000000000..d87e3d14f --- /dev/null +++ b/sui/core/compile_test.go @@ -0,0 +1,20 @@ +package core + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCompile(t *testing.T) { + prepare(t) + defer clean() + + page := testPage(t) + html, err := page.Compile(&BuildOption{}) + if err != nil { + t.Fatalf("Compile error: %v", err) + } + + assert.Contains(t, html, "input.data") +} diff --git a/sui/core/page_test.go b/sui/core/page_test.go index f032334b9..45f18ac0d 100644 --- a/sui/core/page_test.go +++ b/sui/core/page_test.go @@ -45,6 +45,24 @@ func testPage(t *testing.T) *Page { page := &Page{ Name: "test", Route: "test", + Document: []byte(` + + + + + + + Document + + + + +
+ {{ __page }} +
+ + + `), Codes: SourceCodes{ HTML: Source{ File: "test.html", diff --git a/sui/core/sui.go b/sui/core/sui.go index 982818e23..10cfa8f93 100644 --- a/sui/core/sui.go +++ b/sui/core/sui.go @@ -9,6 +9,8 @@ import ( "github.com/yaoapp/kun/maps" ) +var varRe = regexp.MustCompile(`{{\s*([^{}]+)\s*}}`) + // Setting the struct for the DSL func (sui *DSL) Setting() (*Setting, error) { return &Setting{ @@ -42,13 +44,12 @@ func (sui *DSL) PublicRoot() (string, error) { var root = sui.Public.Root dot := maps.Of(vars).Dot() - re := regexp.MustCompile(`{{\s*([^{}]+)\s*}}`) - output := re.ReplaceAllStringFunc(root, func(matched string) string { + output := varRe.ReplaceAllStringFunc(root, func(matched string) string { varName := strings.TrimSpace(matched[2 : len(matched)-2]) if value, ok := dot[varName]; ok { return fmt.Sprint(value) } - return matched + return "__undefined" }) sui.publicRoot = output diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go index 94027f520..6b36f0882 100644 --- a/sui/storages/local/build.go +++ b/sui/storages/local/build.go @@ -84,24 +84,13 @@ func (page *Page) Build(option *core.BuildOption) error { option.AssetRoot = filepath.Join(page.tmpl.local.DSL.Public.Root, "assets") } - doc, _, err := page.Page.Build(option) - if err != nil { - return err - } - - html, err := doc.Html() + html, err := page.Page.Compile(option) if err != nil { return err } // Save the html - err = page.writeHTML([]byte(html)) - if err != nil { - return err - } - - // Save the data - return page.writeData() + return page.writeHTML([]byte(html)) } func (page *Page) publicFile() string {