-
Notifications
You must be signed in to change notification settings - Fork 21
/
html_test.go
133 lines (116 loc) · 3.35 KB
/
html_test.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package parsec
import "fmt"
import "bytes"
import "testing"
import "net/http"
import "io/ioutil"
func TestHTMLValue(t *testing.T) {
data, err := ioutil.ReadFile("testdata/simple.html")
if err != nil {
t.Error(err)
}
data = bytes.Trim(data, " \t\r\n")
ast := NewAST("html", 100)
y := makehtmly(ast)
s := NewScanner(data).TrackLineno()
root, _ := ast.Parsewith(y, s)
ref := `<html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>`
if out := string(root.GetValue()); out != ref {
t.Errorf("expected %q", ref)
t.Errorf("got %q", out)
}
// To generate the dot-graph for input html.
//graph := ast.Dotstring("simplehtml")
//fmt.Println(graph)
// To gather all TEXT values.
//ch := make(chan Queryable, 100)
//ast.Query("TEXT", ch)
//for node := range ch {
// fmt.Println(node.GetValue())
//}
// To gather all terminal values.
ch := make(chan Queryable, 100)
ast.Query(".term", ch)
for node := range ch {
fmt.Printf("%s", node.GetValue())
}
fmt.Println()
}
func TestExample(t *testing.T) {
ast := NewAST("html", 100)
y := makehtmly(ast)
resp, err := http.Get("https://example.com/")
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
fmt.Println(string(data))
s := NewScanner(data).TrackLineno()
ast.Parsewith(y, s)
ch := make(chan Queryable, 100)
go ast.Query("attrunquoted,attrsingleq,attrdoubleq", ch)
for node := range ch {
cs := node.GetChildren()
if cs[0].GetValue() != "href" {
continue
}
if len(cs) == 3 {
fmt.Println(cs[2].GetValue())
} else {
fmt.Println(cs[3].GetValue())
}
}
fmt.Println()
}
func makehtmly(ast *AST) Parser {
var tag Parser
// terminal parsers.
tagobrk := Atom("<", "OT")
tagcbrk := Atom(">", "CT")
tagcend := Atom("/>", "CT")
tagcopen := Atom("</", "CT")
equal := Atom(`=`, "EQ")
single := Atom("'", "SQUOTE")
double := Atom(`"`, "DQUOTE")
tagname := Token(`[a-zA-Z0-9]+`, "TAGNAME")
attrname := Token(`[a-zA-Z0-9_-]+`, "ATTRNAME")
attrval1 := Token(`[^\s"'=<>`+"`]+", "ATTRVAL1")
attrval2 := Token(`[^']*`, "ATTRVAL2")
attrval3 := Token(`[^"]*`, "ATTRVAL3")
entity := Token(`&#?[a-bA-Z0-9]+;`, "ENTITY")
text := Token(`[^<]+`, "TEXT")
doctype := Token(`<!doctype[^>]+>`, "DOCTYPE")
// non-terminals
attrunquoted := ast.And(
"attrunquoted", nil, attrname, equal, attrval1,
)
attrsingleq := ast.And(
"attrsingleq", nil, attrname, equal, single, attrval2, single,
)
attrdoubleq := ast.And(
"attrdoubleq", nil, attrname, equal, double, attrval3, double,
)
attr := ast.OrdChoice(
"attribute", nil, attrsingleq, attrdoubleq, attrunquoted, attrname,
)
attrs := ast.Kleene("attributes", nil, attr, nil)
tagopen := ast.And("tagopen", nil, tagobrk, tagname, attrs, tagcbrk)
tagclose := ast.And("tagclose", nil, tagcopen, tagname, tagcbrk)
content := ast.OrdChoice("content", nil, entity, text, &tag)
contents := ast.Maybe(
"maybecontents", nil, ast.Kleene("contents", nil, content, nil),
)
tagempty := ast.And("tagempty", nil, tagobrk, tagname, attrs, tagcend)
tagproper := ast.And("tagproper", nil, tagopen, contents, tagclose)
tag = ast.OrdChoice("tag", nil, doctype, tagempty, tagproper)
return ast.Kleene("html", nil, tag, nil)
}
func debugfn(name string, s Scanner, node Queryable) Queryable {
attrs := node.GetChildren()[2]
fmt.Printf("%T %v\n", attrs, attrs)
return node
}