summaryrefslogtreecommitdiffstats
path: root/pkg/html/service.go
blob: 42351fb534218550599e1d5bff11bee2320bf1a5 (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
package html

import (
	"strings"
	"time"

	"git.leonardobishop.net/stash/pkg/entries"
)

type Service interface {
	GenerateHtml([]entries.EntryRow) (string, error)
}

type service struct{}

func NewService() Service {
	return &service{}
}

func (s *service) GenerateHtml(entries []entries.EntryRow) (string, error) {
	var str string

	var currentDate time.Time
	var group bool

	for _, entry := range entries {
		date, err := time.Parse(time.DateTime, entry.Timestamp)
		if err != nil {
			return "", err
		}

		if currentDate.Year() != date.Year() || currentDate.Month() != date.Month() {
			if group {
				str += "</div>"
			}
			str += "<h2>" + date.Format("January 2006") + "</h2>"

			str += "<div class=\"entry-group\">"
			group = true
		}

		currentDate = date

		str += "<span class=\"entry\">"
		str += "<a class=\"entry-title\" href=" + entry.Url + " target=\"_blank\">"
		if entry.KindName == "starred" {
			str += "<b>"
		}
		str += entry.KindEmoji + " " + entry.Title
		if entry.KindName == "starred" {
			str += "</b>"
		}
		str += "</a>"

		str += "<span class=\"entry-description\">" + truncateText(entry.Description, 300) + "</span>"
		str += "<i class=\"entry-date\">on " + date.Format("02 Jan 2006") + "</i>"
		str += "</span>"
	}

	if group {
		str += "</div>"
	}

	return str, nil
}

func truncateText(s string, max int) string {
	if max > len(s) {
		return s
	}
	return s[:strings.LastIndex(s[:max], " ")] + "..."
}