Skip to content

Commit

Permalink
Merge pull request #502 from trheyi/main
Browse files Browse the repository at this point in the history
[add] sui page Compile
  • Loading branch information
trheyi authored Nov 22, 2023
2 parents 5e00399 + ef05990 commit 3d51488
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
31 changes: 30 additions & 1 deletion sui/core/compile.go
Original file line number Diff line number Diff line change
@@ -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(`<script name="data" type="json">` +
fmt.Sprintf("\n%s\n", page.Codes.DATA.Code) +
"</script>\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) {
Expand Down
20 changes: 20 additions & 0 deletions sui/core/compile_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
18 changes: 18 additions & 0 deletions sui/core/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ func testPage(t *testing.T) *Page {
page := &Page{
Name: "test",
Route: "test",
Document: []byte(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="/assets/css/tailwind.css" />
<link rel="stylesheet" href="/assets/css/app.css" />
</head>
<body>
<div id="app">
{{ __page }}
</div>
</body>
</html>
`),
Codes: SourceCodes{
HTML: Source{
File: "test.html",
Expand Down
7 changes: 4 additions & 3 deletions sui/core/sui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions sui/storages/local/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3d51488

Please sign in to comment.