forked from gobuffalo/tags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
38 lines (34 loc) · 845 Bytes
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package tags
import (
"fmt"
"html/template"
"sort"
"strings"
)
// Options is a map used to configure tags
type Options map[string]interface{}
func (o Options) String() string {
var out = make([]string, 0, len(o))
for k, v := range o {
if m, ok := v.(map[string]interface{}); ok {
for mk, mv := range m {
out = append(out, kv(fmt.Sprintf("%s-%s", k, mk), mv))
}
continue
}
out = append(out, kv(k, v))
}
sort.Strings(out)
return strings.Join(out, " ")
}
func kv(k string, v interface{}) string {
var tmp = make([]string, 2)
tmp[0] = template.HTMLEscaper(k)
if v != nil {
tmp[1] = fmt.Sprintf("\"%s\"", template.HTMLEscaper(v))
return strings.Join(tmp, "=")
}
// nil attribute value is interpreted as empty attribute notation
// https://www.w3.org/TR/html5/syntax.html#elements-attributes
return tmp[0]
}