aboutsummaryrefslogtreecommitdiff
path: root/renderer/renderer.go
blob: f1595a58d4cd7a106f02490ee567be2d2f81cd9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package renderer

import (
	"html/template"
	"io"
	"regexp"
	"strconv"
	"strings"
	"time"

	"bloat/mastodon"
)

type Page string

const (
	SigninPage       = "signin.tmpl"
	ErrorPage        = "error.tmpl"
	NavPage          = "nav.tmpl"
	RootPage         = "root.tmpl"
	TimelinePage     = "timeline.tmpl"
	ListsPage        = "lists.tmpl"
	ListPage         = "list.tmpl"
	ThreadPage       = "thread.tmpl"
	QuickReplyPage   = "quickreply.tmpl"
	NotificationPage = "notification.tmpl"
	UserPage         = "user.tmpl"
	UserSearchPage   = "usersearch.tmpl"
	AboutPage        = "about.tmpl"
	EmojiPage        = "emoji.tmpl"
	LikedByPage      = "likedby.tmpl"
	RetweetedByPage  = "retweetedby.tmpl"
	SearchPage       = "search.tmpl"
	SettingsPage     = "settings.tmpl"
	FiltersPage      = "filters.tmpl"
)

type TemplateData struct {
	Data interface{}
	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
	for _, e := range emojis {
		replacements = append(replacements, ":"+e.ShortCode+":", emojiHTML(e, "24"))
	}
	return strings.NewReplacer(replacements...).Replace(content)
}

var quoteRE = regexp.MustCompile("(?mU)(^|> *|\n)(&gt;.*)(<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
	for _, e := range emojis {
		replacements = append(replacements, ":"+e.ShortCode+":", emojiHTML(e, "32"))
	}
	for _, m := range mentions {
		replacements = append(replacements, `"`+m.URL+`"`, `"/user/`+m.ID+`" title="@`+m.Acct+`"`)
	}
	return strings.NewReplacer(replacements...).Replace(content)
}

func displayInteractionCount(c int64) string {
	if c > 0 {
		return strconv.Itoa(int(c))
	}
	return ""
}

func durToStr(s int64) string {
	if s < 60 {
		return strconv.Itoa(int(s)) + "s"
	}
	m := s / 60
	if m < 60*2 {
		return strconv.Itoa(int(m)) + "m"
	}
	h := m / 60
	if h < 24*2 {
		return strconv.Itoa(int(h)) + "h"
	}
	d := h / 24
	if d < 30*2 {
		return strconv.Itoa(int(d)) + "d"
	}
	mo := d / 30
	if mo < 12*2 {
		return strconv.Itoa(int(mo)) + "mo"
	}
	y := d / 365
	return strconv.Itoa(int(y)) + "y"
}

func timeSince(t time.Time) string {
	d := time.Now().Unix() - t.Unix()
	if d < 0 {
		d = 0
	}
	return durToStr(d)
}

func timeUntil(t time.Time) string {
	d := t.Unix() - time.Now().Unix()
	if d < 0 {
		d = 0
	}
	return durToStr(d)
}

func formatTimeRFC3339(t time.Time) string {
	return t.Format(time.RFC3339)
}

func formatTimeRFC822(t time.Time) string {
	return t.Format(time.RFC822)
}

func withContext(data interface{}, ctx *Context) TemplateData {
	return TemplateData{data, ctx}
}

func raw(s string) template.HTML {
	return template.HTML(s)
}

type Renderer interface {
	Render(ctx *Context, writer io.Writer, page string, data interface{}) (err error)
}

type renderer struct {
	template *template.Template
}

func NewRenderer(templateGlobPattern string) (r *renderer, err error) {
	t := template.New("default")
	t, err = t.Funcs(template.FuncMap{
		"EmojiFilter":             emojiFilter,
		"StatusContentFilter":     statusContentFilter,
		"DisplayInteractionCount": displayInteractionCount,
		"TimeSince":               timeSince,
		"TimeUntil":               timeUntil,
		"FormatTimeRFC3339":       formatTimeRFC3339,
		"FormatTimeRFC822":        formatTimeRFC822,
		"WithContext":             withContext,
		"HTML":                    template.HTMLEscapeString,
		"Raw":                     raw,
	}).ParseGlob(templateGlobPattern)
	if err != nil {
		return
	}
	return &renderer{
		template: t,
	}, nil
}

func (r *renderer) Render(ctx *Context, writer io.Writer,
	page string, data interface{}) (err error) {
	return r.template.ExecuteTemplate(writer, page, withContext(data, ctx))
}