Skip to content

Commit

Permalink
[add] page soruce structure
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Sep 25, 2023
1 parent 42fa2e1 commit 5d6c7d3
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 137 deletions.
14 changes: 10 additions & 4 deletions sui/storages/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@ func TestGetTemplates(t *testing.T) {
t.Fatalf("GetTemplates error: %v", err)
}

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

assert.Equal(t, "tech-blue", dempTmpls[0].(*Template).ID)
assert.Equal(t, "TECH-BLUE", dempTmpls[0].(*Template).Name)
assert.Equal(t, []string{}, dempTmpls[0].(*Template).Screenshots)
assert.Equal(t, "Tech Blue DEMO", dempTmpls[0].(*Template).Name)
assert.Equal(t, true, len(dempTmpls[1].(*Template).Screenshots) > 0)
assert.Equal(t, 1, dempTmpls[0].(*Template).Version)
assert.Equal(t, "", dempTmpls[0].(*Template).Descrption)
assert.Equal(t, "Tech Blue DEMO", dempTmpls[0].(*Template).Descrption)

assert.Equal(t, "website-ai", dempTmpls[1].(*Template).ID)
assert.Equal(t, "Website DEMO", dempTmpls[1].(*Template).Name)
assert.Equal(t, true, len(dempTmpls[1].(*Template).Screenshots) > 0)
assert.Equal(t, 2, dempTmpls[1].(*Template).Version)
assert.Equal(t, "AI Website DEMO", dempTmpls[1].(*Template).Descrption)

assert.Equal(t, "wechat-web", dempTmpls[2].(*Template).ID)
assert.Equal(t, "WECHAT-WEB", dempTmpls[2].(*Template).Name)
assert.Equal(t, []string{}, dempTmpls[2].(*Template).Screenshots)
assert.Equal(t, 1, dempTmpls[2].(*Template).Version)
assert.Equal(t, "", dempTmpls[2].(*Template).Descrption)

}

func TestGetTemplate(t *testing.T) {
Expand Down
77 changes: 77 additions & 0 deletions sui/storages/local/page.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
package local

import (
"fmt"
"path/filepath"
"strings"

"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/sui/core"
)

// Pages get the pages
func (tmpl *Template) Pages() ([]core.IPage, error) {

exts := []string{"*.sui", "*.html", "*.htm", "*.page"}
pages := []core.IPage{}
tmpl.local.fs.Walk(tmpl.Root, func(root, file string, isdir bool) error {
name := filepath.Base(file)
if isdir {
if strings.HasPrefix(name, "__") {
return filepath.SkipDir
}
return nil
}

if strings.HasPrefix(name, "__") {
return nil
}

page, err := tmpl.getPageFrom(file)
if err != nil {
log.Error("Get page error: %v", err)
return nil
}

pages = append(pages, page)
return nil
}, exts...)

return pages, nil
}

// Page get the page
func (tmpl *Template) Page(route string) (core.IPage, error) {
path := filepath.Join(tmpl.Root, 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)
}
}
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) getPage(route, file string) (core.IPage, error) {
root := filepath.Dir(file)
return &Page{
tmpl: tmpl,
Page: &core.Page{
Route: route,
Root: root,
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)},
},
},
}, nil
}

// Get get the page
func (page *Page) Get() error {
return nil
Expand Down
62 changes: 62 additions & 0 deletions sui/storages/local/page_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package local

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTemplatePages(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("website-ai")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

pages, err := tmpl.Pages()
if err != nil {
t.Fatalf("Pages error: %v", err)
}

if len(pages) < 1 {
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)
}

func TestTemplatePage(t *testing.T) {

tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("website-ai")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

page, err := tmpl.Page("/index")
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)

_, err = tmpl.Page("/the/page/could/not/be/found")
assert.Contains(t, err.Error(), "Page /the/page/could/not/be/found not found")
}
72 changes: 0 additions & 72 deletions sui/storages/local/template.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package local

import (
"fmt"
"path/filepath"
"strings"

"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/sui/core"
)

// Get get the template
Expand All @@ -19,74 +15,6 @@ func (tmpl *Template) Save() error {
return nil
}

// Pages get the pages
func (tmpl *Template) Pages() ([]core.IPage, error) {

exts := []string{"*.sui", "*.html", "*.htm", "*.page"}
pages := []core.IPage{}
tmpl.local.fs.Walk(tmpl.Root, func(root, file string, isdir bool) error {
name := filepath.Base(file)
if isdir {
if strings.HasPrefix(name, "__") {
return filepath.SkipDir
}
return nil
}

if strings.HasPrefix(name, "__") {
return nil
}

page, err := tmpl.getPageFrom(file)
if err != nil {
log.Error("Get page error: %v", err)
return nil
}

pages = append(pages, page)
return nil
}, exts...)

return pages, nil
}

// Page get the page
func (tmpl *Template) Page(route string) (core.IPage, error) {
path := filepath.Join(tmpl.Root, 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)
}
}
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) getPage(route, file string) (core.IPage, error) {
root := filepath.Dir(file)
return &Page{
tmpl: tmpl,
Page: &core.Page{
Route: route,
Root: root,
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)},
},
},
}, nil
}

func (tmpl *Template) getPageRoute(path string) string {
return strings.TrimSuffix(path[len(tmpl.Root):], filepath.Ext(path))
}
Expand Down
61 changes: 0 additions & 61 deletions sui/storages/local/template_test.go
Original file line number Diff line number Diff line change
@@ -1,62 +1 @@
package local

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTemplatePages(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("website-ai")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

pages, err := tmpl.Pages()
if err != nil {
t.Fatalf("Pages error: %v", err)
}

if len(pages) < 1 {
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)
}

func TestTemplatePage(t *testing.T) {

tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("website-ai")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

page, err := tmpl.Page("/index")
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)

_, err = tmpl.Page("/the/page/could/not/be/found")
assert.Contains(t, err.Error(), "Page /the/page/could/not/be/found not found")
}

0 comments on commit 5d6c7d3

Please sign in to comment.