bloat/renderer/renderer.go

148 lines
4.5 KiB
Go
Raw Normal View History

2019-12-13 18:08:26 +00:00
package renderer
import (
"context"
"io"
"strconv"
"strings"
"text/template"
"time"
"mastodon"
)
type Renderer interface {
RenderErrorPage(ctx context.Context, writer io.Writer, err error)
RenderHomePage(ctx context.Context, writer io.Writer) (err error)
RenderSigninPage(ctx context.Context, writer io.Writer) (err error)
RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelinePageTemplateData) (err error)
RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadPageTemplateData) (err error)
2019-12-15 17:37:58 +00:00
RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationPageTemplateData) (err error)
RenderUserPage(ctx context.Context, writer io.Writer, data *UserPageTemplateData) (err error)
2019-12-21 05:48:48 +00:00
RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutPageTemplateData) (err error)
2019-12-22 18:10:42 +00:00
RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiPageTemplateData) (err error)
2019-12-13 18:08:26 +00:00
}
type renderer struct {
template *template.Template
}
func NewRenderer(templateGlobPattern string) (r *renderer, err error) {
t := template.New("default")
t, err = t.Funcs(template.FuncMap{
2019-12-21 05:48:06 +00:00
"EmojiFilter": EmojiFilter,
"StatusContentFilter": StatusContentFilter,
2019-12-13 18:08:26 +00:00
"DisplayInteractionCount": DisplayInteractionCount,
"TimeSince": TimeSince,
"FormatTimeRFC3339": FormatTimeRFC3339,
}).ParseGlob(templateGlobPattern)
if err != nil {
return
}
return &renderer{
template: t,
}, nil
}
func (r *renderer) RenderErrorPage(ctx context.Context, writer io.Writer, err error) {
r.template.ExecuteTemplate(writer, "error.tmpl", err)
return
}
func (r *renderer) RenderHomePage(ctx context.Context, writer io.Writer) (err error) {
return r.template.ExecuteTemplate(writer, "homepage.tmpl", nil)
}
func (r *renderer) RenderSigninPage(ctx context.Context, writer io.Writer) (err error) {
return r.template.ExecuteTemplate(writer, "signin.tmpl", nil)
}
func (r *renderer) RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelinePageTemplateData) (err error) {
return r.template.ExecuteTemplate(writer, "timeline.tmpl", data)
}
func (r *renderer) RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadPageTemplateData) (err error) {
return r.template.ExecuteTemplate(writer, "thread.tmpl", data)
}
2019-12-15 17:37:58 +00:00
func (r *renderer) RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationPageTemplateData) (err error) {
return r.template.ExecuteTemplate(writer, "notification.tmpl", data)
}
func (r *renderer) RenderUserPage(ctx context.Context, writer io.Writer, data *UserPageTemplateData) (err error) {
return r.template.ExecuteTemplate(writer, "user.tmpl", data)
}
2019-12-21 05:48:48 +00:00
func (r *renderer) RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutPageTemplateData) (err error) {
return r.template.ExecuteTemplate(writer, "about.tmpl", data)
}
2019-12-21 05:48:06 +00:00
2019-12-22 18:10:42 +00:00
func (r *renderer) RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiPageTemplateData) (err error) {
return r.template.ExecuteTemplate(writer, "emoji.tmpl", data)
}
2019-12-21 05:48:06 +00:00
func EmojiFilter(content string, emojis []mastodon.Emoji) string {
var replacements []string
for _, e := range emojis {
2019-12-22 03:43:25 +00:00
replacements = append(replacements, ":"+e.ShortCode+":", "<img class=\"status-emoji\" src=\""+e.URL+"\" alt=\""+e.ShortCode+"\" title=\""+e.ShortCode+"\" />")
2019-12-21 05:48:06 +00:00
}
return strings.NewReplacer(replacements...).Replace(content)
}
2019-12-21 07:22:21 +00:00
func StatusContentFilter(spoiler string, content string, emojis []mastodon.Emoji, mentions []mastodon.Mention) string {
if len(spoiler) > 0 {
content = spoiler + "<br />" + content
}
2019-12-21 05:48:06 +00:00
var replacements []string
2019-12-13 18:08:26 +00:00
for _, e := range emojis {
2019-12-22 03:43:25 +00:00
replacements = append(replacements, ":"+e.ShortCode+":", "<img class=\"status-emoji\" src=\""+e.URL+"\" alt=\""+e.ShortCode+"\" title=\""+e.ShortCode+"\" />")
2019-12-21 05:48:06 +00:00
}
for _, m := range mentions {
replacements = append(replacements, "\""+m.URL+"\"", "\"/user/"+m.ID+"\"")
2019-12-13 18:08:26 +00:00
}
2019-12-21 05:48:06 +00:00
return strings.NewReplacer(replacements...).Replace(content)
2019-12-13 18:08:26 +00:00
}
func DisplayInteractionCount(c int64) string {
if c > 0 {
return strconv.Itoa(int(c))
}
return ""
}
func TimeSince(t time.Time) string {
dur := time.Since(t)
s := dur.Seconds()
if s < 60 {
return strconv.Itoa(int(s)) + "s"
}
m := dur.Minutes()
if m < 60 {
return strconv.Itoa(int(m)) + "m"
}
h := dur.Hours()
if h < 24 {
return strconv.Itoa(int(h)) + "h"
}
d := h / 24
if d < 30 {
return strconv.Itoa(int(d)) + "d"
}
mo := d / 30
if mo < 12 {
return strconv.Itoa(int(mo)) + "mo"
}
2019-12-20 18:31:55 +00:00
y := mo / 12
2019-12-13 18:08:26 +00:00
return strconv.Itoa(int(y)) + "y"
}
func FormatTimeRFC3339(t time.Time) string {
return t.Format(time.RFC3339)
}