-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.go
100 lines (85 loc) · 2.86 KB
/
utils.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
package main
import (
"fmt"
"html"
"strings"
"github.com/PuerkitoBio/goquery"
"go.riyazali.net/sqlite"
)
/** html_valid(document)
* Returns 1 if the given document is valid HTML, 0 otherwise.
* @param document {text | html} - HTML document to read from.
**/
type HtmlValidFunc struct{}
func (*HtmlValidFunc) Deterministic() bool { return true }
func (*HtmlValidFunc) Args() int { return 1 }
func (*HtmlValidFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
html := values[0].Text()
_, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
c.ResultInt(0)
} else {
c.ResultInt(1)
}
}
/** html_escape(content)
* Returns an HTML escaped version of the given content.
* @param content {text} - Text content to escape.
**/
type HtmlEscapeFunc struct{}
func (*HtmlEscapeFunc) Deterministic() bool { return true }
func (*HtmlEscapeFunc) Args() int { return 1 }
func (*HtmlEscapeFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
c.ResultText(html.EscapeString(values[0].Text()))
}
/** html_unescape(content)
* Returns an HTML unescaped version of the given content.
* @param content {text} - Text content to unescape.
**/
type HtmlUnescapeFunc struct{}
func (*HtmlUnescapeFunc) Deterministic() bool { return true }
func (*HtmlUnescapeFunc) Args() int { return 1 }
func (*HtmlUnescapeFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
c.ResultText(html.UnescapeString(values[0].Text()))
}
/** html_trim(content)
* Trim whitespace around the given text content. Useful for output of html_text
* @param content {text} - Text content to trim.
**/
type HtmlTrimFunc struct{}
func (*HtmlTrimFunc) Deterministic() bool { return true }
func (*HtmlTrimFunc) Args() int { return 1 }
func (*HtmlTrimFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
s := values[0].Text()
c.ResultText(strings.TrimSpace(s))
}
/** html_table(content)
* Wrap the given content around a HTML table. Useful for parsing table rows.
* @param content {text} - Text content to wrap.
**/
type HtmlTableFunc struct{}
func (*HtmlTableFunc) Deterministic() bool { return true }
func (*HtmlTableFunc) Args() int { return 1 }
func (*HtmlTableFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
s := values[0].Text()
c.ResultText(fmt.Sprintf("<table>%s", s))
}
func RegisterUtils(api *sqlite.ExtensionApi) error {
var err error
if err = api.CreateFunction("html_valid", &HtmlValidFunc{}); err != nil {
return err
}
if err = api.CreateFunction("html_table", &HtmlTableFunc{}); err != nil {
return err
}
if err = api.CreateFunction("html_escape", &HtmlEscapeFunc{}); err != nil {
return err
}
if err = api.CreateFunction("html_unescape", &HtmlUnescapeFunc{}); err != nil {
return err
}
if err = api.CreateFunction("html_trim", &HtmlTrimFunc{}); err != nil {
return err
}
return nil
}