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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
// Package render turns a liturgy.Section, one version at a time, into text
// output: the deduped Polish paragraphs or a bible tool's verse lines, and
// lays several versions out side by side for comparison. It is shared by
// the CLI and the TUI (which re-styles the same gathered data).
package render
import (
"fmt"
"strings"
"github.com/lukaszkasprzak/lectio/internal/bible"
"github.com/lukaszkasprzak/lectio/internal/i18n"
"github.com/lukaszkasprzak/lectio/internal/liturgy"
)
// versionLabel returns version's column-header/prose label in lang (see
// internal/i18n.Get), falling back to the bare version code if lang has no
// entry for it. lang="pl" reproduces ewangelia.py's original VERSION_LABELS
// exactly.
func versionLabel(version, lang string) string {
if l, ok := i18n.Get(lang).Version[version]; ok {
return l
}
return version
}
// LocalizeHeading swaps a modern (niedziela.pl) section heading's leading
// label word for its lang translation, keeping the rest of the heading (the
// parenthetical citation, exactly as scraped) untouched: e.g.
// "Ewangelia (J 20, 1. 11-18)" with partID "ewangelia" and lang "en" becomes
// "Gospel (J 20, 1. 11-18)". It only ever localises the label word, never
// the citation or the verse text.
//
// It is a safe no-op (returns heading unchanged) unless all of: lang is
// "en", partID names a known modern-lectionary part (see
// internal/i18n.UI.PartLabel), and heading actually starts with that part's
// Polish label -- which excludes traditional (missalemeum) headings, already
// in the requested language, and anything unrecognised.
func LocalizeHeading(heading, partID, lang string) string {
if lang != "en" {
return heading
}
plLabel, ok := i18n.Get("pl").PartLabel[partID]
if !ok || !strings.HasPrefix(heading, plLabel) {
return heading
}
enLabel := i18n.Get(lang).PartLabel[partID]
return enLabel + heading[len(plLabel):]
}
// versionSystem maps a bible version to the Psalter system bible.ToEnglishRef
// expects: vul/grb/wuj follow the Vulgate/Septuagint numbering, drb follows
// the Hebrew chapter with the DRB title-fold verse shift. Ported from
// ewangelia.py PSALM_SYSTEM.
var versionSystem = map[string]string{
"vul": "vulgate",
"grb": "vulgate",
"wuj": "vulgate",
"drb": "drb",
}
func system(version string) string {
if s, ok := versionSystem[version]; ok {
return s
}
return "vulgate"
}
const incipit = "Słowa Ewangelii"
// GatherVersion returns (label, blocks) for one version of one reading
// section. Each block is a paragraph (Polish) or a verse line (bible
// versions); on any failure the blocks hold a single short note instead.
//
// lectionary selects how sec.Citation is read: "new" (modern/niedziela.pl)
// citations are Polish and go through bible.ToEnglishRef; "traditional"
// (missalemeum) citations are already English kjv-style and are used as-is.
// lang selects the UI chrome language the label comes from (see
// internal/i18n); it never affects the verse text/citation itself.
func GatherVersion(version string, sec liturgy.Section, lectionary, lang string) (label string, blocks []string) {
label = versionLabel(version, lang)
if version == "pl" {
return label, gatherPL(sec)
}
ref, err := resolveRef(version, sec, lectionary)
if err != nil {
return label, []string{err.Error()}
}
verses, missing := bible.Lookup(version, ref)
if len(verses) == 0 {
return label, []string{fmt.Sprintf("(brak w „%s”)", version)}
}
blocks = make([]string, 0, len(verses))
for _, v := range verses {
blocks = append(blocks, fmt.Sprintf("%d:%d %s", v.Chapter, v.Verse, v.Text))
}
if len(missing) > 0 {
blocks = append(blocks, fmt.Sprintf("(brak w „%s”: %s)", version, strings.Join(missing, ", ")))
}
return label, blocks
}
// resolveRef resolves a non-"pl" version's bible.Lookup reference for sec:
// sec.Citation (or, failing that, the citation extracted from sec.Heading),
// converted to English/kjv-style via bible.ToEnglishRef when
// lectionary=="new" (a "traditional" citation is already English-style and
// used as-is). Shared by GatherVersion and GatherVerses so both apply the
// exact same resolution.
func resolveRef(version string, sec liturgy.Section, lectionary string) (string, error) {
citation := sec.Citation
if citation == "" {
if c, err := liturgy.ExtractCitation(sec.Heading); err == nil {
citation = c
}
}
if citation == "" {
return "", fmt.Errorf("(brak odwołania)")
}
if lectionary != "new" {
return citation, nil
}
ref, err := bible.ToEnglishRef(citation, system(version))
if err != nil {
return "", fmt.Errorf("(brak odwołania: %w)", err)
}
return ref, nil
}
// GatherVerses returns one version's verses for a section as raw bible.Verse
// structs (for column/interlinear alignment). versified is false for "pl"
// (paragraph text, no verse numbers) and on any resolution/lookup failure --
// callers fall back to GatherVersion's string blocks for those. lang selects
// the UI chrome language the label comes from, same as GatherVersion.
func GatherVerses(version string, sec liturgy.Section, lectionary, lang string) (label string, verses []bible.Verse, versified bool) {
label = versionLabel(version, lang)
if version == "pl" {
return label, nil, false
}
ref, err := resolveRef(version, sec, lectionary)
if err != nil {
return label, nil, false
}
verses, _ = bible.Lookup(version, ref)
if len(verses) == 0 {
return label, nil, false
}
return label, verses, true
}
// gatherPL returns the section's paragraphs, one block per paragraph, with
// the liturgical incipit ("Słowa Ewangelii według ...") dropped and repeated
// blocks (a responsorial psalm's refrain) deduped to their first occurrence.
func gatherPL(sec liturgy.Section) []string {
var blocks []string
for _, p := range sec.Paragraphs {
b := strings.Join(p, " ")
if strings.HasPrefix(b, incipit) {
continue
}
blocks = append(blocks, b)
}
seen := map[string]bool{}
deduped := make([]string, 0, len(blocks))
for _, b := range blocks {
if seen[b] {
continue
}
seen[b] = true
deduped = append(deduped, b)
}
return deduped
}
// OfflineVersions drops "pl" (which needs the network fetch of the liturgy
// page) from versions. If "pl" was present but "wuj" (the Polish-language
// bible version) was not, "wuj" takes pl's place, so the offline set still
// carries a Polish column.
func OfflineVersions(versions []string) []string {
hadWuj := false
for _, v := range versions {
if v == "wuj" {
hadWuj = true
break
}
}
out := make([]string, 0, len(versions))
for _, v := range versions {
if v == "pl" {
if hadWuj {
continue
}
out = append(out, "wuj")
continue
}
out = append(out, v)
}
return out
}
// Compare lays the versions of one reading section out as parallel columns,
// side by side, wrapped to fit width. Ports render_compare. lang selects the
// column-header label language (see GatherVersion).
func Compare(secs []liturgy.Section, versions []string, width int, lectionary, lang string) string {
var out []string
for _, sec := range secs {
out = append(out, compareSection(sec, versions, width, lectionary, lang))
}
return strings.Join(out, "\n\n")
}
func compareSection(sec liturgy.Section, versions []string, width int, lectionary, lang string) string {
type column struct {
label string
lines []string
}
n := len(versions)
if n == 0 {
return ""
}
const gap = " "
w := (width - len(gap)*(n-1)) / n
if w < 24 {
w = 24
}
cols := make([]column, 0, n)
height := 0
for _, v := range versions {
label, blocks := GatherVersion(v, sec, lectionary, lang)
var lines []string
for _, b := range blocks {
lines = append(lines, strings.Split(wrap(b, w), "\n")...)
lines = append(lines, "")
}
if len(lines) > 0 {
lines = lines[:len(lines)-1] // drop trailing blank
}
if len(lines) > height {
height = len(lines)
}
cols = append(cols, column{label: label, lines: lines})
}
labelCells := make([]string, n)
dashCells := make([]string, n)
for i, c := range cols {
labelCells[i] = ljust(truncate(c.label, w), w)
dashCells[i] = strings.Repeat("-", w)
}
rows := []string{
strings.TrimRight(strings.Join(labelCells, gap), " "),
strings.Join(dashCells, gap),
}
for i := 0; i < height; i++ {
cells := make([]string, n)
for ci, c := range cols {
cell := ""
if i < len(c.lines) {
cell = c.lines[i]
}
cells[ci] = ljust(cell, w)
}
rows = append(rows, strings.TrimRight(strings.Join(cells, gap), " "))
}
return strings.Join(rows, "\n")
}
// wrap wraps line to width, greedily packing whole words onto each output
// line and never splitting a word (even one longer than width), matching
// ewangelia.py's textwrap.fill(..., break_long_words=False,
// break_on_hyphens=False).
func wrap(line string, width int) string {
words := strings.Fields(line)
if len(words) == 0 {
return ""
}
var out []string
cur := words[0]
for _, word := range words[1:] {
if len([]rune(cur))+1+len([]rune(word)) <= width {
cur += " " + word
} else {
out = append(out, cur)
cur = word
}
}
out = append(out, cur)
return strings.Join(out, "\n")
}
func ljust(s string, w int) string {
if n := w - len([]rune(s)); n > 0 {
return s + strings.Repeat(" ", n)
}
return s
}
func truncate(s string, w int) string {
r := []rune(s)
if len(r) <= w {
return s
}
return string(r[:w])
}
|