-
Notifications
You must be signed in to change notification settings - Fork 11
/
html_parser.go
184 lines (158 loc) · 5.2 KB
/
html_parser.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
package golibxml
/*
#cgo pkg-config: libxml-2.0
#include <libxml/HTMLparser.h>
static inline void free_string(char* s) { free(s); }
static inline xmlChar *to_xmlcharptr(const char *s) { return (xmlChar *)s; }
static inline char *to_charptr(const xmlChar *s) { return (char *)s; }
*/
import "C"
import "unsafe"
////////////////////////////////////////////////////////////////////////////////
// TYPES/STRUCTS
////////////////////////////////////////////////////////////////////////////////
type HTMLParserOption int
const (
HTML_PARSE_RECOVER HTMLParserOption = C.HTML_PARSE_RECOVER //: Relaxed parsing
HTML_PARSE_NODEFDTD = C.HTML_PARSE_NODEFDTD //: do not default a doctype if not found
HTML_PARSE_NOERROR = C.HTML_PARSE_NOERROR //: suppress error reports
HTML_PARSE_NOWARNING = C.HTML_PARSE_NOWARNING //: suppress warning reports
HTML_PARSE_PEDANTIC = C.HTML_PARSE_PEDANTIC //: pedantic error reporting
HTML_PARSE_NOBLANKS = C.HTML_PARSE_NOBLANKS //: remove blank nodes
HTML_PARSE_NONET = C.HTML_PARSE_NONET //: Forbid network access
HTML_PARSE_NOIMPLIED = C.HTML_PARSE_NOIMPLIED //: Do not add implied html/body... elements
HTML_PARSE_COMPACT = C.HTML_PARSE_COMPACT //: compact small text nodes
)
type ElemDesc struct {
Ptr C.htmlElemDescPtr
}
type HTMLDocument struct {
*Document
*HTMLNode
Ptr C.htmlDocPtr
}
type HTMLParser struct {
Ptr C.htmlParserCtxtPtr
}
////////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
func makeHTMLDoc(doc C.htmlDocPtr) *HTMLDocument {
if doc == nil {
return nil
}
return &HTMLDocument{
Ptr: doc,
Document: &Document{
Ptr: C.xmlDocPtr(doc),
Node: &Node{C.xmlNodePtr(unsafe.Pointer(doc))},
},
HTMLNode: &HTMLNode{
Ptr: C.htmlNodePtr(unsafe.Pointer(doc)),
Node: &Node{C.xmlNodePtr(unsafe.Pointer(doc))},
},
}
}
func makeHTMLParser(parser C.htmlParserCtxtPtr) *HTMLParser {
if parser == nil {
return nil
}
return &HTMLParser{parser}
}
func makeElemDesc(desc C.htmlElemDescPtr) *ElemDesc {
if desc == nil {
return nil
}
return &ElemDesc{desc}
}
////////////////////////////////////////////////////////////////////////////////
// INTERFACE
////////////////////////////////////////////////////////////////////////////////
// htmlAutoCloseTag
func (doc *HTMLDocument) AutoCloseTag(name string, node *Node) bool {
ptr := C.CString(name)
defer C.free_string(ptr)
return int(C.htmlAutoCloseTag(doc.Document.Ptr, C.to_xmlcharptr(ptr), node.Ptr)) == 1
}
// htmlCtxtReadDoc
func (p *HTMLParser) ReadDoc(input string, url string, encoding string, options ParserOption) *HTMLDocument {
ptri := C.CString(input)
defer C.free_string(ptri)
ptru := C.CString(url)
defer C.free_string(ptru)
ptre := C.CString(encoding)
defer C.free_string(ptre)
doc := C.htmlCtxtReadDoc(p.Ptr, C.to_xmlcharptr(ptri), ptru, ptre, C.int(options))
return makeHTMLDoc(doc)
}
// htmlCtxtReset
func (p *HTMLParser) Reset() {
C.htmlCtxtReset(p.Ptr)
}
// htmlCtxtUseOptions
func (p *HTMLParser) UseOptions(options HTMLParserOption) int {
return int(C.htmlCtxtUseOptions(p.Ptr, C.int(options)))
}
// htmlFreeParserCtxt
func (p *HTMLParser) Free() {
C.htmlFreeParserCtxt(p.Ptr)
}
// htmlNewParserCtxt
func NewHTMLParserCtxt() *HTMLParser {
pctx := C.htmlNewParserCtxt()
return makeHTMLParser(pctx)
}
// htmlParseDoc
func ParseHTMLDoc(cur string, encoding string) *HTMLDocument {
ptrc := C.CString(cur)
defer C.free_string(ptrc)
ptre := C.CString(encoding)
defer C.free_string(ptre)
doc := C.htmlParseDoc(C.to_xmlcharptr(ptrc), ptre)
return makeHTMLDoc(doc)
}
// htmlParseFile
func ParseHTMLFile(filename string, encoding string) *HTMLDocument {
ptrf := C.CString(filename)
defer C.free_string(ptrf)
ptre := C.CString(encoding)
defer C.free_string(ptre)
doc := C.htmlParseFile(ptrf, ptre)
return makeHTMLDoc(doc)
}
// htmlReadDoc
func ReadHTMLDoc(cur string, url string, encoding string, options HTMLParserOption) *HTMLDocument {
ptrc := C.CString(cur)
defer C.free_string(ptrc)
ptru := C.CString(url)
defer C.free_string(ptru)
ptre := C.CString(encoding)
defer C.free_string(ptre)
doc := C.htmlReadDoc(C.to_xmlcharptr(ptrc), ptru, ptre, C.int(options))
return makeHTMLDoc(doc)
}
// htmlReadFile
func ReadHTMLFile(filename string, encoding string, options HTMLParserOption) *HTMLDocument {
ptrf := C.CString(filename)
defer C.free_string(ptrf)
ptre := C.CString(encoding)
defer C.free_string(ptre)
doc := C.htmlReadFile(ptrf, ptre, C.int(options))
return makeHTMLDoc(doc)
}
// htmlReadMemory
func ReadHTMLMemory(buffer []byte, url string, encoding string, options HTMLParserOption) *HTMLDocument {
ptru := C.CString(url)
defer C.free_string(ptru)
ptre := C.CString(encoding)
defer C.free_string(ptre)
doc := C.htmlReadMemory((*C.char)(unsafe.Pointer(&buffer[0])), C.int(len(buffer)), ptru, ptre, C.int(options))
return makeHTMLDoc(doc)
}
// htmlTagLookup
func TagLookup(tag string) *ElemDesc {
ptr := C.CString(tag)
defer C.free_string(ptr)
cdesc := C.htmlTagLookup(C.to_xmlcharptr(ptr))
return makeElemDesc(cdesc)
}