Browse Source

add more style

pull/253/head
Rasmus Lindroth 3 years ago
parent
commit
b38b1d3028
  1. 8
      config/keys.go
  2. 6
      ui/item_status.go
  3. 35
      util/util.go

8
config/keys.go

@ -32,10 +32,6 @@ func ColorKey(c *Config, pre, key, end string) string {
return text return text
} }
func ColorMark(color tcell.Color) string {
return fmt.Sprintf("[#%06x]", color.Hex())
}
func TextFlags(s string) string { func TextFlags(s string) string {
return fmt.Sprintf("[::%s]", s) return fmt.Sprintf("[::%s]", s)
} }
@ -44,3 +40,7 @@ func SublteText(c *Config, text string) string {
subtle := ColorMark(c.Style.Subtle) subtle := ColorMark(c.Style.Subtle)
return fmt.Sprintf("%s%s", subtle, text) return fmt.Sprintf("%s%s", subtle, text)
} }
func ColorMark(color tcell.Color) string {
return fmt.Sprintf("[#%06x]", color.Hex())
}

6
ui/item_status.go

@ -104,8 +104,7 @@ func drawStatus(tv *TutView, item api.Item, status *mastodon.Status, main *tview
status = status.Reblog status = status.Reblog
} }
strippedContent, _ = util.CleanHTML(status.Content) strippedContent, _ = util.CleanHTMLStyled(status.Content)
strippedContent = tview.Escape(strippedContent)
width := 0 width := 0
if main != nil { if main != nil {
@ -162,8 +161,7 @@ func drawStatus(tv *TutView, item api.Item, status *mastodon.Status, main *tview
} }
if status.Sensitive { if status.Sensitive {
strippedSpoiler, _ = util.CleanHTML(status.SpoilerText) strippedSpoiler, _ = util.CleanHTMLStyled(status.SpoilerText)
strippedSpoiler = tview.Escape(strippedSpoiler)
} }
toot.CWText = strippedSpoiler toot.CWText = strippedSpoiler

35
util/util.go

@ -11,6 +11,7 @@ import (
"github.com/RasmusLindroth/go-mastodon" "github.com/RasmusLindroth/go-mastodon"
"github.com/adrg/xdg" "github.com/adrg/xdg"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"github.com/rivo/tview"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
@ -53,9 +54,9 @@ type URL struct {
} }
func CleanHTML(content string) (string, []URL) { 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) 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, "<br>", "\n") stripped = strings.ReplaceAll(stripped, "<br>", "\n")
stripped = strings.ReplaceAll(stripped, "<br/>", "\n") stripped = strings.ReplaceAll(stripped, "<br/>", "\n")
stripped = strings.ReplaceAll(stripped, "<p>", "") stripped = strings.ReplaceAll(stripped, "<p>", "")
@ -64,11 +65,37 @@ func CleanHTML(content string) (string, []URL) {
stripped = strings.ReplaceAll(stripped, "</li>", "\n") stripped = strings.ReplaceAll(stripped, "</li>", "\n")
stripped = strings.ReplaceAll(stripped, "<ul>", "") stripped = strings.ReplaceAll(stripped, "<ul>", "")
stripped = strings.ReplaceAll(stripped, "</ul>", "\n") stripped = strings.ReplaceAll(stripped, "</ul>", "\n")
stripped = strings.ReplaceAll(stripped, "<ol>", "")
stripped = strings.ReplaceAll(stripped, "</ol>", "\n")
stripped = strings.TrimSpace(stripped) stripped = strings.TrimSpace(stripped)
stripped = html.UnescapeString(stripped) stripped = html.UnescapeString(stripped)
return stripped, urls 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, "<br>", "\n")
stripped = strings.ReplaceAll(stripped, "<br/>", "\n")
stripped = strings.ReplaceAll(stripped, "<p>", "")
stripped = strings.ReplaceAll(stripped, "</p>", "\n\n")
stripped = strings.ReplaceAll(stripped, "<li>", "* ")
stripped = strings.ReplaceAll(stripped, "</li>", "\n")
stripped = strings.ReplaceAll(stripped, "<ul>", "")
stripped = strings.ReplaceAll(stripped, "</ul>", "\n")
stripped = strings.ReplaceAll(stripped, "<ol>", "")
stripped = strings.ReplaceAll(stripped, "</ol>", "\n")
stripped = html.UnescapeString(stripped)
stripped = tview.Escape(stripped)
stripped = strings.ReplaceAll(stripped, "<strong>", TextFlags("b"))
stripped = strings.ReplaceAll(stripped, "</strong>", TextFlags("-"))
stripped = strings.ReplaceAll(stripped, "<em>", TextFlags("i"))
stripped = strings.ReplaceAll(stripped, "</em>", TextFlags("-"))
stripped = strings.TrimSpace(stripped)
return stripped, urls
}
func getURLs(text string) []URL { func getURLs(text string) []URL {
doc := html.NewTokenizer(strings.NewReader(text)) doc := html.NewTokenizer(strings.NewReader(text))
var urls []URL var urls []URL
@ -213,3 +240,7 @@ func StatusOrReblog(s *mastodon.Status) *mastodon.Status {
func SetTerminalTitle(s string) { func SetTerminalTitle(s string) {
fmt.Printf("\033]0;%s\a", s) fmt.Printf("\033]0;%s\a", s)
} }
func TextFlags(s string) string {
return fmt.Sprintf("[::%s]", s)
}

Loading…
Cancel
Save