aboutsummaryrefslogtreecommitdiff
blob: 0cb40f5a358e9f9c5a1b0c7beeb6091df0274ba6 (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
// miscellaneous utility functions used for the landing page of the application

package message

import (
	"archives/pkg/models"
	"html/template"
	"net/http"
	"strings"
)

// renderIndexTemplate renders all templates used for the landing page
func renderMessageTemplate(w http.ResponseWriter, listName string, message *models.Message, inReplyTo *models.Message, replies []*models.Message) {
	templates := template.Must(
		template.Must(
			template.New("Show").
				Funcs(getFuncMap()).
				ParseGlob("web/templates/layout/*.tmpl")).
			ParseGlob("web/templates/message/*.tmpl"))

	templateData := struct {
		ListName  string
		Message   *models.Message
		InReplyTo *models.Message
		Replies   []*models.Message
	}{
		ListName:  listName,
		Message:   message,
		InReplyTo: inReplyTo,
		Replies:   replies,
	}

	templates.ExecuteTemplate(w, "show.tmpl", templateData)
}

func getFuncMap() template.FuncMap {
	return template.FuncMap{
		"formatAddr": func(addr string) string {
			if strings.Contains(addr, "@lists.gentoo.org") || strings.Contains(addr, "@gentoo.org") {
				addr = strings.ReplaceAll(addr, "@lists.gentoo.org", "@l.g.o")
				addr = strings.ReplaceAll(addr, "@gentoo.org", "@g.o")
			} else {
				start := false
				for i := len(addr) - 1; i > 0; i-- {
					if addr[i] == '@' {
						break
					}
					if start {
						out := []rune(addr)
						out[i] = '×'
						addr = string(out)
					}
					if addr[i] == '.' {
						start = true
					}
				}
			}
			return addr
		},
	}
}

func replaceAtIndex(in string, r rune, i int) string {
	out := []rune(in)
	out[i] = r
	return string(out)
}