-
Notifications
You must be signed in to change notification settings - Fork 11
/
html_tree.go
92 lines (77 loc) · 2.49 KB
/
html_tree.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
package golibxml
/*
#cgo pkg-config: libxml-2.0
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.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"
////////////////////////////////////////////////////////////////////////////////
// TYPES/STRUCTS
////////////////////////////////////////////////////////////////////////////////
type HTMLNode struct {
*Node
Ptr C.htmlNodePtr
}
////////////////////////////////////////////////////////////////////////////////
// INTERFACE
////////////////////////////////////////////////////////////////////////////////
// htmlGetMetaEncoding
func (doc *HTMLDocument) GetMetaEncoding() string {
cstr := C.htmlGetMetaEncoding(doc.Ptr)
return C.GoString(C.to_charptr(cstr))
}
// htmlIsBooleanAttr
func IsBooleanAttr(name string) bool {
ptr := C.CString(name)
defer C.free_string(ptr)
return C.htmlIsBooleanAttr(C.to_xmlcharptr(ptr)) != 0
}
// htmlNewDoc
func NewHTMLDoc(uri string, external_id string) *HTMLDocument {
ptru := C.CString(uri)
defer C.free_string(ptru)
ptre := C.CString(external_id)
defer C.free_string(ptre)
doc := C.htmlNewDoc(C.to_xmlcharptr(ptru), C.to_xmlcharptr(ptre))
return makeHTMLDoc(doc)
}
// htmlNewDocNoDtD
func NewHTMLDocNoDtd() *HTMLDocument {
doc := C.htmlNewDocNoDtD(nil, nil)
return makeHTMLDoc(doc)
}
// htmlNodeDump
func (doc *HTMLDocument) NodeDump(buf *Buffer, cur *HTMLNode) int {
return int(C.htmlNodeDump(buf.Ptr, doc.Ptr, cur.Ptr))
}
// htmlSaveFile
func (doc *HTMLDocument) SaveFile(filename string) int {
ptrf := C.CString(filename)
defer C.free_string(ptrf)
return int(C.htmlSaveFile(ptrf, doc.Ptr))
}
// htmlSaveFileEnc
func (doc *HTMLDocument) SaveFileEnc(filename string, encoding string) int {
ptrf := C.CString(filename)
defer C.free_string(ptrf)
ptre := C.CString(encoding)
defer C.free_string(ptre)
return int(C.htmlSaveFileEnc(ptrf, doc.Ptr, ptre))
}
// htmlSaveFileFormat
func (doc *HTMLDocument) SaveFileFormat(filename string, encoding string, format int) int {
ptrf := C.CString(filename)
defer C.free_string(ptrf)
ptre := C.CString(encoding)
defer C.free_string(ptre)
return int(C.htmlSaveFileFormat(ptrf, doc.Ptr, ptre, C.int(format)))
}
// htmlSetMetaEncoding
func (doc *HTMLDocument) SetMetaEncoding(encoding string) int {
ptr := C.CString(encoding)
defer C.free_string(ptr)
return int(C.htmlSetMetaEncoding(doc.Ptr, C.to_xmlcharptr(ptr)))
}