Set SVG height
This commit is contained in:
parent
7d6428b0da
commit
7fe956435a
1
go.mod
1
go.mod
@ -9,6 +9,7 @@ require (
|
||||
github.com/dustin/go-jsonpointer v0.0.0-20160814072949-ba0abeacc3dc // indirect
|
||||
github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad // indirect
|
||||
github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17 // indirect
|
||||
github.com/grokify/html-strip-tags-go v0.0.1 // indirect
|
||||
github.com/joho/godotenv v1.3.0
|
||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@ -10,6 +10,8 @@ github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad h1:Qk76DOWdOp+GlyDKB
|
||||
github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad/go.mod h1:mPKfmRa823oBIgl2r20LeMSpTAteW5j7FLkc0vjmzyQ=
|
||||
github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17 h1:GOfMz6cRgTJ9jWV0qAezv642OhPnKEG7gtUjJSdStHE=
|
||||
github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17/go.mod h1:HfkOCN6fkKKaPSAeNq/er3xObxTW4VLeY6UUK895gLQ=
|
||||
github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0=
|
||||
github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
|
||||
|
33
main.go
33
main.go
@ -5,7 +5,9 @@ import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
@ -14,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ChimeraCoder/anaconda"
|
||||
strip "github.com/grokify/html-strip-tags-go"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
@ -50,7 +53,14 @@ func main() {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
http.HandleFunc("/", getTweet)
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if len(r.URL.Path) > 1 {
|
||||
getTweet(w, r)
|
||||
} else {
|
||||
body, _ := ioutil.ReadFile("index.html")
|
||||
w.Write(body)
|
||||
}
|
||||
})
|
||||
fmt.Println("Server started at port " + port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
}
|
||||
@ -87,6 +97,8 @@ func getTweet(w http.ResponseWriter, r *http.Request) {
|
||||
tweet.FullText = strings.ReplaceAll(tweet.FullText, "#"+hashtag.Text, fmt.Sprintf("<a rel=\"noopener\" target=\"_blank\" href=\"https://twitter.com/hashtag/%s\">#%s</a>", hashtag.Text, hashtag.Text))
|
||||
}
|
||||
|
||||
tweet.FullText = strings.ReplaceAll(tweet.FullText, "\n", "<br />")
|
||||
|
||||
templateFuncs := template.FuncMap{
|
||||
"base64": func(url string) string {
|
||||
res, err := http.Get(url)
|
||||
@ -108,6 +120,25 @@ func getTweet(w http.ResponseWriter, r *http.Request) {
|
||||
"html": func(in string) template.HTML {
|
||||
return template.HTML(in)
|
||||
},
|
||||
"calculateHeight": func(tweet anaconda.Tweet) string {
|
||||
height := 205.0
|
||||
|
||||
lines := math.Floor(float64(len(strip.StripTags(tweet.FullText))) / 40)
|
||||
height += lines * 20
|
||||
|
||||
if tweet.InReplyToScreenName != "" {
|
||||
height += 45
|
||||
}
|
||||
|
||||
height += float64(strings.Count(tweet.FullText, "<br />") * 20)
|
||||
|
||||
for _, pic := range tweet.ExtendedEntities.Media {
|
||||
ratio := float64(pic.Sizes.Small.W) / 464
|
||||
height += float64(pic.Sizes.Small.H) / ratio
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%dpx", int64(height))
|
||||
},
|
||||
}
|
||||
|
||||
t := template.Must(
|
||||
|
@ -1,5 +1,5 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="32em">
|
||||
<foreignObject x="0" y="0" width="32em" height="100%" fill="#eade52">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="499px" height="{{ calculateHeight . }}">
|
||||
<foreignObject x="0" y="0" width="499px" height="100%" fill="#eade52">
|
||||
<style>
|
||||
.tweetsvg{clear:none;font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;}
|
||||
.tweetsvg.text{font-size: 23px;}
|
||||
|
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB |
Loading…
Reference in New Issue
Block a user