Add emoji favicons
alex wennerberg alex@alexwennerberg.com
Thu, 05 Nov 2020 18:50:07 -0800
3 files changed,
32 insertions(+),
4 deletions(-)
M
go.mod
→
go.mod
@@ -3,7 +3,7 @@
go 1.15 require ( - git.sr.ht/~adnano/go-gemini v0.1.6-0.20201105050458-53390dad6bb1 // indirect + git.sr.ht/~adnano/go-gemini v0.1.6-0.20201105050458-53390dad6bb1 github.com/BurntSushi/toml v0.3.1 github.com/gorilla/handlers v1.5.1 github.com/gorilla/sessions v1.2.1
M
http.go
→
http.go
@@ -390,6 +390,19 @@ return
} } +func getFavicon(user string) string { + faviconPath := path.Join(c.FilesDirectory, filepath.Clean(user), "favicon.txt") + content, err := ioutil.ReadFile(faviconPath) + if err != nil { + return "" + } + strcontent := []rune(string(content)) + if len(strcontent) > 0 { + return string(strcontent[0]) + } + return "" +} + // Server a user's file func userFile(w http.ResponseWriter, r *http.Request) { userName := filepath.Clean(strings.Split(r.Host, ".")[0]) // clean probably unnecessary@@ -413,10 +426,13 @@ }
file, _ := os.Open(fileName) htmlString := textToHTML(gmi.ParseText(file)) + favicon := getFavicon(userName) + log.Println(favicon) data := struct { SiteBody template.HTML + Favicon string PageTitle string - }{template.HTML(htmlString), userName} + }{template.HTML(htmlString), favicon, userName} t.ExecuteTemplate(w, "user_page.html", data) } else { http.ServeFile(w, r, fileName)
M
templates/user_page.html
→
templates/user_page.html
@@ -1,3 +1,15 @@
-{{template "header" .}} +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8" /> + <title>{{.PageTitle }}</title> + <meta name="viewport" content="width=device-width" /> + <link rel="stylesheet" type="text/css" href="/style.css" /> + <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>{{.Favicon}}</text></svg>"> + </head> + <body> +<main> {{.SiteBody}} -{{template "footer" .}} +</main> +</body> +</html>