forked from googlecodelabs/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
69 lines (63 loc) · 1.55 KB
/
image.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
package nodes
import "strings"
type NewImageNodeOptions struct {
Src string
Width float32
Alt string
Title string
}
// NewImageNode creates a new ImageNode with the given options.
// TODO this API is inconsistent with button
func NewImageNode(opts NewImageNodeOptions) *ImageNode {
return &ImageNode{
node: node{typ: NodeImage},
Src: opts.Src,
Width: opts.Width,
Alt: opts.Alt,
Title: opts.Title,
}
}
// ImageNode represents a single image.
type ImageNode struct {
node
Src string
Width float32
Alt string
Title string
}
// Empty returns true if its Src is zero, excluding space runes.
func (in *ImageNode) Empty() bool {
return strings.TrimSpace(in.Src) == ""
}
// ImageNodes extracts everything except NodeImage nodes, recursively.
// TODO rename
func ImageNodes(nodes []Node) []*ImageNode {
var imgs []*ImageNode
for _, n := range nodes {
switch n := n.(type) {
case *ImageNode:
imgs = append(imgs, n)
case *ListNode:
imgs = append(imgs, ImageNodes(n.Nodes)...)
case *ItemsListNode:
for _, i := range n.Items {
imgs = append(imgs, ImageNodes(i.Nodes)...)
}
case *HeaderNode:
imgs = append(imgs, ImageNodes(n.Content.Nodes)...)
case *URLNode:
imgs = append(imgs, ImageNodes(n.Content.Nodes)...)
case *ButtonNode:
imgs = append(imgs, ImageNodes(n.Content.Nodes)...)
case *InfoboxNode:
imgs = append(imgs, ImageNodes(n.Content.Nodes)...)
case *GridNode:
for _, r := range n.Rows {
for _, c := range r {
imgs = append(imgs, ImageNodes(c.Content.Nodes)...)
}
}
}
}
return imgs
}