Skip to content

Commit

Permalink
[add] page build interface
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 10, 2023
1 parent b6fb8e4 commit 9a088ce
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 88 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v1.2.0 h1:La19f8d7WIlm4ogzNHB0JGqs5AUDAZ2UfCY4sJXcJdM=
Expand All @@ -260,6 +261,8 @@ github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVH
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-plugin v1.4.4 h1:NVdrSdFRt3SkZtNckJ6tog7gbpRrcbOjQi/rgF7JYWQ=
github.com/hashicorp/go-plugin v1.4.4/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHGUjr2IrTF67s=
github.com/hashicorp/go-plugin v1.5.1 h1:oGm7cWBaYIp3lJpx1RUEfLWophprE2EV/KUeqBYo+6k=
Expand Down
96 changes: 96 additions & 0 deletions sui/core/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package core

import (
"fmt"
"strings"

"github.com/PuerkitoBio/goquery"
)

// Build is the struct for the public
func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error) {

warnings := []string{}
html, err := page.BuildHTML(option.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}

// Add Style & Script & Warning
doc, err := NewDocument([]byte(html))
if err != nil {
warnings = append(warnings, err.Error())
}

// Add Style
style, err := page.BuildStyle(option.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}
doc.Selection.Find("head").AppendHtml(style)

// Add Script
script, err := page.BuildScript(option.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}
doc.Selection.Find("body").AppendHtml(script)
return doc, warnings, nil
}

// BuildHTML build the html
func (page *Page) BuildHTML(assetRoot string) (string, error) {

html := string(page.Document)
if page.Codes.HTML.Code != "" {
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
}

code := strings.ReplaceAll(html, "@assets", assetRoot)
res, err := page.CompileHTML([]byte(code), false)
if err != nil {
return "", err
}

return string(res), nil
}

// BuildStyle build the style
func (page *Page) BuildStyle(assetRoot string) (string, error) {
if page.Codes.CSS.Code == "" {
return "", nil
}

code := strings.ReplaceAll(page.Codes.CSS.Code, "@assets", assetRoot)
res, err := page.CompileCSS([]byte(code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<style>\n%s\n</style>\n", res), nil
}

// BuildScript build the script
func (page *Page) BuildScript(assetRoot string) (string, error) {

if page.Codes.JS.Code == "" && page.Codes.TS.Code == "" {
return "", nil
}

if page.Codes.TS.Code != "" {
res, err := page.CompileTS([]byte(page.Codes.TS.Code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}

code := strings.ReplaceAll(page.Codes.JS.Code, "@assets", assetRoot)
res, err := page.CompileJS([]byte(code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}
59 changes: 0 additions & 59 deletions sui/core/compile.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package core

import (
"fmt"
"regexp"
"strings"

"github.com/evanw/esbuild/pkg/api"
"github.com/yaoapp/gou/runtime/transform"
Expand Down Expand Up @@ -53,60 +51,3 @@ func (page *Page) CompileCSS(source []byte, minify bool) ([]byte, error) {
func (page *Page) CompileHTML(source []byte, minify bool) ([]byte, error) {
return source, nil
}

// BuildHTML build the html
func (page *Page) BuildHTML(assetRoot string) (string, error) {

html := string(page.Document)
if page.Codes.HTML.Code != "" {
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
}

code := strings.ReplaceAll(html, "@assets", assetRoot)
res, err := page.CompileHTML([]byte(code), false)
if err != nil {
return "", err
}

return string(res), nil
}

// BuildStyle build the style
func (page *Page) BuildStyle(assetRoot string) (string, error) {
if page.Codes.CSS.Code == "" {
return "", nil
}

code := strings.ReplaceAll(page.Codes.CSS.Code, "@assets", assetRoot)
res, err := page.CompileCSS([]byte(code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<style>\n%s\n</style>\n", res), nil
}

// BuildScript build the script
func (page *Page) BuildScript(assetRoot string) (string, error) {

if page.Codes.JS.Code == "" && page.Codes.TS.Code == "" {
return "", nil
}

if page.Codes.TS.Code != "" {
res, err := page.CompileTS([]byte(page.Codes.TS.Code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}

code := strings.ReplaceAll(page.Codes.JS.Code, "@assets", assetRoot)
res, err := page.CompileJS([]byte(code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}
5 changes: 5 additions & 0 deletions sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type ITemplate interface {
Themes() []SelectOption

Asset(file string) (*Asset, error)

Build(option *BuildOption) error
SyncAssets(option *BuildOption) error
}

// IPage is the interface for the page
Expand All @@ -53,6 +56,8 @@ type IPage interface {

AssetScript() (*Asset, error)
AssetStyle() (*Asset, error)

Build(option *BuildOption) error
}

// IBlock is the interface for the block
Expand Down
43 changes: 14 additions & 29 deletions sui/core/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,16 @@ import (
func (page *Page) PreviewRender(request *Request) (string, error) {

warnings := []string{}
html, err := page.BuildHTML(request.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}
doc, warnings, err := page.Build(&BuildOption{
SSR: true,
AssetRoot: request.AssetRoot,
})

data, _, err := page.Data(request)
if err != nil {
warnings = append(warnings, err.Error())
}

html, err = page.Render(html, data, warnings)
if err != nil {
warnings = append(warnings, err.Error())
}

// Add Style & Script & Warning
doc, err := NewDocument([]byte(html))
if err != nil {
warnings = append(warnings, err.Error())
}

// Add Style
style, err := page.BuildStyle(request.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}
doc.Selection.Find("head").AppendHtml(style)

// Add Script
script, err := page.BuildScript(request.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}
doc.Selection.Find("body").AppendHtml(script)

// Add Frame Height
if request.Referer != "" {
doc.Selection.Find("body").AppendHtml(`
Expand Down Expand Up @@ -80,5 +55,15 @@ func (page *Page) PreviewRender(request *Request) (string, error) {
doc.Selection.Find("body").AppendHtml(warningHTML)
}

html, err := doc.Html()
if err != nil {
return "", err
}

html, err = page.Render(html, data, warnings)
if err != nil {
warnings = append(warnings, err.Error())
}

return doc.Html()
}
8 changes: 8 additions & 0 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ type Asset struct {
Content []byte `json:"content"`
}

// BuildOption is the struct for the option option
type BuildOption struct {
SSR bool `json:"ssr"`
CDN bool `json:"cdn"`
UpdateAll bool `json:"update_all"`
AssetRoot string `json:"asset_root,omitempty"`
}

// Request is the struct for the request
type Request struct {
Method string `json:"method"`
Expand Down
Loading

0 comments on commit 9a088ce

Please sign in to comment.