-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode.go
72 lines (61 loc) · 1.11 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
package htmlPDF
import (
"fmt"
"strings"
)
//Struct for Node tree
type Node struct {
children map[int]*Node
node_type NodeType
}
type NodeType struct {
element ElementData
text string
}
type ElementData struct {
tag_name string
attr map[string]string
}
func (e ElementData) id() string {
return e.attr["id"]
}
func (e ElementData) classes() []string {
class, ok := e.attr["class"]
if ok {
return strings.Split(class, " ")
}
return []string{}
}
func tab(i int) {
for j := 0; j < i; j++ {
fmt.Printf(" ")
}
}
func (n *Node) print(l int) {
tab(l)
l++
fmt.Printf("%s text: %s\n", n.node_type.element.tag_name, n.node_type.text)
for i := 0; i < len(n.children); i++ {
n.children[i].print(l + 1)
}
}
func text(data string) *Node {
return &Node{
children: map[int]*Node{},
node_type: NodeType{
element: ElementData{attr: map[string]string{}},
text: data,
},
}
}
func elem(name string, attrs map[string]string, children map[int]*Node) *Node {
return &Node{
children: children,
node_type: NodeType{
element: ElementData{
tag_name: name,
attr: attrs,
},
},
}
}