-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore.go
110 lines (83 loc) · 1.74 KB
/
core.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package svg
import (
"github.com/gopherjs/vecty"
"github.com/nathanhack/svg/internal"
)
var Namespace = "http://www.w3.org/2000/svg"
type Number interface{}
type Length interface{}
type LengthOrPercent interface{}
type NumberOrPercent interface{}
func Percent(number Number) interface{} {
return internal.Stringify(number, "%")
}
type Component interface {
svg()
}
type Element interface {
Component
Tag() string
Attributes() []Attribute
Elements() []Element
Inner() string
}
type Root interface {
Element
}
type Core struct {
vecty.Core
}
func (c *Core) svg() {}
type svgComponent struct {
Core
elements []Element
attrs []Attribute
}
func (s *svgComponent) Inner() string {
return ""
}
func (s *svgComponent) Attributes() []Attribute {
return s.attrs
}
func (s *svgComponent) Tag() string {
return "svg"
}
func (s *svgComponent) Elements() []Element {
return s.elements
}
func SVG(elementsOrComponents ...Component) Root {
attrs := make([]Attribute, 0)
el := make([]Element, 0)
for _, x := range elementsOrComponents {
switch x.(type) {
case Element:
el = append(el, x.(Element))
case Attribute:
tmp := x.(Attribute)
attrs = append(attrs, tmp)
}
}
return &svgComponent{
elements: el,
attrs: attrs,
}
}
type Attribute interface {
Component
vecty.Applyer
}
func Render(svg Root) vecty.ComponentOrHTML {
return render(svg)
}
func render(el Element) vecty.ComponentOrHTML {
markups := []vecty.Applyer{vecty.Namespace(Namespace)}
items := make([]vecty.MarkupOrChild, 0)
for _, a := range el.Attributes() {
markups = append(markups, a)
}
for _, e := range el.Elements() {
items = append(items, render(e))
}
items = append(items, vecty.Markup(markups...))
return vecty.Tag(el.Tag(), items...)
}