From eddee31071de701f1fcd861c8036a709a19a1f73 Mon Sep 17 00:00:00 2001 From: azimut Date: Wed, 8 Sep 2021 14:01:50 -0300 Subject: [PATCH] twitter format --- internal/twitter/format.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/internal/twitter/format.go b/internal/twitter/format.go index 9fcbc22..6f04b53 100644 --- a/internal/twitter/format.go +++ b/internal/twitter/format.go @@ -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("(.*)

") +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 }