diff --git a/sui/api/compile_test.go b/sui/api/build_test.go similarity index 54% rename from sui/api/compile_test.go rename to sui/api/build_test.go index 02e9dccf..eaa6bdf7 100644 --- a/sui/api/compile_test.go +++ b/sui/api/build_test.go @@ -20,6 +20,38 @@ func TestCompile(t *testing.T) { assert.Len(t, warnings, 0) } +func TestTrans(t *testing.T) { + prepare(t) + defer clean() + + tmpl := testTmpl(t) + option := &core.BuildOption{SSR: true, AssetRoot: "/unit-test/assets"} + warnings, err := tmpl.Trans(option) + if err != nil { + t.Fatal(err) + } + assert.Len(t, warnings, 0) + warnings, err = tmpl.Build(option) + if err != nil { + t.Fatal(err) + } + assert.Len(t, warnings, 0) +} + +func testTmpl(t *testing.T) core.ITemplate { + sui := core.SUIs["test"] + if sui == nil { + t.Fatal("SUI test not found") + } + + tmpl, err := sui.GetTemplate("advanced") + if err != nil { + t.Fatal(err) + } + + return tmpl +} + func testPage(t *testing.T) *core.Page { sui := core.SUIs["test"] diff --git a/sui/api/request.go b/sui/api/request.go index 2ad2d724..fd1834f8 100644 --- a/sui/api/request.go +++ b/sui/api/request.go @@ -159,6 +159,7 @@ func (r *Request) Render() (string, int, error) { Debug: r.Request.DebugMode(), DisableCache: r.Request.DisableCache(), Route: r.Request.URL.Path, + Root: c.Root, Request: true, } @@ -196,7 +197,8 @@ func (r *Request) MakeCache() (*core.Cache, int, error) { configText := "" cacheStore := "" cacheTime := 0 - dateCacheTime := 0 + dataCacheTime := 0 + root := "" configSel := doc.Find("script[name=config]") if configSel != nil && configSel.Length() > 0 { @@ -222,7 +224,8 @@ func (r *Request) MakeCache() (*core.Cache, int, error) { // Cache store cacheStore = conf.CacheStore cacheTime = conf.Cache - dateCacheTime = conf.DataCache + dataCacheTime = conf.DataCache + root = conf.Root } dataText := "" @@ -253,8 +256,9 @@ func (r *Request) MakeCache() (*core.Cache, int, error) { GuardRedirect: guardRedirect, Config: configText, CacheStore: cacheStore, + Root: root, CacheTime: time.Duration(cacheTime) * time.Second, - DataCacheTime: time.Duration(dateCacheTime) * time.Second, + DataCacheTime: time.Duration(dataCacheTime) * time.Second, } go core.SetCache(r.File, cache) diff --git a/sui/core/cache.go b/sui/core/cache.go index bb9a4b38..a53f74db 100644 --- a/sui/core/cache.go +++ b/sui/core/cache.go @@ -16,6 +16,7 @@ type Cache struct { Guard string GuardRedirect string HTML string + Root string CacheStore string CacheTime time.Duration DataCacheTime time.Duration diff --git a/sui/core/page.go b/sui/core/page.go index c8ec548b..8e36439a 100644 --- a/sui/core/page.go +++ b/sui/core/page.go @@ -54,6 +54,7 @@ func (page *Page) GetConfig() *PageConfig { page.Config.Mock = &PageMock{Method: "GET"} } + page.Config.Root = page.Root return page.Config } @@ -69,6 +70,7 @@ func (page *Page) ExportConfig() string { "cache_store": page.CacheStore, "cache": page.Config.Cache, "data_cache": page.Config.DataCache, + "root": page.Root, }) if err != nil { diff --git a/sui/core/parser.go b/sui/core/parser.go index 03740397..3522453a 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -52,6 +52,7 @@ type ParserOption struct { Route string `json:"route,omitempty"` Theme any `json:"theme,omitempty"` Locale any `json:"locale,omitempty"` + Root string `json:"root,omitempty"` } var keepWords = map[string]bool{ @@ -128,9 +129,9 @@ func (parser *TemplateParser) Locale() *Locale { return nil } - route := parser.option.Route + root := parser.option.Root + route := strings.TrimPrefix(parser.option.Route, root) disableCache := parser.option.Preview || parser.option.Debug || parser.option.Editor || parser.option.DisableCache - locales, ok = Locales[name] if !ok { locales = map[string]*Locale{} @@ -141,7 +142,7 @@ func (parser *TemplateParser) Locale() *Locale { return locale } - path := filepath.Join("public", ".locales", name, route+".yml") + path := filepath.Join("public", parser.option.Root, ".locales", name, route+".yml") if exists, err := application.App.Exists(path); !exists { if err != nil { log.Error("[parser] %s Locale %s", route, err.Error()) diff --git a/sui/core/types.go b/sui/core/types.go index 291b600d..fd995c8a 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -36,6 +36,7 @@ type Page struct { SuiID string `json:"-"` Config *PageConfig `json:"-"` Path string `json:"-"` + Root string `json:"-"` Codes SourceCodes `json:"-"` Document []byte `json:"-"` GlobalData []byte `json:"-"` @@ -259,6 +260,7 @@ type BuildOption struct { SSR bool `json:"ssr"` CDN bool `json:"cdn"` UpdateAll bool `json:"update_all"` + PublicRoot string `json:"public_root,omitempty"` AssetRoot string `json:"asset_root,omitempty"` IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"` IgnoreDocument bool `json:"ignore_document,omitempty"` @@ -373,6 +375,7 @@ type PageSetting struct { Guard string `json:"guard,omitempty"` CacheStore string `json:"cache_store,omitempty"` Cache int `json:"cache,omitempty"` + Root string `json:"root,omitempty"` DataCache int `json:"data_cache,omitempty"` Description string `json:"description,omitempty"` SEO *PageSEO `json:"seo,omitempty"` diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go index b77c8959..dee91fd4 100644 --- a/sui/storages/local/build.go +++ b/sui/storages/local/build.go @@ -68,12 +68,16 @@ func (tmpl *Template) Build(option *core.BuildOption) ([]string, error) { // loaed pages tmpl.loaded = map[string]core.IPage{} + publicRoot, err := tmpl.local.DSL.PublicRoot(option.Data) + if err != nil { + log.Error("Get the public root error: %s. use %s", err.Error(), publicRoot) + } + option.PublicRoot = publicRoot for _, page := range pages { err := page.Load() if err != nil { return warnings, err } - messages, err := page.Build(ctx, option) if err != nil { return warnings, err @@ -217,14 +221,20 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error { func (page *Page) Build(globalCtx *core.GlobalBuildContext, option *core.BuildOption) ([]string, error) { ctx := core.NewBuildContext(globalCtx) - if option.AssetRoot == "" { - root, err := page.tmpl.local.DSL.PublicRoot(option.Data) + + var err error = nil + root := option.PublicRoot + if root == "" { + root, err = page.tmpl.local.DSL.PublicRoot(option.Data) if err != nil { - log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root) - root = page.tmpl.local.DSL.Public.Root + log.Error("Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root) } + } + + if option.AssetRoot == "" { option.AssetRoot = filepath.Join(root, "assets") } + page.Root = root html, warnings, err := page.Page.Compile(ctx, option) if err != nil {