// Package readings is the single entry point the CLI and TUI use to resolve a // day's readings. It computes them offline from the embedded calendar engine // and lectionary data (Ordinary Form when cfg.Lectionary is "new", the 1962 // Extraordinary Form when "traditional"), then applies part filtering, returning // source-agnostic liturgy.Section values. package readings import ( "strings" "github.com/lukaszkasprzak/lectio/internal/config" "github.com/lukaszkasprzak/lectio/internal/liturgy" ) // Options controls how Load resolves a day's readings. type Options struct { // Date is the day to load, formatted YYYY-MM-DD. Date string // All, when true, keeps every part the config doesn't explicitly hide; // when false, only the gospel is kept. All bool } // Load computes the day's sections and its DayInfo (celebration name, // liturgical colour -- see liturgy.DayInfo) for the configured form // (cfg.Lectionary: "traditional" or "new") and applies part filtering. Every // reading is resolved offline from the embedded calendar and lectionary data. // // Load is LoadWith(Prepare(cfg), cfg, opts) -- a single call's worth of // convenience. A caller resolving several dates against the same cfg (a // week/month view) should call Prepare once and use LoadWith directly instead // of paying Prepare's cost on every date; see Prepared's doc comment // (internal/readings/offline.go). func Load(cfg config.Config, opts Options) ([]liturgy.Section, liturgy.DayInfo, error) { return LoadWith(Prepare(cfg), cfg, opts) } // LoadWith is Load, given an already-built Prepared (see Prepare) instead of // building its own. Reuse one Prepared across every date resolved against the // same cfg to skip re-stacking the calendar layers and re-parsing the book // table per date -- the fast path mobile.Days's multi-day loop uses. func LoadWith(p Prepared, cfg config.Config, opts Options) ([]liturgy.Section, liturgy.DayInfo, error) { secs, info, err := offlineLoadWith(p, cfg, opts.Date) if err != nil { return nil, liturgy.DayInfo{}, err } return filterParts(secs, cfg, opts.All), info, nil } // GospelCitation returns the gospel's scripture reference from loaded sections: // the section tagged "ewangelia" (modern) or "evangelium" (traditional), else // the first section; preferring its Citation, else the reference parsed from its // heading. Used by the month-calendar export (and mirrors the CLI helper). func GospelCitation(secs []liturgy.Section) string { pick := func(s liturgy.Section) string { if s.Citation != "" { return s.Citation } if c, err := liturgy.ExtractCitation(s.Heading); err == nil { return c } return "" } for _, s := range secs { if s.PartID == "ewangelia" || s.PartID == "evangelium" { return pick(s) } } if len(secs) > 0 { return pick(secs[0]) } return "" } // filterParts keeps only the gospel when !all (PartID "ewangelia" modern or // "evangelium" traditional). When all and the lectionary is traditional, it // keeps only the scripture readings (isTraditionalReading) -- dropping // Introit/Kolekta/Graduale/Offertorium/Sekreta/Prefacja/Komunia/Pokomunia // and any commemoration, none of which are readings; the "new" lectionary // path is unchanged, keeping sections cfg.PartShown allows (a section with // an empty PartID is always kept). Pure and network-free. func filterParts(secs []liturgy.Section, cfg config.Config, all bool) []liturgy.Section { var out []liturgy.Section for _, s := range secs { if !all { if s.PartID == "ewangelia" || s.PartID == "evangelium" { out = append(out, s) } continue } if cfg.Lectionary == "traditional" { if isTraditionalReading(s.PartID) { out = append(out, s) } continue } if s.PartID == "" || cfg.PartShown(cfg.Lectionary, s.PartID) { out = append(out, s) } } return out } // isTraditionalReading reports whether partID (a lowercased missalemeum // section id) is a scripture reading -- the gospel, or an epistle/lesson -- // rather than Introit/Kolekta/Graduale/Offertorium/Sekreta/Prefacja/ // Komunia/Pokomunia or a commemoration. func isTraditionalReading(partID string) bool { return partID == "evangelium" || strings.HasPrefix(partID, "lectio") || strings.HasPrefix(partID, "epistola") || strings.HasPrefix(partID, "prophetia") }