From b38b1d3028bb469b95e2f0a6f33b5fef5d9c2b60 Mon Sep 17 00:00:00 2001 From: Rasmus Lindroth Date: Sun, 22 Jan 2023 15:05:20 +0100 Subject: [PATCH] add more style --- config/keys.go | 8 ++++---- ui/item_status.go | 6 ++---- util/util.go | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/config/keys.go b/config/keys.go index 860e6d2..4095b72 100644 --- a/config/keys.go +++ b/config/keys.go @@ -32,10 +32,6 @@ func ColorKey(c *Config, pre, key, end string) string { return text } -func ColorMark(color tcell.Color) string { - return fmt.Sprintf("[#%06x]", color.Hex()) -} - func TextFlags(s string) string { return fmt.Sprintf("[::%s]", s) } @@ -44,3 +40,7 @@ func SublteText(c *Config, text string) string { subtle := ColorMark(c.Style.Subtle) return fmt.Sprintf("%s%s", subtle, text) } + +func ColorMark(color tcell.Color) string { + return fmt.Sprintf("[#%06x]", color.Hex()) +} diff --git a/ui/item_status.go b/ui/item_status.go index b66e224..26f4730 100644 --- a/ui/item_status.go +++ b/ui/item_status.go @@ -104,8 +104,7 @@ func drawStatus(tv *TutView, item api.Item, status *mastodon.Status, main *tview status = status.Reblog } - strippedContent, _ = util.CleanHTML(status.Content) - strippedContent = tview.Escape(strippedContent) + strippedContent, _ = util.CleanHTMLStyled(status.Content) width := 0 if main != nil { @@ -162,8 +161,7 @@ func drawStatus(tv *TutView, item api.Item, status *mastodon.Status, main *tview } if status.Sensitive { - strippedSpoiler, _ = util.CleanHTML(status.SpoilerText) - strippedSpoiler = tview.Escape(strippedSpoiler) + strippedSpoiler, _ = util.CleanHTMLStyled(status.SpoilerText) } toot.CWText = strippedSpoiler diff --git a/util/util.go b/util/util.go index 0c09d5e..738a313 100644 --- a/util/util.go +++ b/util/util.go @@ -11,6 +11,7 @@ import ( "github.com/RasmusLindroth/go-mastodon" "github.com/adrg/xdg" "github.com/microcosm-cc/bluemonday" + "github.com/rivo/tview" "golang.org/x/net/html" ) @@ -53,9 +54,9 @@ type URL struct { } func CleanHTML(content string) (string, []URL) { - stripped := bluemonday.NewPolicy().AllowElements("p", "br", "li", "ul").AllowAttrs("href", "class").OnElements("a").Sanitize(content) + stripped := bluemonday.NewPolicy().AllowElements("p", "br", "li", "ul", "ol").AllowAttrs("href", "class").OnElements("a").Sanitize(content) urls := getURLs(stripped) - stripped = bluemonday.NewPolicy().AllowElements("p", "br", "li", "ul").Sanitize(content) + stripped = bluemonday.NewPolicy().AllowElements("p", "br", "li", "ul", "ol").Sanitize(content) stripped = strings.ReplaceAll(stripped, "
", "\n") stripped = strings.ReplaceAll(stripped, "
", "\n") stripped = strings.ReplaceAll(stripped, "

", "") @@ -64,11 +65,37 @@ func CleanHTML(content string) (string, []URL) { stripped = strings.ReplaceAll(stripped, "", "\n") stripped = strings.ReplaceAll(stripped, "

", "\n") + stripped = strings.ReplaceAll(stripped, "
    ", "") + stripped = strings.ReplaceAll(stripped, "
", "\n") stripped = strings.TrimSpace(stripped) stripped = html.UnescapeString(stripped) return stripped, urls } +func CleanHTMLStyled(content string) (string, []URL) { + stripped := bluemonday.NewPolicy().AllowElements("p", "br", "li", "ul", "ol", "strong", "em").AllowAttrs("href", "class").OnElements("a").Sanitize(content) + urls := getURLs(stripped) + stripped = bluemonday.NewPolicy().AllowElements("p", "br", "li", "ul", "ol", "strong", "em").Sanitize(content) + stripped = strings.ReplaceAll(stripped, "
", "\n") + stripped = strings.ReplaceAll(stripped, "
", "\n") + stripped = strings.ReplaceAll(stripped, "

", "") + stripped = strings.ReplaceAll(stripped, "

", "\n\n") + stripped = strings.ReplaceAll(stripped, "
  • ", "* ") + stripped = strings.ReplaceAll(stripped, "
  • ", "\n") + stripped = strings.ReplaceAll(stripped, "", "\n") + stripped = strings.ReplaceAll(stripped, "
      ", "") + stripped = strings.ReplaceAll(stripped, "
    ", "\n") + stripped = html.UnescapeString(stripped) + stripped = tview.Escape(stripped) + stripped = strings.ReplaceAll(stripped, "", TextFlags("b")) + stripped = strings.ReplaceAll(stripped, "", TextFlags("-")) + stripped = strings.ReplaceAll(stripped, "", TextFlags("i")) + stripped = strings.ReplaceAll(stripped, "", TextFlags("-")) + stripped = strings.TrimSpace(stripped) + return stripped, urls +} + func getURLs(text string) []URL { doc := html.NewTokenizer(strings.NewReader(text)) var urls []URL @@ -213,3 +240,7 @@ func StatusOrReblog(s *mastodon.Status) *mastodon.Status { func SetTerminalTitle(s string) { fmt.Printf("\033]0;%s\a", s) } + +func TextFlags(s string) string { + return fmt.Sprintf("[::%s]", s) +}