-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame_html.go
85 lines (74 loc) · 2.04 KB
/
frame_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
package main
import (
"bytes"
"fmt"
"strings"
"text/template"
)
const frameTemplate = `
<!DOCTYPE html>
<html>
<head>
<meta property="fc:frame" content="vNext" />
<meta property="fc:frame:post_url" content="{{.PostURL}}" />
<meta property="fc:frame:image" content="{{.ImageURL}}" />
<meta property="og:image" content="{{.ImageURL}}" />
{{range $idx, $element := .Buttons}}
{{$buttonIdx := inc $idx}}
<meta property="fc:frame:button:{{$buttonIdx}}" content="{{$element.Label}}" />
<meta property="fc:frame:button:{{$buttonIdx}}:action" content="{{$element.Action}}" />
{{if ne $element.Target ""}}
<meta property="fc:frame:button:{{$buttonIdx}}:target" content="{{$element.Target}}" />
{{end}}
{{end}}
{{if ne .Input ""}}
<meta property="fc:frame:input:text" content="{{.Input}}" />
{{end}}
<title>Faxcaster</title>
</head>
<body>
<img src="{{.ImageURL}}" alt="{{.ImageAlt}}" />
</body>
</html>
`
type FrameData struct {
PostURL string
ImageURL string
ImageAlt string
Frame
}
var funcMap = template.FuncMap{
"inc": func(i int) int {
return i + 1
},
}
// stripNewlines replaces newlines with spaces in a string.
func stripNewlines(s string) string {
return strings.ReplaceAll(s, "\n", " ")
}
func generateFrameHTML(postURL string, frame Frame, values ...interface{}) (string, error) {
// parse the template with the frameTemplate string
tmpl, err := template.New("frame").Funcs(funcMap).Parse(frameTemplate)
if err != nil {
return "", err
}
// print the values to the frame text
text := fmt.Sprintf(frame.FormatText, values...)
// generate SVG
svg, err := textToSVG(text)
if err != nil {
return "", fmt.Errorf("failed to generate SVG: %w", err)
}
frameData := FrameData{
PostURL: postURL,
ImageURL: svg,
ImageAlt: stripNewlines(text),
Frame: frame,
}
// render the template to the ResponseWriter
var rendered bytes.Buffer
if err := tmpl.Execute(&rendered, frameData); err != nil {
return "", err
}
return rendered.String(), nil
}