forked from lalluviamola/web-blog
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtool_gitoembed.go
247 lines (228 loc) · 6.63 KB
/
tool_gitoembed.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
238
239
240
241
242
243
244
245
246
247
package main
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"html/template"
"net/http"
"net/url"
"path/filepath"
"strings"
"time"
)
/*
Notes about oembed standard:
- https://oembed.com/
- https://blog.ycombinator.com/how-to-build-an-oembed-integration-for-your-startup-and-why-its-necessary/
*/
var (
oembedDownloadCache = NewHTTPDownloadCache()
//baseURL = "https://www.onlinetool.io"
baseURL = "https://blog.kowalczyk.info"
)
// json oembed response
// example response: https://api.clyp.it/oembed?url=https%3a%2f%2fclyp.it%2fzhwuptos&format=JSON
type oembedResult struct {
Version int64 `json:"version"`
Type string `json:"type"`
ProviderName string `json:"provider_name"`
ProviderURL string `json:"provider_url"`
Height interface{} `json:"height"` // can be a number like 256 or string like "100%"
Width interface{} `json:"width"`
Title string `json:"title"`
HTML string `json:"html"`
}
/*
<link rel="alternate" type="text/xml+oembed"
href="http://flickr.com/services/oembed?url=http%3A%2F%2Fflickr.com%2Fphotos%2Fbees%2F2362225867%2F&format=xml"
title="Bacon Lollys oEmbed Profile" />
*/
type oembedXMLResult struct {
XMLName xml.Name `xml:"oembed"`
Version string `xml:"version"`
Type string `xml:"type"`
Width interface{} `xml:"width"`
Height interface{} `xml:"height"` // can be a number like 256 or string like "100%"
Title string `xml:"title"`
URL string `xml:"url,omitempty"`
ProviderName string `json:"provider_name"`
ProviderURL string `json:"provider_url"`
HTML string `json:"html"`
}
// covnert
// https://github.com/essentialbooks/books/blob/master/netlify.toml
// =>
// https://raw.githubusercontent.com/essentialbooks/books/master/netlify.toml
// returns empty string if not a valid format
func getGitHubRawURL(uri string) string {
uri = strings.TrimPrefix(uri, "http://")
uri = strings.TrimPrefix(uri, "https://")
// this is because when copy&paste Chrome removes one of the "//"
uri = strings.TrimPrefix(uri, "https:/")
uri = strings.TrimPrefix(uri, "http:/")
parts := strings.SplitN(uri, "/", 5)
if len(parts) < 5 {
return ""
}
if parts[0] != "github.com" {
return ""
}
if parts[3] != "blob" {
return ""
}
newParts := []string{"raw.githubusercontent.com", parts[1], parts[2], parts[4]}
return "https://" + strings.Join(newParts, "/")
}
func getFileNameFromURL(uri string) string {
parts := strings.Split(uri, "/")
n := len(parts)
if n == 1 {
return uri
}
return parts[n-1]
}
type oembedQuery struct {
url string
theme string
format string
noLines bool
}
func parseOembedQuery(uri *url.URL) *oembedQuery {
res := &oembedQuery{}
query := uri.Query()
res.url = strings.TrimSpace(query.Get("url"))
res.format = strings.TrimSpace(query.Get("format"))
if _, ok := query["nolines"]; ok {
res.noLines = true
}
res.theme = strings.TrimSpace(query.Get("theme"))
return res
}
// /gitoembed/
// TODO: redirect to https://blog.kowalczyk.info/tools/gitoembed
func handleGitOembedIndex(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Path
if uri != "/gitoembed/" {
http.NotFound(w, r)
return
}
relativePath := filepath.Join("gitoembed", "index.html")
serveRelativeFile(w, r, relativePath)
}
// /gitoembed/widget?url=${GITURL}&nolines&theme=${theme}
// to test:
// http://127.0.0.1:8508/gitoembed/widget?https://github.com/kjk/programming-books.io/blob/master/books/go/0200-panic-and-recover/recover_from_panic.go?theme=github
func handleGitOembedWidget(w http.ResponseWriter, r *http.Request) {
args := parseOembedQuery(r.URL)
githubURL := args.url
if githubURL == "" {
http.NotFound(w, r)
return
}
rawURL := getGitHubRawURL(githubURL)
if rawURL == "" {
// not a valid url
http.NotFound(w, r)
return
}
query := "&url=" + githubURL
if args.noLines {
query += "&nolines"
}
if args.theme != "" {
query += "&theme" + args.theme
}
fileName := getFileNameFromURL(githubURL)
timeStart := time.Now()
d, fromCache, err := oembedDownloadCache.Download(rawURL)
dur := time.Since(timeStart)
ctx := context.Background()
if err != nil {
logf(ctx, "Failed to download %s. Time: %s. Error: %s\n", rawURL, dur, err)
http.NotFound(w, r)
return
}
if fromCache {
logf(ctx, "Got %s (%d bytes) from cache in %s\n", rawURL, len(d), dur)
} else {
logf(ctx, "Downloaded %s (%d bytes) in %s\n", rawURL, len(d), dur)
}
var buf bytes.Buffer
f := makeHTMLFormatter(args.noLines)
theme := validateTheme(args.theme)
err = codeHighlight(&buf, string(d), fileName, f, theme)
if err != nil {
logerrf(ctx, "quick.Highlight failed with %s\n", err)
return
}
data := struct {
Title string
OembedLinkJSON string
OembedLinkXML string
Body template.HTML
FileName string
OriginalURL string
ServiceURL string
}{
Title: fileName,
OembedLinkJSON: baseURL + "/gitoembed/oembed?format=json" + query,
OembedLinkXML: baseURL + "/gitoembed/oembed?format=xml" + query,
Body: template.HTML(buf.Bytes()),
FileName: fileName,
OriginalURL: githubURL,
ServiceURL: baseURL + "/tools/gitoembed/",
}
path := filepath.Join("gitoembed", "oembed.tmpl.html")
serveTemplate(w, r, path, data)
}
// /gitoembed/oembed?url=${url}&format=[json|xml]&nolines&theme=${theme}
func handleGitOembedOembed(w http.ResponseWriter, r *http.Request) {
args := parseOembedQuery(r.URL)
// url is mandatory
gitHubURL := args.url
if gitHubURL == "" {
http.NotFound(w, r)
return
}
format := "json"
if strings.EqualFold(args.format, "xml") {
format = "xml"
}
widgetURL := baseURL + "/gitoembed/widget?url=" + gitHubURL
if args.noLines {
widgetURL += "&nolines"
}
if args.theme != "" {
widgetURL += "&theme=" + args.theme
}
// oembed spec requires those are integers. Those are somewhat arbitrary values
width := 720
height := 320
html := fmt.Sprintf(`<iframe width="100%%" height=%v src="%s" frameborder="0" onload="resizeFrame(this);"></iframe>`, height, widgetURL)
if format == "json" {
resjs := oembedResult{
Version: 1,
Type: "rich",
ProviderName: "gitoembed",
ProviderURL: baseURL + "/gitoembed/",
Height: height,
Width: width,
Title: getFileNameFromURL(gitHubURL),
HTML: html,
}
httpOkWithJSON(w, r, resjs)
return
}
resxml := oembedXMLResult{
Version: "1.0",
Type: "rich",
ProviderName: "gitoembed",
ProviderURL: baseURL + "/gitoembed/",
Height: height,
Width: width,
Title: getFileNameFromURL(gitHubURL),
HTML: html,
}
httpOkWithXML(w, r, resxml)
}