forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
309 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package core | ||
|
||
// Compile the page | ||
func (page *Page) Compile() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.