2020-08-03 04:46:04 +02:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/yosssi/ace"
|
|
|
|
)
|
|
|
|
|
2020-10-01 15:06:16 +02:00
|
|
|
// AceEngine represents the Ace view engine.
|
|
|
|
// See the `Ace` package-level function for more.
|
|
|
|
type AceEngine struct {
|
|
|
|
*HTMLEngine
|
|
|
|
|
|
|
|
indent string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIndent string used for indentation.
|
|
|
|
// Do NOT use tabs, only spaces characters.
|
|
|
|
// Defaults to minified response, no indentation.
|
|
|
|
func (s *AceEngine) SetIndent(indent string) *AceEngine {
|
|
|
|
s.indent = indent
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ace returns a new Ace view engine.
|
2020-08-03 04:46:04 +02:00
|
|
|
// It shares the same exactly logic with the
|
|
|
|
// html view engine, it uses the same exactly configuration.
|
2020-08-05 05:46:45 +02:00
|
|
|
// The given "extension" MUST begin with a dot.
|
2020-10-01 15:06:16 +02:00
|
|
|
// Ace minifies the response automatically unless
|
|
|
|
// SetIndent() method is set.
|
2020-08-03 04:46:04 +02:00
|
|
|
//
|
|
|
|
// Read more about the Ace Go Parser: https://github.com/yosssi/ace
|
2020-09-05 07:34:09 +02:00
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// Ace("./views", ".ace") or
|
|
|
|
// Ace(iris.Dir("./views"), ".ace") or
|
|
|
|
// Ace(AssetFile(), ".ace") for embedded data.
|
2020-10-01 15:06:16 +02:00
|
|
|
func Ace(fs interface{}, extension string) *AceEngine {
|
|
|
|
s := &AceEngine{HTMLEngine: HTML(fs, extension), indent: ""}
|
2020-09-29 18:19:19 +02:00
|
|
|
s.name = "Ace"
|
2020-08-03 04:46:04 +02:00
|
|
|
|
2021-01-09 04:41:20 +01:00
|
|
|
funcs := make(map[string]interface{})
|
2020-08-03 04:46:04 +02:00
|
|
|
|
|
|
|
once := new(sync.Once)
|
2020-10-01 15:06:16 +02:00
|
|
|
|
2020-08-03 04:46:04 +02:00
|
|
|
s.middleware = func(name string, text []byte) (contents string, err error) {
|
|
|
|
once.Do(func() { // on first template parse, all funcs are given.
|
2020-08-05 18:34:55 +02:00
|
|
|
for k, v := range emptyFuncs {
|
2020-08-03 04:46:04 +02:00
|
|
|
funcs[k] = v
|
|
|
|
}
|
2020-08-05 18:34:55 +02:00
|
|
|
|
|
|
|
for k, v := range s.funcs {
|
2020-08-03 04:46:04 +02:00
|
|
|
funcs[k] = v
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-09-05 07:34:09 +02:00
|
|
|
// name = path.Join(path.Clean(directory), name)
|
2020-08-03 04:46:04 +02:00
|
|
|
|
|
|
|
src := ace.NewSource(
|
|
|
|
ace.NewFile(name, text),
|
|
|
|
ace.NewFile("", []byte{}),
|
|
|
|
[]*ace.File{},
|
|
|
|
)
|
|
|
|
|
2020-10-01 15:06:16 +02:00
|
|
|
opts := &ace.Options{
|
2020-08-03 04:46:04 +02:00
|
|
|
Extension: extension[1:],
|
|
|
|
FuncMap: funcs,
|
|
|
|
DelimLeft: s.left,
|
|
|
|
DelimRight: s.right,
|
2020-10-01 15:06:16 +02:00
|
|
|
Indent: s.indent,
|
|
|
|
}
|
|
|
|
|
|
|
|
rslt, err := ace.ParseSource(src, opts)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := ace.CompileResult(name, rslt, opts)
|
2020-08-03 04:46:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Lookup(name).Tree.Root.String(), nil
|
|
|
|
}
|
2020-09-29 18:19:19 +02:00
|
|
|
|
2020-08-03 04:46:04 +02:00
|
|
|
return s
|
|
|
|
}
|