Username as link in HTML

Line break before image in HTML
This commit is contained in:
Alexander Sheiko 2020-12-17 11:16:38 +02:00
parent 29ee72026a
commit 5c6a4dcc8d

View file

@ -13,6 +13,7 @@ import (
var ( var (
reHashtag = regexp.MustCompile(`\B(\#[a-zA-Z]+\b)`) reHashtag = regexp.MustCompile(`\B(\#[a-zA-Z]+\b)`)
reTwitterURL = regexp.MustCompile(`https:(\/\/t\.co\/([A-Za-z0-9]|[A-Za-z]){10})`) reTwitterURL = regexp.MustCompile(`https:(\/\/t\.co\/([A-Za-z0-9]|[A-Za-z]){10})`)
reUsername = regexp.MustCompile(`\B(\@[a-zA-Z]+\b)`)
) )
func (s *Scraper) newRequest(method string, url string) (*http.Request, error) { func (s *Scraper) newRequest(method string, url string) (*http.Request, error) {
@ -166,6 +167,12 @@ func parseTimeline(timeline *timeline) ([]*Tweet, string) {
hashtag, hashtag,
) )
}) })
tw.HTML = reUsername.ReplaceAllStringFunc(tw.HTML, func(username string) string {
return fmt.Sprintf(`<a href="https://twitter.com/%s">%s</a>`,
strings.TrimPrefix(username, "@"),
username,
)
})
tw.HTML = reTwitterURL.ReplaceAllStringFunc(tw.HTML, func(tco string) string { tw.HTML = reTwitterURL.ReplaceAllStringFunc(tw.HTML, func(tco string) string {
for _, entity := range tweet.Entities.URLs { for _, entity := range tweet.Entities.URLs {
if tco == entity.URL { if tco == entity.URL {
@ -174,7 +181,7 @@ func parseTimeline(timeline *timeline) ([]*Tweet, string) {
} }
for _, entity := range tweet.Entities.Media { for _, entity := range tweet.Entities.Media {
if tco == entity.URL { if tco == entity.URL {
return fmt.Sprintf(`<a href="%s"><img src="%s"/></a>`, tco, entity.MediaURLHttps) return fmt.Sprintf(`<br><a href="%s"><img src="%s"/></a>`, tco, entity.MediaURLHttps)
} }
} }
return tco return tco