You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

165 lines
3.8 KiB

package renderer
import (
"fmt"
"io"
"strconv"
"strings"
"text/template"
"time"
"bloat/mastodon"
)
type Page string
const (
SigninPage = "signin.tmpl"
ErrorPage = "error.tmpl"
NavPage = "nav.tmpl"
RootPage = "root.tmpl"
TimelinePage = "timeline.tmpl"
ThreadPage = "thread.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 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)
}
return strings.NewReplacer(replacements...).Replace(content)
}
func statusContentFilter(content string, emojis []mastodon.Emoji, mentions []mastodon.Mention) string {
var replacements []string
var r string
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)
}
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(dur time.Duration, isAgo bool) string {
ret := ""
s := dur.Seconds()
m := dur.Minutes()
h := dur.Hours()
d := h / 24
mo := d / 30
y := mo / 12
if s < 60 {
ret = strconv.Itoa(int(s)) + "s"
} else if m < 60*2 {
ret = strconv.Itoa(int(m)) + "m"
} else if h < 24*2 {
ret = strconv.Itoa(int(h)) + "h"
} else if d < 30*2 {
ret = strconv.Itoa(int(d)) + "d"
} else if mo < 12*2 {
ret = strconv.Itoa(int(mo)) + "mo"
} else {
ret = strconv.Itoa(int(y)) + "y"
}
if isAgo {
ret += " ago"
} else {
ret += " from now"
}
return ret
}
func timeSince(t time.Time) string {
d := time.Since(t)
if d < 0 {
return timeUntil(t)
}
return DurToStr(d, true)
}
func timeUntil(t time.Time) string {
d := time.Until(t)
if d < 0 {
return timeSince(t)
}
return DurToStr(d, false)
}
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}
}
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,
}).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))
}