-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
41 lines (35 loc) · 892 Bytes
/
parser.go
File metadata and controls
41 lines (35 loc) · 892 Bytes
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
package GoHtml
import (
"errors"
"io"
"strings"
"golang.org/x/net/html"
)
var (
NoNodesFound error = errors.New("No nodes found in the node tree")
)
// Decode reads from rd and create a node-tree. Then returns the root node and nil.
// If no nodes are found Decode returns a error.
// r must no be nil.
func Decode(r io.Reader) (*Node, error) {
t := NewTokenizer(r)
nodeTreeBuilder := NewNodeTreeBuilder()
for {
tt := t.Advanced()
if tt == html.ErrorToken {
break
}
nodeTreeBuilder.WriteNodeTree(t.GetCurrentNode(), tt)
}
rootNode := nodeTreeBuilder.GetRootNode()
if rootNode == nil {
return nil, NoNodesFound
}
return rootNode, nil
}
// HTMLToNodeTree return html code as a node-tree. If error were to occur it would be SyntaxError.
func HTMLToNodeTree(html string) (*Node, error) {
rd := strings.NewReader(html)
node, err := Decode(rd)
return node, err
}