diff --git a/go.mod b/go.mod index d086d93..11adc62 100644 --- a/go.mod +++ b/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 ) diff --git a/go.sum b/go.sum index 13ce5ab..a980bfb 100644 --- a/go.sum +++ b/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= diff --git a/main.go b/main.go index ff24d98..5fed6ba 100644 --- a/main.go +++ b/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("#%s", hashtag.Text, hashtag.Text)) } + tweet.FullText = strings.ReplaceAll(tweet.FullText, "\n", "
") + 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, "
") * 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( diff --git a/tweet.svg.tmpl b/tweet.svg.tmpl index 81a40bc..a9971a8 100644 --- a/tweet.svg.tmpl +++ b/tweet.svg.tmpl @@ -1,5 +1,5 @@ - - + +