package calfeed import ( "time" "github.com/lukaszkasprzak/lectio/internal/calendar" ) // 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, falling back to English, then // a humanized slug (with the "ef-" prefix stripped and dashes turned to // spaces), then "(feria)" for an unnamed temporal day. Mirrors // cli.celebrationName. func celebrationName(uiLang string, c calendar.Celebration) string { if n := c.Name[uiLang]; n != "" { return n } if n := c.Name["en"]; n != "" { return n } if c.Slug == "" { return "(feria)" } return calendar.HumanizeSlug(c.Slug) }