// Package readings is the single entry point the CLI and TUI use to fetch // a day's readings. It routes between the modern (liturgy) and traditional // (tradlit) lectionaries by config, then applies part filtering, returning // source-agnostic liturgy.Section values either way. package readings import ( "strings" "github.com/lukaszkasprzak/lectio/internal/config" "github.com/lukaszkasprzak/lectio/internal/liturgy" "github.com/lukaszkasprzak/lectio/internal/tradlit" ) // Options controls how Load resolves a day's readings. type Options struct { // Date is the day to load, formatted YYYY-MM-DD. Date string // Refresh bypasses cache layers and re-fetches from the network // (modern lectionary only; see liturgy.Options.Refresh). Refresh bool // Offline restricts Load to previously cached/harvested data, never // hitting the network (both lectionaries; see liturgy.Options.Offline // and tradlit.Load's offline parameter). Offline bool // All, when true, keeps every part the config doesn't explicitly hide; // when false, only the gospel is kept. All bool } // Load fetches the day's sections and its DayInfo (celebration name, // temporal, liturgical colour -- see liturgy.DayInfo) for the configured // lectionary (cfg.Lectionary: "traditional" or "new") and applies part // filtering. Returns liturgy.Section values regardless of source. A source // that yields no DayInfo (see liturgy.Load/tradlit.Load) comes back as a // zero liturgy.DayInfo, not an error -- callers omit the header for it. func Load(cfg config.Config, opts Options) ([]liturgy.Section, liturgy.DayInfo, error) { offline := opts.Offline || cfg.Offline var secs []liturgy.Section var info liturgy.DayInfo var err error if cfg.Lectionary == "traditional" { secs, info, err = tradlit.Load(opts.Date, cfg.TraditionalLang, offline) } else { secs, info, err = liturgy.Load(liturgy.Options{ Date: opts.Date, Refresh: opts.Refresh, Offline: offline, }) } if err != nil { return nil, liturgy.DayInfo{}, err } return filterParts(secs, cfg, opts.All), info, nil } // 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") }