package chrome
import (
"fmt"
"html/template"
)
// Funcs returns the template helpers every service was carrying its own copy
// of. Merge into a service's FuncMap before its own helpers, so a service can
// still shadow a name deliberately.
func Funcs() template.FuncMap {
return template.FuncMap{
"dict": Dict,
"shortsha": ShortSHA,
}
}
// Dict builds a map from alternating key/value arguments, so a partial that
// needs several fields can be invoked with an inline context:
// {{template "x" (dict "A" .A "B" .B)}}. An odd argument count or a non-string
// key is a template authoring error and surfaces as a render error.
func Dict(kv ...any) (map[string]any, error) {
if len(kv)%2 != 0 {
return nil, fmt.Errorf("dict: expected an even number of arguments, got %d", len(kv))
}
m := make(map[string]any, len(kv)/2)
for i := 0; i < len(kv); i += 2 {
k, ok := kv[i].(string)
if !ok {
return nil, fmt.Errorf("dict: key %d is not a string", i)
}
m[k] = kv[i+1]
}
return m, nil
}
// ShortSHA abbreviates an object id to its first 8 characters (or returns it
// unchanged if shorter), the convention used everywhere commits are listed.
func ShortSHA(s string) string {
if len(s) > 8 {
return s[:8]
}
return s
}