Skip to content

Commit

Permalink
twitter format
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Sep 8, 2021
1 parent bdc1126 commit eddee31
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions internal/twitter/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@ package twitter

import (
"fmt"
"regexp"
"strings"

text "github.com/MichaelMure/go-term-text"
"github.com/PuerkitoBio/goquery"
"jaytaylor.com/html2text"
)

func enrich(html string) (md string) {
func Format(t *Embedded) (formatted string) {
formatted += fmt.Sprintf("URL: %s\n", t.Url)
formatted += "\n" + fitInScreen(plaintext(paragraph(t.Html)))
return
}

func fitInScreen(s string) string {
s, _ = text.WrapLeftPadded(s, 100, 3)
return s
}

func plaintext(html string) string {
md, err := html2text.FromString(html)
if err != nil {
panic(err)
}
return
return md
}

func extract(html string) (p string) {
r, err := regexp.Compile("<p.*>(.*)</p>")
func paragraph(html string) string {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
panic(err)
}
return r.FindStringSubmatch(html)[0]
}

func Format(t *Embedded) (formatted string) {
formatted += fmt.Sprintf("URL: %s\n", t.Url)
formatted += enrich(extract(t.Html))
formatted += fmt.Sprintf("> %s\n", t.AuthorName)
return
doc.Find("p a").Remove()
msg, err := doc.Find("p").Html()
if err != nil {
panic(err)
}
return msg
}

0 comments on commit eddee31

Please sign in to comment.