-
Notifications
You must be signed in to change notification settings - Fork 7
/
html.go
199 lines (165 loc) · 3.83 KB
/
html.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package htmlPDF
import (
"fmt"
"regexp"
)
// Parse an HTML document and return the *Node
func ParseHtml(source string) *Node {
p := new(Parser)
p.input = source
p.pos = 0
nodes := p.parseNodes()
if len(nodes) > 1 {
panic("Not one root tag")
}
//return first node
return nodes[0]
}
func (p *Parser) parseNodes() map[int]*Node {
nodes := map[int]*Node{}
for {
p.consumeWhitespace()
if p.eof() || p.startWith("</") {
break
}
nodes[len(nodes)] = p.parseNode()
}
return nodes
}
//Parse a single node
func (p *Parser) parseNode() *Node {
if p.nextChar() == "<" {
return p.parseElement()
} else {
return p.parseText()
}
}
//Parse a single element, including contents(and childrens if exist)
func (p *Parser) parseElement() *Node {
//Opening tag
start := p.consumeChar()
if start != "<" {
panic(fmt.Sprintf("%v was not an openig tag <", start))
}
tagName := p.parseTagName()
attrs := p.parseAttributes()
end := p.consumeChar()
if end != ">" {
panic(fmt.Sprintf("%v was not a closing tag <", end))
}
//Parse children
children := p.parseNodes()
//Closing tag
start = p.consumeChar()
if start != "<" {
panic(fmt.Sprintf("%v was not an openig tag <", start))
}
slash := p.consumeChar()
if slash != "/" {
panic(fmt.Sprintf("%v was not a tag /", slash))
}
closeName := p.parseTagName()
if closeName != tagName {
panic(fmt.Sprintf("open tag %v and close tag %v don't equal ", tagName, closeName))
}
end = p.consumeChar()
if end != ">" {
panic(fmt.Sprintf("%v was not a closing tag <", end))
}
return elem(tagName, attrs, children)
}
//Parse a text node
func (p *Parser) parseText() *Node {
return text(p.consumeWhile(func(char string) bool {
return char != "<"
}))
}
//Parse a tag or attribute name
func (p *Parser) parseTagName() string {
reg := regexp.MustCompile("[a-zA-Z0-9]")
f := func(char string) bool {
return reg.MatchString(char)
}
return p.consumeWhile(f)
}
//Parse a list of name="value" pairs
func (p *Parser) parseAttributes() map[string]string {
attr := map[string]string{}
for {
p.consumeWhitespace()
if p.nextChar() == ">" {
break
}
name, value := p.parseAttribute()
attr[name] = value
}
return attr
}
//Parse a single name="value" pair
func (p *Parser) parseAttribute() (string, string) {
name := p.parseTagName()
delimiter := p.consumeChar()
if delimiter != "=" {
panic(fmt.Sprintf("%v was not =", delimiter))
}
value := p.parseAttributeValue()
return name, value
}
//Parse a quoted value
func (p *Parser) parseAttributeValue() string {
q := p.consumeChar()
if q != "\"" && q != "'" {
panic(fmt.Sprintf("%v was not \" or '", q))
}
value := p.consumeWhile(func(char string) bool {
return char != q
})
cq := p.consumeChar()
if cq != q {
panic(fmt.Sprintf("%v was not %v", cq, q))
}
return value
}
//Return true if current input start with the given string
func (p *Parser) startWith(test string) bool {
start := true
for i := 0; i < len(test); i++ {
if p.input[p.pos+i] != test[i] {
start = false
}
}
return start
}
//Consume characters until function 'test' returns false
func (p *Parser) consumeWhile(test func(char string) bool) string {
var result string
for {
if p.eof() || !test(p.nextChar()) {
break
}
result += p.consumeChar()
}
return result
}
//Consume and discard zero or more whitespace characters
func (p *Parser) consumeWhitespace() {
reg := regexp.MustCompile("[\\s]")
f := func(char string) bool {
return reg.MatchString(char)
}
p.consumeWhile(f)
}
//Return the current character with consuming it
func (p *Parser) consumeChar() string {
char := p.input[p.pos]
p.pos++
return string(char)
}
//Read the current character without consuming it
func (p *Parser) nextChar() string {
return string(p.input[p.pos])
}
//Return true if all input is consumed
func (p *Parser) eof() bool {
return p.pos >= len(p.input)
}