diff options
Diffstat (limited to 'renderer/renderer.go')
-rw-r--r-- | renderer/renderer.go | 87 |
1 files changed, 46 insertions, 41 deletions
diff --git a/renderer/renderer.go b/renderer/renderer.go index 6c9877a..50fced7 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -1,11 +1,11 @@ package renderer import ( - "fmt" + "html/template" "io" + "regexp" "strconv" "strings" - "text/template" "time" "bloat/mastodon" @@ -19,6 +19,8 @@ const ( NavPage = "nav.tmpl" RootPage = "root.tmpl" TimelinePage = "timeline.tmpl" + ListsPage = "lists.tmpl" + ListPage = "list.tmpl" ThreadPage = "thread.tmpl" QuickReplyPage = "quickreply.tmpl" StatusPopup = "status.tmpl" @@ -39,29 +41,25 @@ type TemplateData struct { 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 { var replacements []string - var r string for _, e := range emojis { - r = fmt.Sprintf("<img class=\"emoji\" src=\"%s\" alt=\":%s:\" title=\":%s:\" height=\"24\" />", - e.URL, e.ShortCode, e.ShortCode) - replacements = append(replacements, ":"+e.ShortCode+":", r) + replacements = append(replacements, ":"+e.ShortCode+":", emojiHTML(e, "24")) } return strings.NewReplacer(replacements...).Replace(content) } -func statusContentFilter(spoiler string, content string, - emojis []mastodon.Emoji, mentions []mastodon.Mention) string { +var quoteRE = regexp.MustCompile("(?mU)(^|> *|\n)(>.*)(<br|$)") +func statusContentFilter(content string, emojis []mastodon.Emoji, mentions []mastodon.Mention) string { + content = quoteRE.ReplaceAllString(content, `$1<span class="quote">$2</span>$3`) var replacements []string - var r string - if len(spoiler) > 0 { - content = spoiler + "<br />" + content - } for _, e := range emojis { - r = fmt.Sprintf("<img class=\"emoji\" src=\"%s\" alt=\":%s:\" title=\":%s:\" height=\"32\" />", - e.URL, e.ShortCode, e.ShortCode) - replacements = append(replacements, ":"+e.ShortCode+":", r) + replacements = append(replacements, ":"+e.ShortCode+":", emojiHTML(e, "32")) } for _, m := range mentions { replacements = append(replacements, `"`+m.URL+`"`, `"/user/`+m.ID+`" title="@`+m.Acct+`"`) @@ -76,45 +74,41 @@ func displayInteractionCount(c int64) string { return "" } -func DurToStr(dur time.Duration) string { - s := dur.Seconds() +func durUnit(s int64) (dur int64, unit string) { if s < 60 { - return strconv.Itoa(int(s)) + "s" + if s < 0 { + s = 0 + } + return s, "s" } - m := dur.Minutes() - if m < 60*2 { - return strconv.Itoa(int(m)) + "m" - } - h := dur.Hours() - if h < 24*2 { - return strconv.Itoa(int(h)) + "h" + m := s / 60 + h := m / 60 + if h < 2 { + return m, "m" } d := h / 24 - if d < 30*2 { - return strconv.Itoa(int(d)) + "d" + if d < 2 { + return h, "h" } mo := d / 30 - if mo < 12*2 { - return strconv.Itoa(int(mo)) + "mo" + if mo < 2 { + return d, "d" + } + y := d / 365 + if y < 2 { + return mo, "mo" } - y := mo / 12 - return strconv.Itoa(int(y)) + "y" + return y, "y" } func timeSince(t time.Time) string { - d := time.Since(t) - if d < 0 { - d = 0 - } - return DurToStr(d) + d, u := durUnit(time.Now().Unix() - t.Unix()) + return strconv.FormatInt(d, 10) + u } func timeUntil(t time.Time) string { - d := time.Until(t) - if d < 0 { - d = 0 - } - return DurToStr(d) + d, u := durUnit(t.Unix() - time.Now().Unix()) + return strconv.FormatInt(d, 10) + u } func formatTimeRFC3339(t time.Time) string { @@ -129,6 +123,14 @@ func withContext(data interface{}, ctx *Context) TemplateData { return TemplateData{data, ctx} } +func raw(s string) template.HTML { + return template.HTML(s) +} + +func rawCSS(s string) template.CSS { + return template.CSS(s) +} + type Renderer interface { Render(ctx *Context, writer io.Writer, page string, data interface{}) (err error) } @@ -148,6 +150,9 @@ func NewRenderer(templateGlobPattern string) (r *renderer, err error) { "FormatTimeRFC3339": formatTimeRFC3339, "FormatTimeRFC822": formatTimeRFC822, "WithContext": withContext, + "HTML": template.HTMLEscapeString, + "Raw": raw, + "RawCSS": rawCSS, }).ParseGlob(templateGlobPattern) if err != nil { return |