Refactor status filter

This commit is contained in:
r 2022-01-27 11:41:31 +00:00
parent b8bab6fcf3
commit 03a1cd819a
1 changed files with 10 additions and 15 deletions

View File

@ -1,7 +1,6 @@
package renderer package renderer
import ( import (
"fmt"
"io" "io"
"regexp" "regexp"
"strconv" "strconv"
@ -39,32 +38,28 @@ type TemplateData struct {
Ctx *Context Ctx *Context
} }
func emojiHTML(e mastodon.Emoji, height string) string {
return `<img class="emoji" src="` + e.URL + `" alt=":` + e.ShortCode + `:" title=":` + e.ShortCode + `:" height="` + height + `"/>`
}
func emojiFilter(content string, emojis []mastodon.Emoji) string { func emojiFilter(content string, emojis []mastodon.Emoji) string {
var replacements []string var replacements []string
var r string
for _, e := range emojis { for _, e := range emojis {
r = fmt.Sprintf("<img class=\"emoji\" src=\"%s\" alt=\":%s:\" title=\":%s:\" height=\"24\" />", replacements = append(replacements, ":"+e.ShortCode+":", emojiHTML(e, "24"))
e.URL, e.ShortCode, e.ShortCode)
replacements = append(replacements, ":"+e.ShortCode+":", r)
} }
return strings.NewReplacer(replacements...).Replace(content) return strings.NewReplacer(replacements...).Replace(content)
} }
var quoteRE = regexp.MustCompile("(?mU)(^|> *|\n)(&gt;.*)(<br|$)") var quoteRE = regexp.MustCompile("(?mU)(^|> *|\n)(&gt;.*)(<br|$)")
func statusContentFilter(spoiler string, content string, func statusContentFilter(spoiler, content string, emojis []mastodon.Emoji, mentions []mastodon.Mention) string {
emojis []mastodon.Emoji, mentions []mastodon.Mention) string {
var replacements []string
var r string
if len(spoiler) > 0 { if len(spoiler) > 0 {
content = spoiler + "<br />" + content content = spoiler + "<br/>" + content
} }
content = quoteRE.ReplaceAllString(content, "$1<span class=\"quote\">$2</span>$3") content = quoteRE.ReplaceAllString(content, `$1<span class="quote">$2</span>$3`)
var replacements []string
for _, e := range emojis { for _, e := range emojis {
r = fmt.Sprintf("<img class=\"emoji\" src=\"%s\" alt=\":%s:\" title=\":%s:\" height=\"32\" />", replacements = append(replacements, ":"+e.ShortCode+":", emojiHTML(e, "32"))
e.URL, e.ShortCode, e.ShortCode)
replacements = append(replacements, ":"+e.ShortCode+":", r)
} }
for _, m := range mentions { for _, m := range mentions {
replacements = append(replacements, `"`+m.URL+`"`, `"/user/`+m.ID+`" title="@`+m.Acct+`"`) replacements = append(replacements, `"`+m.URL+`"`, `"/user/`+m.ID+`" title="@`+m.Acct+`"`)