You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1005 B

2 years ago
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
}