[add] Page Builder page load
This commit is contained in:
parent
5d6c7d32d6
commit
70f6931eb7
5 changed files with 165 additions and 51 deletions
|
|
@ -20,8 +20,7 @@ type ITemplate interface {
|
|||
|
||||
// IPage is the interface for the page
|
||||
type IPage interface {
|
||||
Get() error
|
||||
Save() error
|
||||
Load() error
|
||||
|
||||
// Render()
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ type DSL struct {
|
|||
type Page struct {
|
||||
Route string `json:"route"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Root string `json:"-"`
|
||||
Path string `json:"-"`
|
||||
Codes SourceCodes `json:"-"`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -42,47 +42,118 @@ func (tmpl *Template) Pages() ([]core.IPage, error) {
|
|||
|
||||
// Page get the page
|
||||
func (tmpl *Template) Page(route string) (core.IPage, error) {
|
||||
path := filepath.Join(tmpl.Root, route)
|
||||
path := tmpl.getPagePath(route)
|
||||
exts := []string{".sui", ".html", ".htm", ".page"}
|
||||
for _, ext := range exts {
|
||||
file := fmt.Sprintf("%s%s", path, ext)
|
||||
if exist, _ := tmpl.local.fs.Exists(file); exist {
|
||||
return tmpl.getPageFrom(file)
|
||||
page, err := tmpl.getPageFrom(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Load the page source code
|
||||
err = page.Load()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("Page %s not found", route)
|
||||
}
|
||||
|
||||
func (tmpl *Template) getPageFrom(path string) (core.IPage, error) {
|
||||
route := tmpl.getPageRoute(path)
|
||||
return tmpl.getPage(route, path)
|
||||
func (tmpl *Template) getPageFrom(file string) (core.IPage, error) {
|
||||
route := tmpl.getPageRoute(file)
|
||||
return tmpl.getPage(route, file)
|
||||
}
|
||||
|
||||
func (tmpl *Template) getPage(route, file string) (core.IPage, error) {
|
||||
root := filepath.Dir(file)
|
||||
path := filepath.Dir(file)
|
||||
name := tmpl.getPageBase(route)
|
||||
return &Page{
|
||||
tmpl: tmpl,
|
||||
Page: &core.Page{
|
||||
Route: route,
|
||||
Root: root,
|
||||
Path: path,
|
||||
Codes: core.SourceCodes{
|
||||
HTML: core.Source{File: fmt.Sprintf("%s%s", route, filepath.Ext(file))},
|
||||
CSS: core.Source{File: fmt.Sprintf("%s.css", route)},
|
||||
JS: core.Source{File: fmt.Sprintf("%s.js", route)},
|
||||
DATA: core.Source{File: fmt.Sprintf("%s.json", route)},
|
||||
TS: core.Source{File: fmt.Sprintf("%s.ts", route)},
|
||||
LESS: core.Source{File: fmt.Sprintf("%s.less", route)},
|
||||
HTML: core.Source{File: fmt.Sprintf("%s%s", name, filepath.Ext(file))},
|
||||
CSS: core.Source{File: fmt.Sprintf("%s.css", name)},
|
||||
JS: core.Source{File: fmt.Sprintf("%s.js", name)},
|
||||
DATA: core.Source{File: fmt.Sprintf("%s.json", name)},
|
||||
TS: core.Source{File: fmt.Sprintf("%s.ts", name)},
|
||||
LESS: core.Source{File: fmt.Sprintf("%s.less", name)},
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Get get the page
|
||||
func (page *Page) Get() error {
|
||||
return nil
|
||||
func (tmpl *Template) getPageRoute(file string) string {
|
||||
return filepath.Dir(file[len(tmpl.Root):])
|
||||
}
|
||||
|
||||
// Save save the page
|
||||
func (page *Page) Save() error {
|
||||
func (tmpl *Template) getPagePath(route string) string {
|
||||
name := tmpl.getPageBase(route)
|
||||
return filepath.Join(tmpl.Root, route, name)
|
||||
}
|
||||
|
||||
func (tmpl *Template) getPageBase(route string) string {
|
||||
return filepath.Base(route)
|
||||
}
|
||||
|
||||
// Load get the page from the storage
|
||||
func (page *Page) Load() error {
|
||||
|
||||
// Read the Script code
|
||||
// Type script is the default language
|
||||
tsFile := filepath.Join(page.Path, page.Codes.TS.File)
|
||||
if exist, _ := page.tmpl.local.fs.Exists(tsFile); exist {
|
||||
tsCode, err := page.tmpl.local.fs.ReadFile(tsFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
page.Codes.TS.Code = string(tsCode)
|
||||
|
||||
} else {
|
||||
jsFile := filepath.Join(page.Path, page.Codes.JS.File)
|
||||
jsCode, err := page.tmpl.local.fs.ReadFile(jsFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
page.Codes.JS.Code = string(jsCode)
|
||||
}
|
||||
|
||||
// Read the HTML code
|
||||
htmlFile := filepath.Join(page.Path, page.Codes.HTML.File)
|
||||
if exist, _ := page.tmpl.local.fs.Exists(htmlFile); exist {
|
||||
htmlCode, err := page.tmpl.local.fs.ReadFile(htmlFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
page.Codes.HTML.Code = string(htmlCode)
|
||||
}
|
||||
|
||||
// Read the CSS code
|
||||
// @todo: Less support
|
||||
cssFile := filepath.Join(page.Path, page.Codes.CSS.File)
|
||||
if exist, _ := page.tmpl.local.fs.Exists(cssFile); exist {
|
||||
cssCode, err := page.tmpl.local.fs.ReadFile(cssFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
page.Codes.CSS.Code = string(cssCode)
|
||||
}
|
||||
|
||||
// Read the JSON code
|
||||
dataFile := filepath.Join(page.Path, page.Codes.DATA.File)
|
||||
if exist, _ := page.tmpl.local.fs.Exists(dataFile); exist {
|
||||
dataCode, err := page.tmpl.local.fs.ReadFile(dataFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
page.Codes.DATA.Code = string(dataCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package local
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -10,7 +11,7 @@ func TestTemplatePages(t *testing.T) {
|
|||
tests := prepare(t)
|
||||
defer clean()
|
||||
|
||||
tmpl, err := tests.Demo.GetTemplate("website-ai")
|
||||
tmpl, err := tests.Demo.GetTemplate("tech-blue")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTemplate error: %v", err)
|
||||
}
|
||||
|
|
@ -20,42 +21,94 @@ func TestTemplatePages(t *testing.T) {
|
|||
t.Fatalf("Pages error: %v", err)
|
||||
}
|
||||
|
||||
if len(pages) < 1 {
|
||||
if len(pages) < 8 {
|
||||
t.Fatalf("Pages error: %v", len(pages))
|
||||
}
|
||||
|
||||
assert.Equal(t, "/index", pages[0].(*Page).Route)
|
||||
assert.Equal(t, "/templates/website-ai", pages[0].(*Page).Root)
|
||||
assert.Equal(t, "/index.css", pages[0].(*Page).Codes.CSS.File)
|
||||
assert.Equal(t, "/index.html", pages[0].(*Page).Codes.HTML.File)
|
||||
assert.Equal(t, "/index.js", pages[0].(*Page).Codes.JS.File)
|
||||
assert.Equal(t, "/index.less", pages[0].(*Page).Codes.LESS.File)
|
||||
assert.Equal(t, "/index.ts", pages[0].(*Page).Codes.TS.File)
|
||||
assert.Equal(t, "/index.json", pages[0].(*Page).Codes.DATA.File)
|
||||
for _, page := range pages {
|
||||
|
||||
page := page.(*Page)
|
||||
name := filepath.Base(page.Path)
|
||||
dir := page.Path[len(tmpl.(*Template).Root):]
|
||||
path := filepath.Join(tmpl.(*Template).Root, dir)
|
||||
|
||||
assert.Equal(t, dir, page.Route)
|
||||
assert.Equal(t, path, page.Path)
|
||||
assert.Equal(t, name+".css", page.Codes.CSS.File)
|
||||
assert.Equal(t, name+".html", page.Codes.HTML.File)
|
||||
assert.Equal(t, name+".js", page.Codes.JS.File)
|
||||
assert.Equal(t, name+".less", page.Codes.LESS.File)
|
||||
assert.Equal(t, name+".ts", page.Codes.TS.File)
|
||||
assert.Equal(t, name+".json", page.Codes.DATA.File)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplatePage(t *testing.T) {
|
||||
func TestTemplatePageTS(t *testing.T) {
|
||||
|
||||
tests := prepare(t)
|
||||
defer clean()
|
||||
|
||||
tmpl, err := tests.Demo.GetTemplate("website-ai")
|
||||
tmpl, err := tests.Demo.GetTemplate("tech-blue")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTemplate error: %v", err)
|
||||
}
|
||||
|
||||
page, err := tmpl.Page("/index")
|
||||
ipage, err := tmpl.Page("/page/[id]")
|
||||
if err != nil {
|
||||
t.Fatalf("Page error: %v", err)
|
||||
}
|
||||
assert.Equal(t, "/index", page.(*Page).Route)
|
||||
assert.Equal(t, "/templates/website-ai", page.(*Page).Root)
|
||||
assert.Equal(t, "/index.css", page.(*Page).Codes.CSS.File)
|
||||
assert.Equal(t, "/index.html", page.(*Page).Codes.HTML.File)
|
||||
assert.Equal(t, "/index.js", page.(*Page).Codes.JS.File)
|
||||
assert.Equal(t, "/index.less", page.(*Page).Codes.LESS.File)
|
||||
assert.Equal(t, "/index.ts", page.(*Page).Codes.TS.File)
|
||||
assert.Equal(t, "/index.json", page.(*Page).Codes.DATA.File)
|
||||
|
||||
page := ipage.(*Page)
|
||||
|
||||
assert.Equal(t, "/page/[id]", page.Route)
|
||||
assert.Equal(t, "/templates/tech-blue/page/[id]", page.Path)
|
||||
assert.Equal(t, "[id].css", page.Codes.CSS.File)
|
||||
assert.Equal(t, "[id].html", page.Codes.HTML.File)
|
||||
assert.Equal(t, "[id].js", page.Codes.JS.File)
|
||||
assert.Equal(t, "[id].less", page.Codes.LESS.File)
|
||||
assert.Equal(t, "[id].ts", page.Codes.TS.File)
|
||||
assert.Equal(t, "[id].json", page.Codes.DATA.File)
|
||||
|
||||
assert.NotEmpty(t, page.Codes.TS.Code)
|
||||
assert.Empty(t, page.Codes.JS.Code)
|
||||
assert.NotEmpty(t, page.Codes.HTML.Code)
|
||||
assert.NotEmpty(t, page.Codes.CSS.Code)
|
||||
assert.NotEmpty(t, page.Codes.DATA.Code)
|
||||
|
||||
_, err = tmpl.Page("/the/page/could/not/be/found")
|
||||
assert.Contains(t, err.Error(), "Page /the/page/could/not/be/found not found")
|
||||
}
|
||||
|
||||
func TestTemplatePageJS(t *testing.T) {
|
||||
|
||||
tests := prepare(t)
|
||||
defer clean()
|
||||
|
||||
tmpl, err := tests.Demo.GetTemplate("tech-blue")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTemplate error: %v", err)
|
||||
}
|
||||
|
||||
ipage, err := tmpl.Page("/page/404")
|
||||
if err != nil {
|
||||
t.Fatalf("Page error: %v", err)
|
||||
}
|
||||
|
||||
page := ipage.(*Page)
|
||||
assert.Equal(t, "/page/404", page.Route)
|
||||
assert.Equal(t, "/templates/tech-blue/page/404", page.Path)
|
||||
assert.Equal(t, "404.css", page.Codes.CSS.File)
|
||||
assert.Equal(t, "404.html", page.Codes.HTML.File)
|
||||
assert.Equal(t, "404.js", page.Codes.JS.File)
|
||||
assert.Equal(t, "404.less", page.Codes.LESS.File)
|
||||
assert.Equal(t, "404.ts", page.Codes.TS.File)
|
||||
assert.Equal(t, "404.json", page.Codes.DATA.File)
|
||||
|
||||
assert.NotEmpty(t, page.Codes.JS.Code)
|
||||
assert.Empty(t, page.Codes.TS.Code)
|
||||
assert.NotEmpty(t, page.Codes.HTML.Code)
|
||||
assert.Empty(t, page.Codes.CSS.Code)
|
||||
assert.NotEmpty(t, page.Codes.DATA.Code)
|
||||
|
||||
_, err = tmpl.Page("/the/page/could/not/be/found")
|
||||
assert.Contains(t, err.Error(), "Page /the/page/could/not/be/found not found")
|
||||
|
|
|
|||
|
|
@ -1,10 +1,5 @@
|
|||
package local
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Get get the template
|
||||
func (tmpl *Template) Get() error {
|
||||
return nil
|
||||
|
|
@ -15,10 +10,6 @@ func (tmpl *Template) Save() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (tmpl *Template) getPageRoute(path string) string {
|
||||
return strings.TrimSuffix(path[len(tmpl.Root):], filepath.Ext(path))
|
||||
}
|
||||
|
||||
// Styles get the global styles
|
||||
func (tmpl *Template) Styles() []string {
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue