aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-07-23 13:28:38 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-07-23 13:28:38 +0200
commit0351f4f188207d0204e1e0124d32127e820e33d5 (patch)
tree526ae229dd7a56987cf6f262afbf2d5f0ad53293
parentf8772261f0c4feca68eee0300b5d726fd168a534 (diff)
downloadlectio-0351f4f188207d0204e1e0124d32127e820e33d5.tar.gz
lectio-0351f4f188207d0204e1e0124d32127e820e33d5.zip
readings: lectionary router + part filtering
-rw-r--r--internal/liturgy/fetch.go7
-rw-r--r--internal/readings/readings.go75
-rw-r--r--internal/readings/readings_test.go97
3 files changed, 179 insertions, 0 deletions
diff --git a/internal/liturgy/fetch.go b/internal/liturgy/fetch.go
index d6cc4b6..5447552 100644
--- a/internal/liturgy/fetch.go
+++ b/internal/liturgy/fetch.go
@@ -15,6 +15,13 @@ import (
// It is a package var so tests can point it at an httptest server.
var baseURL = "https://niezbednik.niedziela.pl/liturgia/%s/Ewangelia"
+// SetBaseURL overrides the fetch URL template used by Load. It exists so
+// tests in other packages (e.g. internal/readings) can point Load at an
+// httptest server; production code must never call it.
+func SetBaseURL(url string) {
+ baseURL = url
+}
+
// userAgent is sent on every fetch; the site serves a different (broken)
// page to non-browser clients without it.
const userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 " +
diff --git a/internal/readings/readings.go b/internal/readings/readings.go
new file mode 100644
index 0000000..886c1eb
--- /dev/null
+++ b/internal/readings/readings.go
@@ -0,0 +1,75 @@
+// 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 (
+ "fmt"
+
+ "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. Traditional+Offline is not yet supported.
+ 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 for the configured lectionary
+// (cfg.Lectionary: "traditional" or "new") and applies part filtering.
+// Returns liturgy.Section values regardless of source.
+func Load(cfg config.Config, opts Options) ([]liturgy.Section, error) {
+ offline := opts.Offline || cfg.Offline
+
+ var secs []liturgy.Section
+ var err error
+
+ if cfg.Lectionary == "traditional" {
+ if offline {
+ return nil, fmt.Errorf("readings: offline traditional not yet supported; use lectionary=new offline")
+ }
+ secs, err = tradlit.Load(opts.Date, cfg.TraditionalLang)
+ } else {
+ secs, err = liturgy.Load(liturgy.Options{
+ Date: opts.Date,
+ Refresh: opts.Refresh,
+ Offline: offline,
+ })
+ }
+ if err != nil {
+ return nil, err
+ }
+
+ return filterParts(secs, cfg, opts.All), nil
+}
+
+// filterParts keeps only the gospel when !all (PartID "ewangelia" modern or
+// "evangelium" traditional), otherwise keeps 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 s.PartID == "" || cfg.PartShown(cfg.Lectionary, s.PartID) {
+ out = append(out, s)
+ }
+ }
+ return out
+}
diff --git a/internal/readings/readings_test.go b/internal/readings/readings_test.go
new file mode 100644
index 0000000..fd355ff
--- /dev/null
+++ b/internal/readings/readings_test.go
@@ -0,0 +1,97 @@
+package readings
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "strings"
+ "testing"
+
+ "github.com/lukaszkasprzak/lectio/internal/config"
+ "github.com/lukaszkasprzak/lectio/internal/liturgy"
+)
+
+func sectionsForFilterTests() []liturgy.Section {
+ return []liturgy.Section{
+ {PartID: "pierwsze_czytanie", Heading: "1. czytanie"},
+ {PartID: "psalm", Heading: "Psalm"},
+ {PartID: "ewangelia", Heading: "Ewangelia"},
+ }
+}
+
+func TestFilterPartsGospelOnly(t *testing.T) {
+ secs := sectionsForFilterTests()
+ got := filterParts(secs, config.Config{}, false)
+
+ if len(got) != 1 {
+ t.Fatalf("got %d sections, want 1: %+v", len(got), got)
+ }
+ if got[0].PartID != "ewangelia" {
+ t.Errorf("got PartID %q, want ewangelia", got[0].PartID)
+ }
+}
+
+func TestFilterPartsAll(t *testing.T) {
+ secs := sectionsForFilterTests()
+ cfg := config.Config{
+ Lectionary: "new",
+ Parts: map[string]map[string]bool{
+ "new": {"psalm": false},
+ },
+ }
+ got := filterParts(secs, cfg, true)
+
+ if len(got) != 2 {
+ t.Fatalf("got %d sections, want 2: %+v", len(got), got)
+ }
+ for _, s := range got {
+ if s.PartID == "psalm" {
+ t.Errorf("psalm should have been filtered out: %+v", got)
+ }
+ }
+ ids := map[string]bool{got[0].PartID: true, got[1].PartID: true}
+ if !ids["pierwsze_czytanie"] || !ids["ewangelia"] {
+ t.Errorf("got %+v, want pierwsze_czytanie and ewangelia", got)
+ }
+}
+
+func TestLoadModernRoutes(t *testing.T) {
+ html, err := os.ReadFile("../liturgy/testdata/2026-07-22.html")
+ if err != nil {
+ t.Fatal(err)
+ }
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Write(html)
+ }))
+ defer srv.Close()
+ liturgy.SetBaseURL(srv.URL + "/liturgia/%s/Ewangelia")
+ t.Setenv("XDG_CACHE_HOME", t.TempDir())
+
+ cfg := config.Config{Lectionary: "new"}
+ secs, err := Load(cfg, Options{Date: "2026-07-22", All: true})
+ if err != nil {
+ t.Fatalf("Load: %v", err)
+ }
+
+ found := false
+ for _, s := range secs {
+ if s.PartID == "ewangelia" {
+ found = true
+ }
+ }
+ if !found {
+ t.Errorf("no gospel section (PartID=ewangelia) found in %+v", secs)
+ }
+}
+
+func TestLoadTraditionalOfflineErrors(t *testing.T) {
+ cfg := config.Config{Lectionary: "traditional", TraditionalLang: "pl"}
+ _, err := Load(cfg, Options{Date: "2026-07-22", Offline: true})
+ if err == nil {
+ t.Fatal("expected error, got nil")
+ }
+ msg := strings.ToLower(err.Error())
+ if !strings.Contains(msg, "offline") || !strings.Contains(msg, "traditional") {
+ t.Errorf("error %q should mention offline and traditional", err.Error())
+ }
+}