-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
102 lines (83 loc) · 1.4 KB
/
node.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
package hype
import (
"fmt"
"golang.org/x/net/html"
)
type Node interface {
Children() Nodes
}
type Nodes []Node
type HTMLNode interface {
Node
HTML() *html.Node
}
type MDNode interface {
Node
MD() string
}
func (list Nodes) String() string {
var s string
for _, n := range list {
if st, ok := n.(fmt.Stringer); ok {
s += st.String()
continue
}
s += n.Children().String()
}
return s
}
func (list Nodes) MD() string {
var s string
for _, n := range list {
if st, ok := n.(MDNode); ok {
s += st.MD()
continue
}
}
return s
}
func (list Nodes) Children() Nodes {
return list
}
func (list Nodes) Delete(node Node) Nodes {
if len(list) == 0 {
return list
}
nodes := make(Nodes, 0, len(list)-1)
for _, n := range list {
if n == node {
continue
}
nodes = append(nodes, n)
}
return nodes
}
func (list Nodes) updateFileName(dir string) {
type namer interface {
updateFileName(dir string)
}
for _, n := range list {
if n, ok := n.(namer); ok {
n.updateFileName(dir)
}
n.Children().updateFileName(dir)
}
}
func ToNodes[T Node](list []T) Nodes {
nodes := make(Nodes, len(list))
for i, n := range list {
nodes[i] = n
}
return nodes
}
func (list Nodes) Format(f fmt.State, verb rune) {
switch verb {
case 'v':
for _, n := range list {
fmt.Fprintf(f, "%v", n)
n.Children().Format(f, verb)
}
default:
fmt.Fprintf(f, "%s", list.String())
}
}