init template
This commit is contained in:
43
parser/name.go
Normal file
43
parser/name.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/mime"
|
||||
"gitoa.ru/go-4devs/templating/render"
|
||||
)
|
||||
|
||||
const (
|
||||
nameWithExt = 2
|
||||
nameWithEngine = 3
|
||||
countOption = 2
|
||||
)
|
||||
|
||||
// Name parse name by <tpl name>.<format>.<engine> or <tpl name>.<format>.
|
||||
func Name(_ context.Context, name string, opts ...render.Option) (render.Reference, error) {
|
||||
options := make([]render.Option, 0, countOption)
|
||||
tplName := strings.ToLower(name)
|
||||
|
||||
base := path.Base(tplName)
|
||||
el := strings.SplitN(base, ".", nameWithEngine)
|
||||
|
||||
var ext mime.Ext
|
||||
|
||||
switch len(el) {
|
||||
case nameWithEngine:
|
||||
ext = mime.ExtFromString(el[1])
|
||||
options = append(options, render.WithEngine(el[2]))
|
||||
tplName = strings.TrimRight(tplName, "."+el[1]+"."+el[2])
|
||||
case nameWithExt:
|
||||
ext = mime.ExtFromString(el[1])
|
||||
tplName = strings.TrimRight(tplName, "."+el[1])
|
||||
}
|
||||
|
||||
if !ext.Is(mime.ExtUnrecognized) {
|
||||
options = append(options, render.WithFormat(ext))
|
||||
}
|
||||
|
||||
return render.NewReference(tplName, append(options, opts...)...), nil
|
||||
}
|
||||
Reference in New Issue
Block a user