Skip to content

Commit

Permalink
[add] page RenderEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Sep 27, 2023
1 parent 5d18028 commit d005a64
Showing 13 changed files with 309 additions and 9 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ require (
)

require (
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -39,8 +39,12 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
@@ -653,10 +657,12 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
4 changes: 4 additions & 0 deletions sui/core/compile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package core

// Compile the page
func (page *Page) Compile() {}
11 changes: 11 additions & 0 deletions sui/core/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core

// Data get the data
func (page *Page) Data(request *Request) (map[string]interface{}, error) {
return nil, nil
}

// RenderHTML render for the html
func (page *Page) renderData(html string, data map[string]interface{}, warnings []string) (string, error) {
return html, nil
}
1 change: 1 addition & 0 deletions sui/core/interfaces.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ type ITemplate interface {
// IPage is the interface for the page
type IPage interface {
Load() error
RenderEditor(request *Request) (*ResponseEditor, error)

// Render()

19 changes: 19 additions & 0 deletions sui/core/page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core

// GetHTML get the html
func (page *Page) GetHTML() {}

// GetScript get the script
func (page *Page) GetScript() {}

// GetStyle get the style
func (page *Page) GetStyle() {}

// GetData get the data
func (page *Page) GetData() {}

// SaveTemp save the temp
func (page *Page) SaveTemp() {}

// Save the page
func (page *Page) Save() {}
129 changes: 129 additions & 0 deletions sui/core/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package core

import (
"path/filepath"

"github.com/PuerkitoBio/goquery"
)

// Render render the page
func (page *Page) Render() {}

// RenderEditor render for the editor
func (page *Page) RenderEditor(request *Request) (*ResponseEditor, error) {

html := page.Codes.HTML.Code
res := &ResponseEditor{
HTML: "",
CSS: page.Codes.CSS.Code,
Scripts: []string{},
Styles: []string{},
Warnings: []string{},
}

// Get The scripts and styles
// Global scripts
scripts, err := page.GlobalScripts()
if err != nil {
res.Warnings = append(res.Warnings, err.Error())
}
res.Scripts = append(res.Scripts, scripts...)

// Global styles
styles, err := page.GlobalStyles()
if err != nil {
res.Warnings = append(res.Warnings, err.Error())
}
res.Styles = append(res.Styles, styles...)

// Page Styles
if page.Codes.CSS.Code != "" {
res.Styles = append(res.Styles, filepath.Join("@pages", page.Route, page.Name+".css"))
}

// Render the HTML with the data
// Page Scripts
if page.Codes.JS.Code != "" {
res.Scripts = append(res.Scripts, filepath.Join("@pages", page.Route, page.Name+".js"))
}
if page.Codes.TS.Code != "" {
res.Scripts = append(res.Scripts, filepath.Join("@pages", page.Route, page.Name+".ts"))
}

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

if data == nil {
res.HTML = html
return res, nil
}

if html != "" {
html, err := page.renderData(html, data, res.Warnings)
if err != nil {
res.Warnings = append(res.Warnings, err.Error())
}
res.HTML = html
}

return res, nil
}

// RenderPreview render for the preview
func (page *Page) RenderPreview() {}

// GlobalScripts get the global scripts
func (page *Page) GlobalScripts() ([]string, error) {
if page.Document == nil {
return []string{}, nil
}

doc, err := NewDocument(page.Document)
if err != nil {
return []string{}, err
}

// Global scripts
scripts := []string{}
doc.Find("script").Each(func(i int, s *goquery.Selection) {
src, _ := s.Attr("src")
if src != "" {
scripts = append(scripts, src)
}
})

return scripts, nil
}

// GlobalStyles get the global styles
func (page *Page) GlobalStyles() ([]string, error) {

if page.Document == nil {
return []string{}, nil
}

doc, err := NewDocument(page.Document)
if err != nil {
return []string{}, err
}

// Global styles
styles := []string{}
doc.Find("link[rel=stylesheet]").Each(func(i int, s *goquery.Selection) {
href, _ := s.Attr("href")
if href != "" {
styles = append(styles, href)
}
})

return styles, nil
}

func (page *Page) document() []byte {
if page.Document != nil {
return page.Document
}
return DocumentDefault
}
62 changes: 58 additions & 4 deletions sui/core/types.go
Original file line number Diff line number Diff line change
@@ -17,10 +17,11 @@ type DSL struct {

// Page is the struct for the page
type Page struct {
Route string `json:"route"`
Name string `json:"name,omitempty"`
Path string `json:"-"`
Codes SourceCodes `json:"-"`
Route string `json:"route"`
Name string `json:"name,omitempty"`
Path string `json:"-"`
Codes SourceCodes `json:"-"`
Document []byte `json:"-"`
}

// Component is the struct for the component
@@ -47,6 +48,7 @@ type Template struct {
Descrption string `json:"description"`
Screenshots []string `json:"screenshots"`
Themes []SelectOption `json:"themes"`
Document []byte `json:"-"`
}

// Theme is the struct for the theme
@@ -61,6 +63,27 @@ type SelectOption struct {
Value string `json:"value"`
}

// Request is the struct for the request
type Request struct {
Method string `json:"method"`
Payload map[string]interface{} `json:"payload,omitempty"`
Query map[string][]string `json:"query,omitempty"`
Params map[string]string `json:"params,omitempty"`
Headers []string `json:"headers,omitempty"`
Body []byte `json:"body,omitempty"`
Theme string `json:"theme,omitempty"`
Locale string `json:"locale,omitempty"`
}

// ResponseEditor is the struct for the response
type ResponseEditor struct {
HTML string `json:"html,omitempty"`
CSS string `json:"css,omitempty"`
Scripts []string `json:"scripts,omitempty"`
Styles []string `json:"styles,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}

// SourceCodes is the struct for the page codes
type SourceCodes struct {
HTML Source `json:"-"`
@@ -89,3 +112,34 @@ type Storage struct {
Driver string `json:"driver"`
Option map[string]interface{} `json:"option,omitempty"`
}

// DocumentDefault is the default document
var DocumentDefault = []byte(`
<!DOCTYPE html>
<html lang="{{ $REQ.locale || 'en' }}">
<head>
<meta charset="UTF-8" />
<title>{{ $DATA.head.title || '' }}</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta
name="description"
content="{{ $DATA.head.description || '' }}"
/>
<meta
name="keywords"
content="{{ $DATA.head.keywords || '' }}"
/>
<meta name="author" content="Yao" />
<meta name="website" content="https://yaoapps.com" />
<meta name="email" content="friends@iqka.com" />
<meta name="version" content="2.0.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body>
{{ __page }}
</body>
</html>
`)
18 changes: 18 additions & 0 deletions sui/core/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core

import (
"bytes"
"strings"

"github.com/PuerkitoBio/goquery"
)

// NewDocument create a new document
func NewDocument(html []byte) (*goquery.Document, error) {
return goquery.NewDocumentFromReader(bytes.NewReader(html))
}

// NewDocumentString create a new document
func NewDocumentString(html string) (*goquery.Document, error) {
return goquery.NewDocumentFromReader(strings.NewReader(html))
}
10 changes: 10 additions & 0 deletions sui/storages/local/local.go
Original file line number Diff line number Diff line change
@@ -130,6 +130,16 @@ func (local *Local) getTemplate(id string, path string) (*Template, error) {
}
}

// load the __document.html
documentFile := filepath.Join(path, "__document.html")
if local.fs.IsFile(documentFile) {
documentBytes, err := local.fs.ReadFile(documentFile)
if err != nil {
return nil, err
}
tmpl.Document = documentBytes
}

return &tmpl, nil
}

2 changes: 1 addition & 1 deletion sui/storages/local/local_test.go
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ func TestGetTemplates(t *testing.T) {
}

if len(dempTmpls) < 3 {
t.Fatalf("The demo templates less than %v", len(dempTmpls))
t.Fatalf("The demo templates less than 3 (%v<3)", len(dempTmpls))
}

assert.Equal(t, "tech-blue", dempTmpls[0].(*Template).ID)
13 changes: 9 additions & 4 deletions sui/storages/local/page.go
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ func (tmpl *Template) getPage(route, file string) (core.IPage, error) {
Page: &core.Page{
Route: route,
Path: path,
Name: name,
Codes: core.SourceCodes{
HTML: core.Source{File: fmt.Sprintf("%s%s", name, filepath.Ext(file))},
CSS: core.Source{File: fmt.Sprintf("%s.css", name)},
@@ -117,11 +118,13 @@ func (page *Page) Load() error {

} else {
jsFile := filepath.Join(page.Path, page.Codes.JS.File)
jsCode, err := page.tmpl.local.fs.ReadFile(jsFile)
if err != nil {
return err
if exist, _ := page.tmpl.local.fs.Exists(jsFile); exist {
jsCode, err := page.tmpl.local.fs.ReadFile(jsFile)
if err != nil {
return err
}
page.Codes.JS.Code = string(jsCode)
}
page.Codes.JS.Code = string(jsCode)
}

// Read the HTML code
@@ -155,5 +158,7 @@ func (page *Page) Load() error {
page.Codes.DATA.Code = string(dataCode)
}

// Set the page document
page.Document = page.tmpl.Document
return nil
}
Loading

0 comments on commit d005a64

Please sign in to comment.