-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathelements.go
237 lines (201 loc) · 5.75 KB
/
elements.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
"go.riyazali.net/sqlite"
"golang.org/x/net/html"
)
// A random "magic" number to use for sqlite subtypes.
// only the lower 8 bits are used. chose at random tbh
// https://www.sqlite.org/c3ref/result_subtype.html
var HTML_SUBTYPE = 0xdd
func subtypeIsHtml(value sqlite.Value) bool {
return value.SubType() == HTML_SUBTYPE
}
// https://github.com/sqlite/sqlite/blob/8b554e2a1ea4de0cb30a49357684836710f44905/ext/misc/json1.c#L159
const JSON_SUBTYPE = 74
/** html(document)
* Verifies and "cleans" (quotes attributes) the given document as HTML.
* Also sets the return subtype to the HTML magic number, for
* use in other funcs like html_element to designate something as "HTML"
**/
type HtmlFunc struct{}
func (*HtmlFunc) Deterministic() bool { return true }
func (*HtmlFunc) Args() int { return 1 }
func (*HtmlFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
html := values[0].Text()
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
c.ResultError(err)
return
}
// goquery seems to wrap with "<html><body>" stuff, so only get original
outHtml, err := doc.Find("body").Html()
if err != nil {
c.ResultError(err)
return
}
c.ResultText(outHtml)
c.ResultSubType(HTML_SUBTYPE)
}
/** html_element(tag, attributes, child1, ...)
* Create an HTML element with the given tag, attributes, and children.
* Modeled after React.createElement https://reactjs.org/docs/react-without-jsx.html
* @param tag {text} - required top-level tag name for the returned root element.
* @param attributes {json} - should be a json object with string keys/values, for the attributes of the element.
* @param children {text | html} - are either strings or html elements.
* If 'children" is a string, then it will be rendered as a TextNode in the top-level element
* If 'children" is an html element, from "html()" or "html_element()" then it will be rendered as a RawNode in the top-level element
*/
type HtmlElementFunc struct{}
func (*HtmlElementFunc) Deterministic() bool { return true }
func (*HtmlElementFunc) Args() int { return -1 }
func (*HtmlElementFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
var children []sqlite.Value
if len(values) < 1 {
c.ResultError(errors.New("html_element requires tag"))
}
tag := values[0].Text()
if len(values) >= 3 {
children = values[2:]
}
var attr []html.Attribute
if len(values) > 1 && values[1].Type() != sqlite.SQLITE_NULL {
rawAttrs := values[1].Text()
var attrs map[string]interface{}
if err := json.Unmarshal([]byte(rawAttrs), &attrs); err != nil {
c.ResultError(errors.New("attributes is not a JSON object"))
}
for k, v := range attrs {
var Val string
switch t := v.(type) {
case string:
Val = t
case int64:
Val = fmt.Sprintf("%d", t)
case float64:
Val = fmt.Sprintf("%f", t)
}
attr = append(attr, html.Attribute{
Key: k,
Val: Val,
})
}
}
root := &html.Node{
Type: html.ElementNode,
Data: tag,
Attr: attr,
}
for _, v := range children {
var child *html.Node
childData := v.Text()
if subtypeIsHtml(v) {
child = &html.Node{
Type: html.RawNode,
Data: childData,
}
} else {
child = &html.Node{
Type: html.TextNode,
Data: childData,
}
}
root.AppendChild(child)
}
var buf bytes.Buffer
html.Render(&buf, root)
c.ResultText(buf.String())
c.ResultSubType(HTML_SUBTYPE)
}
type HtmlGroupElementFunc struct{
parent string
}
func (h *HtmlGroupElementFunc) Args() int { return -1 }
func (h *HtmlGroupElementFunc) Deterministic() bool { return true }
type HtmlGroupElementContext struct {
root *html.Node
}
func (s *HtmlGroupElementFunc) Step(ctx *sqlite.AggregateContext, values ...sqlite.Value) {
if len(values) < 1 {
ctx.ResultError(errors.New("html_element requires tag"))
}
if ctx.Data() == nil {
root := &html.Node{
Type: html.ElementNode,
Data: s.parent,
Attr: nil,
}
ctx.SetData(&HtmlGroupElementContext{root:root,})
}
var gCtx = ctx.Data().(*HtmlGroupElementContext)
var children []sqlite.Value
if len(values) >= 3 {
children = values[2:]
}
var attr []html.Attribute
if len(values) > 1 && values[1].Type() != sqlite.SQLITE_NULL {
rawAttrs := values[1].Text()
var attrs map[string]string
if err := json.Unmarshal([]byte(rawAttrs), &attrs); err != nil {
ctx.ResultError(errors.New("attributes is not a JSON object"))
}
for k, v := range attrs {
attr = append(attr, html.Attribute{
Key: k,
Val: v,
})
}
}
node := &html.Node{
Type: html.ElementNode,
Data: values[0].Text(),
Attr: attr,
}
for _, v := range children {
var child *html.Node
childData := v.Text()
if subtypeIsHtml(v) {
child = &html.Node{
Type: html.RawNode,
Data: childData,
}
} else {
child = &html.Node{
Type: html.TextNode,
Data: childData,
}
}
node.AppendChild(child)
}
gCtx.root.AppendChild(node)
}
func (s *HtmlGroupElementFunc) Final(ctx *sqlite.AggregateContext) {
if ctx.Data() != nil {
var buf bytes.Buffer
var gCtx = ctx.Data().(*HtmlGroupElementContext)
html.Render(&buf, gCtx.root)
ctx.ResultText(buf.String())
ctx.ResultSubType(HTML_SUBTYPE)
}
}
func RegisterElements(api *sqlite.ExtensionApi) error {
var err error
if err = api.CreateFunction("html", &HtmlFunc{}); err != nil {
return err
}
if err = api.CreateFunction("html_element", &HtmlElementFunc{}); err != nil {
return err
}
if err = api.CreateFunction("html_group_element_div", &HtmlGroupElementFunc{parent: "div"}); err != nil {
return err
}
if err = api.CreateFunction("html_group_element_span", &HtmlGroupElementFunc{parent: "span"}); err != nil {
return err
}
return nil
}