-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer_test.go
More file actions
36 lines (30 loc) · 967 Bytes
/
tokenizer_test.go
File metadata and controls
36 lines (30 loc) · 967 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
package GoHtml_test
import (
"fmt"
"net/http"
GoHtml "github.com/udan-jayanith/GoHTML"
"golang.org/x/net/html"
)
func ExampleTokenizer() {
//Request the html
res, err := http.Get("https://go.dev/")
if err != nil || res.StatusCode != http.StatusOK {
return
}
defer res.Body.Close()
//NewTokenizer takes a io.reader that receives UTF-8 encoded html code and returns a Tokenizer.
t := GoHtml.NewTokenizer(res.Body)
//NewNodeTreeBuilder return a new NodeTreeBuilder that can be used to build a node tree.
nodeTreeBuilder := GoHtml.NewNodeTreeBuilder()
for {
//Advanced scans the next token and returns its type.
tt := t.Advanced()
if tt == html.ErrorToken {
break
}
//WriteNodeTree takes a node and a token type. The node can be nil so if token type is EndTagToken.
nodeTreeBuilder.WriteNodeTree(t.GetCurrentNode(), tt)
}
//Prints the root node of the node tree in the nodeTreeBuilder.
fmt.Println(nodeTreeBuilder.GetRootNode())
}