commit
1c18f2e11e
5 changed files with 165 additions and 51 deletions
|
|
@ -20,8 +20,7 @@ type ITemplate interface {
|
||||||
|
|
||||||
// IPage is the interface for the page
|
// IPage is the interface for the page
|
||||||
type IPage interface {
|
type IPage interface {
|
||||||
Get() error
|
Load() error
|
||||||
Save() error
|
|
||||||
|
|
||||||
// Render()
|
// Render()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ type DSL struct {
|
||||||
type Page struct {
|
type Page struct {
|
||||||
Route string `json:"route"`
|
Route string `json:"route"`
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
Root string `json:"-"`
|
Path string `json:"-"`
|
||||||
Codes SourceCodes `json:"-"`
|
Codes SourceCodes `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,47 +42,118 @@ func (tmpl *Template) Pages() ([]core.IPage, error) {
|
||||||
|
|
||||||
// Page get the page
|
// Page get the page
|
||||||
func (tmpl *Template) Page(route string) (core.IPage, error) {
|
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"}
|
exts := []string{".sui", ".html", ".htm", ".page"}
|
||||||
for _, ext := range exts {
|
for _, ext := range exts {
|
||||||
file := fmt.Sprintf("%s%s", path, ext)
|
file := fmt.Sprintf("%s%s", path, ext)
|
||||||
if exist, _ := tmpl.local.fs.Exists(file); exist {
|
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)
|
return nil, fmt.Errorf("Page %s not found", route)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tmpl *Template) getPageFrom(path string) (core.IPage, error) {
|
func (tmpl *Template) getPageFrom(file string) (core.IPage, error) {
|
||||||
route := tmpl.getPageRoute(path)
|
route := tmpl.getPageRoute(file)
|
||||||
return tmpl.getPage(route, path)
|
return tmpl.getPage(route, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tmpl *Template) getPage(route, file string) (core.IPage, error) {
|
func (tmpl *Template) getPage(route, file string) (core.IPage, error) {
|
||||||
root := filepath.Dir(file)
|
path := filepath.Dir(file)
|
||||||
|
name := tmpl.getPageBase(route)
|
||||||
return &Page{
|
return &Page{
|
||||||
tmpl: tmpl,
|
tmpl: tmpl,
|
||||||
Page: &core.Page{
|
Page: &core.Page{
|
||||||
Route: route,
|
Route: route,
|
||||||
Root: root,
|
Path: path,
|
||||||
Codes: core.SourceCodes{
|
Codes: core.SourceCodes{
|
||||||
HTML: core.Source{File: fmt.Sprintf("%s%s", route, filepath.Ext(file))},
|
HTML: core.Source{File: fmt.Sprintf("%s%s", name, filepath.Ext(file))},
|
||||||
CSS: core.Source{File: fmt.Sprintf("%s.css", route)},
|
CSS: core.Source{File: fmt.Sprintf("%s.css", name)},
|
||||||
JS: core.Source{File: fmt.Sprintf("%s.js", route)},
|
JS: core.Source{File: fmt.Sprintf("%s.js", name)},
|
||||||
DATA: core.Source{File: fmt.Sprintf("%s.json", route)},
|
DATA: core.Source{File: fmt.Sprintf("%s.json", name)},
|
||||||
TS: core.Source{File: fmt.Sprintf("%s.ts", route)},
|
TS: core.Source{File: fmt.Sprintf("%s.ts", name)},
|
||||||
LESS: core.Source{File: fmt.Sprintf("%s.less", route)},
|
LESS: core.Source{File: fmt.Sprintf("%s.less", name)},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get get the page
|
func (tmpl *Template) getPageRoute(file string) string {
|
||||||
func (page *Page) Get() error {
|
return filepath.Dir(file[len(tmpl.Root):])
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save save the page
|
func (tmpl *Template) getPagePath(route string) string {
|
||||||
func (page *Page) Save() error {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package local
|
package local
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
@ -10,7 +11,7 @@ func TestTemplatePages(t *testing.T) {
|
||||||
tests := prepare(t)
|
tests := prepare(t)
|
||||||
defer clean()
|
defer clean()
|
||||||
|
|
||||||
tmpl, err := tests.Demo.GetTemplate("website-ai")
|
tmpl, err := tests.Demo.GetTemplate("tech-blue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetTemplate error: %v", err)
|
t.Fatalf("GetTemplate error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -20,42 +21,94 @@ func TestTemplatePages(t *testing.T) {
|
||||||
t.Fatalf("Pages error: %v", err)
|
t.Fatalf("Pages error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(pages) < 1 {
|
if len(pages) < 8 {
|
||||||
t.Fatalf("Pages error: %v", len(pages))
|
t.Fatalf("Pages error: %v", len(pages))
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, "/index", pages[0].(*Page).Route)
|
for _, page := range pages {
|
||||||
assert.Equal(t, "/templates/website-ai", pages[0].(*Page).Root)
|
|
||||||
assert.Equal(t, "/index.css", pages[0].(*Page).Codes.CSS.File)
|
page := page.(*Page)
|
||||||
assert.Equal(t, "/index.html", pages[0].(*Page).Codes.HTML.File)
|
name := filepath.Base(page.Path)
|
||||||
assert.Equal(t, "/index.js", pages[0].(*Page).Codes.JS.File)
|
dir := page.Path[len(tmpl.(*Template).Root):]
|
||||||
assert.Equal(t, "/index.less", pages[0].(*Page).Codes.LESS.File)
|
path := filepath.Join(tmpl.(*Template).Root, dir)
|
||||||
assert.Equal(t, "/index.ts", pages[0].(*Page).Codes.TS.File)
|
|
||||||
assert.Equal(t, "/index.json", pages[0].(*Page).Codes.DATA.File)
|
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)
|
tests := prepare(t)
|
||||||
defer clean()
|
defer clean()
|
||||||
|
|
||||||
tmpl, err := tests.Demo.GetTemplate("website-ai")
|
tmpl, err := tests.Demo.GetTemplate("tech-blue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetTemplate error: %v", err)
|
t.Fatalf("GetTemplate error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
page, err := tmpl.Page("/index")
|
ipage, err := tmpl.Page("/page/[id]")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Page error: %v", err)
|
t.Fatalf("Page error: %v", err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, "/index", page.(*Page).Route)
|
|
||||||
assert.Equal(t, "/templates/website-ai", page.(*Page).Root)
|
page := ipage.(*Page)
|
||||||
assert.Equal(t, "/index.css", page.(*Page).Codes.CSS.File)
|
|
||||||
assert.Equal(t, "/index.html", page.(*Page).Codes.HTML.File)
|
assert.Equal(t, "/page/[id]", page.Route)
|
||||||
assert.Equal(t, "/index.js", page.(*Page).Codes.JS.File)
|
assert.Equal(t, "/templates/tech-blue/page/[id]", page.Path)
|
||||||
assert.Equal(t, "/index.less", page.(*Page).Codes.LESS.File)
|
assert.Equal(t, "[id].css", page.Codes.CSS.File)
|
||||||
assert.Equal(t, "/index.ts", page.(*Page).Codes.TS.File)
|
assert.Equal(t, "[id].html", page.Codes.HTML.File)
|
||||||
assert.Equal(t, "/index.json", page.(*Page).Codes.DATA.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")
|
_, err = tmpl.Page("/the/page/could/not/be/found")
|
||||||
assert.Contains(t, err.Error(), "Page /the/page/could/not/be/found not found")
|
assert.Contains(t, err.Error(), "Page /the/page/could/not/be/found not found")
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,5 @@
|
||||||
package local
|
package local
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Get get the template
|
// Get get the template
|
||||||
func (tmpl *Template) Get() error {
|
func (tmpl *Template) Get() error {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -15,10 +10,6 @@ func (tmpl *Template) Save() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tmpl *Template) getPageRoute(path string) string {
|
|
||||||
return strings.TrimSuffix(path[len(tmpl.Root):], filepath.Ext(path))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Styles get the global styles
|
// Styles get the global styles
|
||||||
func (tmpl *Template) Styles() []string {
|
func (tmpl *Template) Styles() []string {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue