-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.go
196 lines (174 loc) · 7.32 KB
/
render.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
package enclave
import (
"fmt"
"github.com/quail-ink/goldmark-enclave/core"
"github.com/quail-ink/goldmark-enclave/object"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
type HTMLRenderer struct {
cfg *core.Config
}
func NewHTMLRenderer(cfg *core.Config) renderer.NodeRenderer {
r := &HTMLRenderer{cfg: cfg}
return r
}
func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
// image with alt like [alt](url "title") will generate a node seq like
// layout:
// - imgLeftNode: kind = paragraph, content = alt
// - imgNode: kind = image
// - imgRightNode: kind = text, content = alt
// I don't know how to handle them yet.
reg.Register(core.KindEnclave, r.renderEnclave)
}
func (r *HTMLRenderer) renderEnclave(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
return ast.WalkContinue, nil
}
enc := node.(*core.Enclave)
switch enc.Provider {
case core.EnclaveProviderYouTube:
{
// if enc.IframeDisabled {
// w.Write([]byte(`
// <div class="enclave-object-wrapper">
// <a href="https://www.youtube.com/watch?v=` + enc.ObjectID + `" target="_blank" rel="noopener noreferrer">
// <img src="https://img.youtube.com/vi/` + enc.ObjectID + `/maxresdefault.jpg" title="YouTube video player" />
// </a>
// </div>`))
// } else {
// w.Write([]byte(`
// <div class="enclave-object-wrapper">
// <iframe class="enclave-object youtube-enclave-object" width="100%" height="400" src="https://www.youtube.com/embed/` + enc.ObjectID + `" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
// </div>`))
// }
html, err := object.GetYoutubeEmbedHtml(enc)
if err != nil || html == "" {
html = r.wrapEnclaveErrorHtml("youtube", enc.ObjectID)
} else {
html = r.wrapEnclaveHtml("youtube", html, false, false)
}
w.Write([]byte(html))
}
case core.EnclaveProviderBilibili:
{
html, err := object.GetBilibiliEmbedHtml(enc)
if err != nil || html == "" {
html = r.wrapEnclaveErrorHtml("bilibili", enc.ObjectID)
} else {
html = r.wrapEnclaveHtml("bilibili", html, false, false)
}
w.Write([]byte(html))
}
case core.EnclaveProviderTwitter:
html, err := object.GetTweetOembedHtml(enc.ObjectID, enc.Theme)
if err != nil || html == "" {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object twitter-enclave-object normal-object error">Failed to load tweet from %s</div></div>`, enc.ObjectID)
html = r.wrapEnclaveErrorHtml("twitter", enc.ObjectID)
} else {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object twitter-enclave-object normal-object no-border">%s</div></div>`, html)
html = r.wrapEnclaveHtml("twitter", html, true, false)
}
w.Write([]byte(html))
case core.EnclaveProviderTradingView:
html, err := object.GetTradingViewWidgetHtml(enc)
if err != nil || html == "" {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object tradingview-enclave-object error">Failed to load tradingview chart from %s</div></div>`, enc.ObjectID)
html = r.wrapEnclaveErrorHtml("tradingview", enc.ObjectID)
} else {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper auto-resize"><div class="enclave-object tradingview-enclave-object no-border">%s</div></div>`, html)
html = r.wrapEnclaveHtml("tradingview", html, false, false)
}
w.Write([]byte(html))
case core.EnclaveProviderDifyWidget:
html, err := object.GetDifyWidgetHtml(enc)
if err != nil || html == "" {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object dify-enclave-object error">Failed to load dify widget from %s</div></div>`, enc.ObjectID)
html = r.wrapEnclaveErrorHtml("dify", enc.ObjectID)
} else {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object dify-enclave-object normal-object no-border">%s</div></div>`, html)
html = r.wrapEnclaveHtml("dify", html, true, false)
}
w.Write([]byte(html))
case core.EnclaveProviderQuailWidget:
html, err := object.GetQuailWidgetHtml(enc)
if err != nil || html == "" {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object quail-enclave-object error">Failed to load quail widget from %s</div></div>`, enc.ObjectID)
html = r.wrapEnclaveErrorHtml("quail", enc.ObjectID)
} else {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object quail-enclave-object normal-object no-border">%s</div></div>`, html)
html = r.wrapEnclaveHtml("quail", html, true, false)
}
w.Write([]byte(html))
case core.EnclaveProviderSpotify:
html, err := object.GetSpotifyWidgetHtml(enc)
if err != nil || html == "" {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object spotify-enclave-object error">Failed to load spotify widget from %s</div></div>`, enc.ObjectID)
html = r.wrapEnclaveErrorHtml("spotify", enc.ObjectID)
} else {
// html = fmt.Sprintf(`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object spotify-enclave-object normal-object no-border">%s</div></div>`, html)
html = r.wrapEnclaveHtml("spotify", html, true, false)
}
w.Write([]byte(html))
case core.EnclaveHtml5Audio:
html, err := object.GetAudioHtml(enc)
if err != nil || html == "" {
html = r.wrapEnclaveErrorHtml("audio", enc.ObjectID)
} else {
html = r.wrapEnclaveHtml("audio", html, true, false)
}
w.Write([]byte(html))
case core.EnclaveProviderQuailImage:
alt := string(node.Text(source))
if alt == "" && len(enc.Title) != 0 {
alt = fmt.Sprintf("An image to describe %s", enc.Title)
}
if alt == "" {
alt = "An image to describe post"
}
enc.Params["alt"] = alt
html, err := object.GetQuailImageHtml(enc.URL, enc.Params)
if err != nil || html == "" {
html = r.wrapEnclaveErrorHtml("quail-image", enc.ObjectID)
}
w.Write([]byte(html))
case core.EnclaveRegularImage:
alt := string(node.Text(source))
if alt == "" && len(enc.Title) != 0 {
alt = fmt.Sprintf("An image to describe %s", enc.Title)
}
if alt == "" {
alt = "An image to describe post"
}
html := fmt.Sprintf(`<img src="%s" alt="%s" />`, enc.URL.String(), alt)
w.Write([]byte(html))
}
return ast.WalkContinue, nil
}
func (r *HTMLRenderer) wrapEnclaveErrorHtml(enclaveName, objectID string) string {
html := fmt.Sprintf(
`<div class="enclave-object-wrapper normal-wrapper"><div class="enclave-object %s-enclave-object error">Failed to load %s from %s</div></div>`,
enclaveName, enclaveName, objectID,
)
return html
}
func (r *HTMLRenderer) wrapEnclaveHtml(enclaveName, html string, isNormal, hasBorder bool) string {
normalCls := ""
borderCls := ""
autoResizeCls := "normal-wrapper"
if isNormal {
normalCls = "normal-object"
} else {
autoResizeCls = "auto-resize"
}
if !hasBorder {
borderCls = "no-border"
}
ret := fmt.Sprintf(
`<div class="enclave-object-wrapper %s"><div class="enclave-object %s-enclave-object %s %s">%s</div></div>`,
autoResizeCls, enclaveName, normalCls, borderCls, html,
)
return ret
}