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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
// Command clectio-gen emits the compiled data for clectio, the tiny suckless-C
// daily-readings build. It reuses lectio's validated calendar + lectionary
// engine to compute, for every day in a year range, the day's name, colour, and
// readings -- then writes two artifacts:
//
// liturgy_<form>.h : corpus-INDEPENDENT C tables (days, readings, citations,
// and verse KEYS as indices) -- shipped, frozen.
// verses_<form>.keys : the ordered (book,chapter,verse) keys, one per line --
// shipped, resolved to text at clectio build time (mktext)
// against whatever Vulgate-numbered corpus is compiled in.
//
// So the hard liturgical logic stays here (Go, oracle-validated); the C side is
// a dumb lookup + renderer, and swapping the Bible never touches this generator.
//
// Usage: clectio-gen <new|old> <startYear> <endYear> <outdir>
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/lukaszkasprzak/lectio/internal/bible"
"github.com/lukaszkasprzak/lectio/internal/config"
"github.com/lukaszkasprzak/lectio/internal/liturgy"
"github.com/lukaszkasprzak/lectio/internal/readings"
)
// colourIndex maps a liturgical colour to the small enum clectio prints.
var colourIndex = map[string]int{
"green": 0, "white": 1, "red": 2, "violet": 3, "rose": 4, "black": 5,
}
// partLabel is the English section label clectio prints, by PartID.
func partLabel(id string) string {
switch id {
case "pierwsze_czytanie":
return "First Reading"
case "psalm":
return "Responsorial Psalm"
case "drugie_czytanie":
return "Second Reading"
case "aklamacja":
return "Gospel Acclamation"
case "ewangelia", "evangelium":
return "Gospel"
case "epistola":
return "Epistle"
}
switch {
case strings.HasPrefix(id, "lectio"):
return "Lesson"
case strings.HasPrefix(id, "prophetia"):
return "Prophecy"
}
return "Reading"
}
// intern assigns a stable index to each distinct string, preserving first-seen
// order.
type intern struct {
idx map[string]int
list []string
}
func newIntern() *intern { return &intern{idx: map[string]int{}} }
func (n *intern) get(s string) int {
if i, ok := n.idx[s]; ok {
return i
}
i := len(n.list)
n.idx[s] = i
n.list = append(n.list, s)
return i
}
// reading is one distinct pericope: its part label, citation in each sigla, and
// the verse-key indices that make it up.
type reading struct {
part, citeEN, citeLA int
verses []int
}
// day is one distinct liturgy: its name, colour, and ordered reading indices.
type day struct {
name, colour int
readings []int
}
func main() {
if len(os.Args) != 5 {
fmt.Fprintln(os.Stderr, "usage: clectio-gen <new|old> <startYear> <endYear> <outdir>")
os.Exit(2)
}
form := os.Args[1] // "new" (OF) or "old" (EF)
lect := "new"
tag := "of"
if form == "old" {
lect, tag = "traditional", "ef"
}
y0, _ := strconv.Atoi(os.Args[2])
y1, _ := strconv.Atoi(os.Args[3])
outdir := os.Args[4]
base := func(sigla string) config.Config {
c := config.Default()
c.UILanguage = "en"
c.Lectionary = lect
c.SiglaStyle = sigla
return c
}
cfgEN, cfgLA := base("english"), base("latin")
names := newIntern()
parts := newIntern()
citesEN := newIntern()
citesLA := newIntern()
verseKeys := newIntern() // key "Book\tChap\tVerse" -> verse index
var readingList []reading
readingIdx := map[string]int{}
var dayList []day
dayIdx := map[string]int{}
var cal []int
internReading := func(secEN, secLA liturgy.Section) int {
keys := bible.LookupKeyed("vul", resolveRef(secEN, form))
if len(keys) == 0 {
return -1
}
vids := make([]int, len(keys))
for i, k := range keys {
vids[i] = verseKeys.get(fmt.Sprintf("%s\t%d\t%d", k.Book, k.Chapter, k.Verse))
}
r := reading{
part: parts.get(partLabel(secEN.PartID)),
citeEN: citesEN.get(secEN.Citation),
citeLA: citesLA.get(secLA.Citation),
verses: vids,
}
key := fmt.Sprintf("%d|%d|%d|%v", r.part, r.citeEN, r.citeLA, vids)
if i, ok := readingIdx[key]; ok {
return i
}
i := len(readingList)
readingIdx[key] = i
readingList = append(readingList, r)
return i
}
start := time.Date(y0, 1, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(y1, 12, 31, 0, 0, 0, 0, time.UTC)
for d := start; !d.After(end); d = d.AddDate(0, 0, 1) {
ds := d.Format("2006-01-02")
secsEN, info, err := readings.Load(cfgEN, readings.Options{Date: ds, All: true})
if err != nil {
fmt.Fprintf(os.Stderr, "clectio-gen: %s: %v\n", ds, err)
os.Exit(1)
}
secsLA, _, _ := readings.Load(cfgLA, readings.Options{Date: ds, All: true})
var rids []int
for i, secEN := range secsEN {
secLA := secEN
if i < len(secsLA) {
secLA = secsLA[i]
}
if id := internReading(secEN, secLA); id >= 0 {
rids = append(rids, id)
}
}
dy := day{
name: names.get(info.Name),
colour: colourIndex[info.Colour],
readings: rids,
}
key := fmt.Sprintf("%d|%d|%v", dy.name, dy.colour, rids)
id, ok := dayIdx[key]
if !ok {
id = len(dayList)
dayIdx[key] = id
dayList = append(dayList, dy)
}
cal = append(cal, id)
}
if err := os.MkdirAll(outdir, 0o755); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
writeKeys(filepath.Join(outdir, "verses_"+tag+".keys"), verseKeys.list)
writeHeader(filepath.Join(outdir, "liturgy_"+tag+".h"), tag, y0,
names.list, parts.list, citesEN.list, citesLA.list, readingList, dayList, cal)
fmt.Printf("clectio-gen %s: %d days, %d distinct days, %d readings, %d verses\n",
tag, len(cal), len(dayList), len(readingList), len(verseKeys.list))
}
// resolveRef mirrors render.resolveRef: prefer the English-canonical Ref, and
// for the Ordinary Form renumber the Psalms to the Vulgate the keys come from.
func resolveRef(sec liturgy.Section, form string) string {
cit := sec.Ref
if cit == "" {
cit = sec.Citation
}
if cit == "" {
if c, err := liturgy.ExtractCitation(sec.Heading); err == nil {
cit = c
}
}
if cit == "" {
return ""
}
if form != "new" {
return cit // EF citations are already Vulgate-numbered
}
return bible.OFRef(cit, "vulgate")
}
func writeKeys(path string, keys []string) {
f, err := os.Create(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
w := bufio.NewWriter(f)
for _, k := range keys {
w.WriteString(k + "\n")
}
w.Flush()
}
func cstr(s string) string {
r := strings.NewReplacer(`\`, `\\`, `"`, `\"`, "\t", `\t`)
return `"` + r.Replace(s) + `"`
}
func writeHeader(path, tag string, y0 int, names, parts, citesEN, citesLA []string,
readings []reading, days []day, cal []int) {
f, err := os.Create(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
w := bufio.NewWriter(f)
defer w.Flush()
guard := "LITURGY_" + strings.ToUpper(tag) + "_H"
fmt.Fprintf(w, "/* generated by clectio-gen -- do not edit */\n#ifndef %s\n#define %s\n\n", guard, guard)
fmt.Fprintf(w, "#define EPOCH_Y %d\n#define EPOCH_M 1\n#define EPOCH_D 1\n#define NDAYS %d\n\n", y0, len(cal))
strArr := func(name string, xs []string) {
fmt.Fprintf(w, "static const char *const %s[] = {\n", name)
for _, s := range xs {
fmt.Fprintf(w, "\t%s,\n", cstr(s))
}
fmt.Fprintf(w, "};\n\n")
}
strArr("names", names)
strArr("parts", parts)
strArr("cites_en", citesEN)
strArr("cites_la", citesLA)
// Flat verse-index pool; each reading points at a [voff,voff+vlen) slice.
var vpool []int
fmt.Fprintf(w, "static const unsigned short vpool[] = {")
for _, r := range readings {
for _, v := range r.verses {
vpool = append(vpool, v)
}
}
for i, v := range vpool {
if i%16 == 0 {
fmt.Fprintf(w, "\n\t")
}
fmt.Fprintf(w, "%d,", v)
}
fmt.Fprintf(w, "\n};\n\n")
fmt.Fprintf(w, "typedef struct { unsigned short part, cite, voff, vlen; } Reading;\n")
fmt.Fprintf(w, "static const Reading readings[] = {\n")
voff := 0
for _, r := range readings {
fmt.Fprintf(w, "\t{%d,%d,%d,%d},\n", r.part, r.citeEN, voff, len(r.verses))
voff += len(r.verses)
}
fmt.Fprintf(w, "};\n\n")
// cite_la parallels readings[] by index (same part/verses, latin citation).
fmt.Fprintf(w, "static const unsigned short readings_cite_la[] = {")
for i, r := range readings {
if i%16 == 0 {
fmt.Fprintf(w, "\n\t")
}
fmt.Fprintf(w, "%d,", r.citeLA)
}
fmt.Fprintf(w, "\n};\n\n")
// Flat reading-index pool; each day points at a [roff,roff+rlen) slice.
var rpool []int
fmt.Fprintf(w, "static const unsigned short rpool[] = {")
for _, d := range days {
for _, r := range d.readings {
rpool = append(rpool, r)
}
}
for i, r := range rpool {
if i%16 == 0 {
fmt.Fprintf(w, "\n\t")
}
fmt.Fprintf(w, "%d,", r)
}
fmt.Fprintf(w, "\n};\n\n")
fmt.Fprintf(w, "typedef struct { unsigned short name; unsigned char colour; unsigned short roff, rlen; } Day;\n")
fmt.Fprintf(w, "static const Day days[] = {\n")
roff := 0
for _, d := range days {
fmt.Fprintf(w, "\t{%d,%d,%d,%d},\n", d.name, d.colour, roff, len(d.readings))
roff += len(d.readings)
}
fmt.Fprintf(w, "};\n\n")
fmt.Fprintf(w, "static const unsigned short cal[NDAYS] = {")
for i, c := range cal {
if i%16 == 0 {
fmt.Fprintf(w, "\n\t")
}
fmt.Fprintf(w, "%d,", c)
}
fmt.Fprintf(w, "\n};\n\n#endif\n")
}
|