summaryrefslogtreecommitdiff
path: root/internal/calfeed/build.go
blob: 8b4f7f093afcee951d92816703541615e1d6e99d (plain) (blame)
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
package calfeed

import (
	"time"

	"github.com/lukaszkasprzak/lectio/internal/calendar"
	"github.com/lukaszkasprzak/lectio/internal/naming"
)

// Build computes the day list from..to inclusive (calendar.Compute per date)
// and maps it to the wire model. readings is injected -- the caller passes
// caldata.Readings -- so calfeed never imports internal/caldata.
func Build(from, to time.Time, uiLang string, sel calendar.Selection, layers []calendar.Layer, readings func(date time.Time, day calendar.LiturgicalDay) []calendar.Reading) []DayView {
	var days []DayView
	for d := from; !d.After(to); d = d.AddDate(0, 0, 1) {
		day := calendar.Compute(d, sel, layers)

		others := make([]CelView, 0, len(day.Others))
		for _, o := range day.Others {
			others = append(others, celView(uiLang, o))
		}

		var rs []ReadingView
		for _, r := range readings(d, day) {
			rs = append(rs, ReadingView{Part: r.Part, Citation: r.Citation})
		}

		days = append(days, DayView{
			Date:     d.Format("2006-01-02"),
			Season:   string(day.Season),
			Week:     day.Week,
			Weekday:  day.Weekday.String(),
			Colour:   string(day.Colour),
			Observed: celView(uiLang, day.Observed),
			Others:   others,
			Cycles:   Cycles{Sunday: day.SundayCycle, Weekday: day.WeekdayCycle},
			Readings: rs,
		})
	}
	return days
}

// celView maps a calendar.Celebration to its wire view.
func celView(uiLang string, c calendar.Celebration) CelView {
	return CelView{
		Slug:  c.Slug,
		Name:  celebrationName(uiLang, c),
		Rank:  string(c.Rank),
		Class: int(c.Class),
	}
}

// celebrationName resolves c's name in uiLang via naming.CelebrationName
// (name.<lang> -> English -> Latin -> humanized slug), then "(feria)" for an
// unnamed temporal day.
func celebrationName(uiLang string, c calendar.Celebration) string {
	if n := naming.CelebrationName(uiLang, c); n != "" {
		return n
	}
	return "(feria)"
}