2020-08-03 04:46:04 +02:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/yosssi/ace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ace returns a new ace view engine.
|
|
|
|
// 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-08-03 04:46:04 +02:00
|
|
|
//
|
|
|
|
// Read more about the Ace Go Parser: https://github.com/yosssi/ace
|
|
|
|
func Ace(directory, extension string) *HTMLEngine {
|
|
|
|
s := HTML(directory, extension)
|
|
|
|
|
|
|
|
funcs := make(map[string]interface{}, 0)
|
|
|
|
|
|
|
|
once := new(sync.Once)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
name = path.Join(path.Clean(directory), name)
|
|
|
|
|
|
|
|
src := ace.NewSource(
|
|
|
|
ace.NewFile(name, text),
|
|
|
|
ace.NewFile("", []byte{}),
|
|
|
|
[]*ace.File{},
|
|
|
|
)
|
|
|
|
|
|
|
|
rslt, err := ace.ParseSource(src, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := ace.CompileResult(name, rslt, &ace.Options{
|
|
|
|
Extension: extension[1:],
|
|
|
|
FuncMap: funcs,
|
|
|
|
DelimLeft: s.left,
|
|
|
|
DelimRight: s.right,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Lookup(name).Tree.Root.String(), nil
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|