summaryrefslogtreecommitdiff
path: root/src/lib/album-render.source.sh
blob: a6012bb3de33b4928d3b760ca099ddcc7d353064 (plain)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# Album page orchestration. After the ar0 split this module owns only the page
# assembly and the job plumbing: it builds the preview pages, the per-photo
# view/details pages, the navigation redirects and the index/splash, driving the
# job_pool_* pool so pages render in parallel. The three concerns it used to
# bundle now live in siblings, all sourced before this file:
#   - album-tile-layout.source.sh   (tile_layout_for/build_tile_block/...)
#   - album-thumbnail-html.source.sh (build_preview_thumbnail/append_preview_grid)
#   - album-photo-select.source.sh  (album_photo_files/album_page_records/splash)
# The generic "pick one seeded-random photo from a dir" used for page backgrounds
# now comes from the shared pick_random_photo (photo-list.source.sh, task br0).
# This module calls into all of these at runtime; all libs are sourced before any
# code runs, so the cross-module calls resolve regardless of source order.
#
# Maps each album photo filename to its view-page basename ("<page>-<preview>")
# as assigned during render_album_pages. Declared globally so it always exists
# for the accessor even when no album was rendered.
#
# This is the album module's PRIVATE backing store (task pn0). Outside callers
# must NOT index it directly: use the album_view_page_for_photo accessor below.
# Keeping the map private behind a documented function decouples consumers (the
# stats filter mini-albums) from how the album internally names or caches view
# pages, so a change to the page-naming scheme stays contained in this module.
declare -gA ALBUM_VIEW_PAGE_BY_PHOTO=()

# Public album API (task pn0): return the view-page basename for a photo, or the
# empty string when the photo was not rendered into the album. The stats filter
# mini-albums call this to link each photo's "Details" to the album's own
# details page, instead of reaching into ALBUM_VIEW_PAGE_BY_PHOTO directly.
album_view_page_for_photo() {
    local -r photo="$1"; shift

    printf '%s' "${ALBUM_VIEW_PAGE_BY_PHOTO[$photo]:-}"
}

start_preview_page() {
    local -r photos_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r page_name="$1"; shift
    local -r header_bar="$1"; shift
    local background_image

    background_image=$(pick_random_photo "$photos_dir" "$page_name")
    template header "$page_name.html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        blurs_dir "$blurs_dir" \
        background_image "$background_image" \
        show_header_bar "$header_bar"
}

finish_preview_page() {
    local -r page_name="$1"; shift
    local -r html_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift

    template footer "$page_name.html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        tarball_name "$tarball_name"
}

finish_preview_page_with_next() {
    local -r page_name="$1"; shift
    local -r html_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -r next_page="$1"; shift
    local -r prev_page="$1"; shift

    template next "$page_name.html" \
        html_dir "$html_dir" \
        next "$next_page" \
        prev "$prev_page"
    finish_preview_page "$page_name" "$html_dir" "$backhref" "$tarball_name"
}

render_previous_page_link() {
    local -r page_name="$1"; shift
    local -r html_dir="$1"; shift
    local -r prev_page="$1"; shift

    template prev "$page_name.html" \
        html_dir "$html_dir" \
        prev "$prev_page"
}

# Assemble one complete preview page (page-N.html) in a single call: header,
# optional previous-page link, every thumbnail in order, then the footer (with
# a next-page link unless this is the last page). The appends to the one page
# file happen here, in sequence, so the per-page ordering is preserved even when
# this whole function runs as one backgrounded render job (parallelism is only
# ACROSS pages, never within a page). The page's photos are the trailing
# positional arguments; each photo's preview index is its 1-based position.
render_full_preview_page() {
    local -r photos_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r thumbs_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -r page_name="$1"; shift
    local -r page_num="$1"; shift
    local -r header_bar="$1"; shift
    local -r prev_page="$1"; shift
    local -r next_page="$1"; shift

    start_preview_page \
        "$photos_dir" "$html_dir" "$blurs_dir" "$backhref" "$page_name" \
        "$header_bar"
    if [ -n "$prev_page" ]; then
        render_previous_page_link "$page_name" "$html_dir" "$prev_page"
    fi

    # Batch all of this page's thumbnails into ONE template call. Building the
    # markup in bash and emitting it via the raw "preview_thumbs" field collapses
    # what used to be N "template preview" renders (one env -i bash per
    # thumbnail) into a single previewpage render per page. append_preview_grid
    # also groups the page's photos into tiles (some subdivided into smaller
    # thumbnails), so the per-page ordering and preview numbering stay here.
    local preview_thumbs=''
    # The album's last page (no "next" link) is the only one allowed to widen a
    # leftover final tile to a full row, so a short final page stays flush.
    local fill_last='no'
    if [ -z "$next_page" ]; then
        fill_last='yes'
    fi
    # The main album's view pages are "<page_num>-<preview_num>.html", so the
    # shared grid builder gets "<page_num>-" as the href prefix.
    append_preview_grid preview_thumbs \
        "$thumbs_dir" "$backhref" "$page_num-" "$fill_last" "$@"
    template previewpage "$page_name.html" \
        html_dir "$html_dir" \
        preview_thumbs "$preview_thumbs"

    if [ -n "$next_page" ]; then
        finish_preview_page_with_next \
            "$page_name" "$html_dir" "$backhref" "$tarball_name" \
            "$next_page" "$prev_page"
    else
        finish_preview_page "$page_name" "$html_dir" "$backhref" "$tarball_name"
    fi
}

# Enqueue one complete preview page as a background render job in the given job
# pool (throttled to IMAGE_JOBS). Mirrors queue_album_view_render_job: job_pool_submit
# waits for a free slot, backgrounds the whole-page assembly, and tracks its
# pid/label so a failed job is reported by job_pool_wait and makes generation
# fail loudly. The page's photos are passed as trailing positional args; the
# background subshell forks a private copy of them, so the caller is free to
# reuse its per-page accumulator for the next page.
queue_preview_page_render_job() {
    local -r photos_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r thumbs_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -r page_name="$1"; shift
    local -r page_num="$1"; shift
    local -r header_bar="$1"; shift
    local -r prev_page="$1"; shift
    local -r next_page="$1"; shift
    local -r pool="$1"; shift
    # Remaining positional args ("$@") are this page's photos in order.

    job_pool_submit "$pool" "template render job for preview $page_name" \
        render_full_preview_page \
        "$photos_dir" \
        "$html_dir" \
        "$thumbs_dir" \
        "$blurs_dir" \
        "$backhref" \
        "$tarball_name" \
        "$page_name" \
        "$page_num" \
        "$header_bar" \
        "$prev_page" \
        "$next_page" \
        "$@"
}

render_view_page() {
    local -r html_dir="$1"; shift
    local -r photos_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -r page_num="$1"; shift
    local -r preview_num="$1"; shift
    local -r photo_file="$1"; shift
    local animation_class
    local exif_tooltip_text

    template header "$page_num-$preview_num.html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        blurs_dir "$blurs_dir" \
        background_image "$photo_file" \
        show_header_bar 'no'

    animation_class=$(random_animation_css_class fast "$photo_file")
    # Reuse the same EXIF tooltip as the details page so hovering the image in
    # the normal view shows the camera/exposure summary (reads the shared
    # identify cache, so no extra ImageMagick work).
    exif_tooltip_text=$(
        photo_exif_tooltip_text "$photo_file" "$INCOMING_DIR/$photo_file"
    )
    template view "$page_num-$preview_num.html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        photos_dir "$photos_dir" \
        page_num "$page_num" \
        preview_num "$preview_num" \
        photo "$photo_file" \
        animation_class "$animation_class" \
        exif_tooltip "$exif_tooltip_text"
    template footer "$page_num-$preview_num.html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        tarball_name "$tarball_name"
}

# Render one photo's *-details.html page. Only called by
# render_photo_view_and_details when DETAILS_PAGE=yes; callers must not invoke
# this directly for a photo when details are disabled, or a dangling file would
# be produced with no page linking to it.
render_details_page() {
    local -r html_dir="$1"; shift
    local -r photos_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -r page_num="$1"; shift
    local -r preview_num="$1"; shift
    local -r photo_file="$1"; shift
    local animation_class
    local exif_details_html
    local exif_tooltip_text

    template header "$page_num-$preview_num-details.html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        blurs_dir "$blurs_dir" \
        background_image "$photo_file" \
        show_header_bar 'no'

    animation_class=$(random_animation_css_class fast "$photo_file")
    exif_details_html=$(
        photo_exif_details_html "$photo_file" "$INCOMING_DIR/$photo_file"
    )
    exif_tooltip_text=$(
        photo_exif_tooltip_text "$photo_file" "$INCOMING_DIR/$photo_file"
    )
    template details "$page_num-$preview_num-details.html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        photos_dir "$photos_dir" \
        page_num "$page_num" \
        preview_num "$preview_num" \
        photo "$photo_file" \
        animation_class "$animation_class" \
        exif_details "$exif_details_html" \
        exif_tooltip "$exif_tooltip_text"
    template footer "$page_num-$preview_num-details.html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        tarball_name "$tarball_name"
}

render_photo_view_and_details() {
    local -r photos_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -r page_num="$1"; shift
    local -r preview_num="$1"; shift
    local -r photo="$1"; shift

    render_view_page \
        "$html_dir" \
        "$photos_dir" \
        "$blurs_dir" \
        "$backhref" \
        "$tarball_name" \
        "$page_num" \
        "$preview_num" \
        "$photo"

    # DETAILS_PAGE=no skips the *-details.html file entirely (view.tmpl's Details
    # link is likewise suppressed via render_details_page_html, so nothing links
    # to it). This is the only place a photo's details page would be rendered, so
    # skipping the call here is sufficient to omit the file for every photo.
    if [ "$DETAILS_PAGE" = yes ]; then
        render_details_page \
            "$html_dir" \
            "$photos_dir" \
            "$blurs_dir" \
            "$backhref" \
            "$tarball_name" \
            "$page_num" \
            "$preview_num" \
            "$photo"
    fi
}

record_rendered_view_page() {
    # shellcheck disable=SC2178
    local -n view_pages_ref="$1"; shift
    # shellcheck disable=SC2178
    local -n last_views_ref="$1"; shift
    local -r page="$1"; shift
    local -r preview="$1"; shift

    if [ -z "${last_views_ref[$page]+set}" ]; then
        view_pages_ref+=("$page")
    fi
    last_views_ref["$page"]="$preview"
}

# Navigation-redirect count single source of truth (task nr0; extended for
# DETAILS_PAGE by task 6v0). Every view page gets ALBUM_VIEW_REDIRECTS_PER_PAGE
# wrap-around redirect files: the prev stub (N-0.html) and the next stub
# (N-(last+1).html). When DETAILS_PAGE=yes, each of those also gets a
# "-details" twin, adding ALBUM_DETAILS_REDIRECTS_PER_PAGE more -- so
# render_page_view_redirects emits 2 files per page with details disabled, 4
# with them enabled. The LAST page additionally emits
# ALBUM_VIEW_REDIRECTS_LAST_PAGE_EXTRA entry stubs (plus their details twins
# when enabled): the 0-MAXPREVIEWS / loop-to-1 stubs that bounce into the
# album. Keep these numbers in lockstep with render_page_view_redirects; the
# dry-run plan predicts redirect_count from them via
# album_redirect_count_for_page_count instead of a magic formula.
declare -gri ALBUM_VIEW_REDIRECTS_PER_PAGE=2
declare -gri ALBUM_DETAILS_REDIRECTS_PER_PAGE=2
declare -gri ALBUM_VIEW_REDIRECTS_LAST_PAGE_EXTRA=1
declare -gri ALBUM_DETAILS_REDIRECTS_LAST_PAGE_EXTRA=1

# Per-page / last-page-extra redirect file counts for the CURRENT DETAILS_PAGE
# setting: the view-only counts, plus the details counts when DETAILS_PAGE=yes.
# Split out of album_redirect_count_for_page_count so that function stays a
# short arithmetic one-liner.
album_redirects_per_page() {
    local -i count=$ALBUM_VIEW_REDIRECTS_PER_PAGE

    if [ "$DETAILS_PAGE" = yes ]; then
        count+=$ALBUM_DETAILS_REDIRECTS_PER_PAGE
    fi
    printf '%d\n' "$count"
}

album_redirects_last_page_extra() {
    local -i count=$ALBUM_VIEW_REDIRECTS_LAST_PAGE_EXTRA

    if [ "$DETAILS_PAGE" = yes ]; then
        count+=$ALBUM_DETAILS_REDIRECTS_LAST_PAGE_EXTRA
    fi
    printf '%d\n' "$count"
}

# Total navigation redirects a run produces for a given number of preview pages:
# album_redirects_per_page per page plus the last page's extra entry stubs.
# Zero pages -> zero redirects (render_view_redirects returns early on an empty
# album). This is the count render_page_view_redirects actually writes across
# all pages, expressed once so the dry-run plan cannot drift from real
# generation.
album_redirect_count_for_page_count() {
    local -ri page_count="$1"; shift
    local -i per_page
    local -i last_page_extra

    if (( page_count <= 0 )); then
        printf '0\n'
        return
    fi
    per_page=$(album_redirects_per_page)
    last_page_extra=$(album_redirects_last_page_extra)
    printf '%d\n' "$(( page_count * per_page + last_page_extra ))"
}

# Render one details-redirect stub, but only when DETAILS_PAGE=yes (mirrors
# render_photo_view_and_details skipping render_details_page entirely). A no-op
# under DETAILS_PAGE=no keeps every "-details.html" navigation stub from ever
# being written, so no generated page can link to a missing one.
_render_details_redirect() {
    local -r html_dir="$1"; shift
    local -r filename="$1"; shift
    local -r target="$1"; shift

    if [ "$DETAILS_PAGE" != yes ]; then
        return
    fi
    template redirect "$filename" html_dir "$html_dir" redirect_page "$target"
}

# Render every navigation redirect for a single view page (the prev/next
# wrap-around stubs that bounce N-0 / N-(last+1) to the neighbouring page).
# Each redirect is its own self-contained file (the template overwrites it), so
# this whole group is safe to run as one independent background job; only the
# files for distinct pages are produced here. The wrap-around redirects for the
# very last page (0-MAXPREVIEWS and the loop-to-1 links) are emitted as part of
# that page's group. Per-page / last-page file counts are fixed by
# album_redirects_per_page / album_redirects_last_page_extra above.
render_page_view_redirects() {
    local -r html_dir="$1"; shift
    local -ri page="$1"; shift
    local -ri lastview="$1"; shift
    local -ri max_page="$1"; shift
    local -r prevredirect="${page}-0"
    local -r nextredirect="${page}-$(( lastview + 1 ))"

    template redirect "$prevredirect.html" \
        html_dir "$html_dir" \
        redirect_page "$(( page - 1 ))-${MAXPREVIEWS}"
    _render_details_redirect "$html_dir" "$prevredirect-details.html" \
        "$(( page - 1 ))-${MAXPREVIEWS}-details"

    if (( page == max_page )); then
        template redirect "0-$MAXPREVIEWS.html" \
            html_dir "$html_dir" \
            redirect_page "${page}-$lastview"
        _render_details_redirect "$html_dir" "0-$MAXPREVIEWS-details.html" \
            "${page}-$lastview-details"
        template redirect "$nextredirect.html" \
            html_dir "$html_dir" \
            redirect_page '1-1'
        _render_details_redirect "$html_dir" "$nextredirect-details.html" \
            '1-1-details'
    else
        template redirect "$nextredirect.html" \
            html_dir "$html_dir" \
            redirect_page "$(( page + 1 ))-1"
        _render_details_redirect "$html_dir" "$nextredirect-details.html" \
            "$(( page + 1 ))-1-details"
    fi
}

# Render all navigation redirects in parallel, one background job per view page,
# throttled to IMAGE_JOBS via the shared template render job pool. This blocks
# until every redirect is on disk (and fails loudly if any job failed), so
# callers can rely on the redirects existing once it returns -- the redirect
# groups are mutually independent, so only timing changes, not output.
render_view_redirects() {
    local -r html_dir="$1"; shift
    # shellcheck disable=SC2178
    local -n view_pages_ref="$1"; shift
    # shellcheck disable=SC2178
    local -n redirect_last_views_ref="$1"; shift
    local max_page
    local page

    if (( ${#view_pages_ref[@]} == 0 )); then
        return
    fi

    # Render job pool (max IMAGE_JOBS concurrent), addressed by the single handle
    # "render_jobs". job_pool_wait returns 1 if any render job failed.
    job_pool_init render_jobs

    max_page=${view_pages_ref[$(( ${#view_pages_ref[@]} - 1 ))]}

    for page in "${view_pages_ref[@]}"; do
        job_pool_submit render_jobs \
            "template render job for redirect page $page" \
            render_page_view_redirects \
            "$html_dir" "$page" "${redirect_last_views_ref[$page]}" \
            "$max_page"
    done

    job_pool_wait render_jobs
}

render_album_index_redirect() {
    local -r html_dir="$1"; shift

    template 'redirect' 'index.html' \
        html_dir "$html_dir" \
        redirect_page 'page-1'
}

render_album_splash_page() {
    local -r photos_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift
    local html='index.html'
    local photo

    if (( $# > 0 )); then
        html="$1"
        shift
    fi

    photo=$(random_splash_photo "$photos_dir" "$blurs_dir")
    template 'splash' "$html" \
        html_dir "$html_dir" \
        backhref "$backhref" \
        blurs_dir "$blurs_dir" \
        background_image "$photo" \
        photos_dir "$photos_dir" \
        photo "$photo" \
        enter_page 'page-1'
}

render_album_index() {
    local -r photos_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift

    if [ "$SPLASH_PAGE" = yes ]; then
        render_album_splash_page "$photos_dir" "$html_dir" "$blurs_dir" \
            "$backhref"
    else
        render_album_index_redirect "$html_dir"
    fi
}

album_page_name() {
    local -r page_num="$1"; shift

    printf 'page-%s\n' "$page_num"
}

queue_album_view_render_job() {
    local -r photos_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -r page_num="$1"; shift
    local -r preview_num="$1"; shift
    local -r photo="$1"; shift
    local -r pool="$1"; shift

    job_pool_submit "$pool" "template render job for photo $photo" \
        render_photo_view_and_details \
        "$photos_dir" \
        "$blurs_dir" \
        "$html_dir" \
        "$backhref" \
        "$tarball_name" \
        "$page_num" \
        "$preview_num" \
        "$photo"
}

# Per-photo bookkeeping shared by the album loop: queue the view+details render
# job, record the page/preview as a rendered view page (for redirect generation)
# and remember the photo -> view-page mapping for the stats mini-albums. Kept
# separate from the preview-page assembly so each concern stays small.
_album_record_view_photo() {
    local -r photos_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -ri page_num="$1"; shift
    local -ri preview_num="$1"; shift
    local -r photo="$1"; shift
    local -r pool="$1"; shift
    local -r view_pages_name="$1"; shift
    local -r last_views_name="$1"; shift

    queue_album_view_render_job \
        "$photos_dir" "$blurs_dir" "$html_dir" "$backhref" "$tarball_name" \
        "$page_num" "$preview_num" "$photo" \
        "$pool"
    record_rendered_view_page "$view_pages_name" "$last_views_name" \
        "$page_num" "$preview_num"
    # Read later through the album_view_page_for_photo accessor (e.g. by the
    # stats filter mini-albums for their Details links); shellcheck cannot see
    # that cross-function use.
    # shellcheck disable=SC2034
    ALBUM_VIEW_PAGE_BY_PHOTO["$photo"]="$page_num-$preview_num"
}

# Process one page record: queue every photo's view+details render job, record
# the view pages (into the rendered_view_pages / rendered_last_views arrays passed
# by NAME), then queue the preview-page render. total_pages and prev_name decide
# the "prev"/"next" links and whether the header bar shows (first page only).
# Side effects only -- run in the same order as the original inline loop body.
_render_album_page() {
    local -r record="$1"; shift
    local -r photos_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r thumbs_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift
    local -ri total_pages="$1"; shift
    local -r prev_name="$1"; shift
    local -r view_pages_name="$1"; shift
    local -r last_views_name="$1"; shift

    local header_bar
    local name
    local next_name
    local -ri page_num=${record%%$'\t'*}
    local photo
    local -i preview_num
    local -a page_photos=()

    name=$(album_page_name "$page_num")
    # The first page shows the header bar; later pages do not (matches the
    # previous start_preview_page calls): prev_name is empty only on page 1.
    if [ -z "$prev_name" ]; then
        header_bar='yes'
    else
        header_bar='no'
    fi

    # Split the tab-separated photo list for this page into an array.
    IFS=$'\t' read -r -a page_photos <<< "${record#*$'\t'}"

    preview_num=0
    for photo in "${page_photos[@]}"; do
        (( ++preview_num ))
        _album_record_view_photo \
            "$photos_dir" "$blurs_dir" "$html_dir" "$backhref" \
            "$tarball_name" "$page_num" "$preview_num" "$photo" \
            render_jobs "$view_pages_name" "$last_views_name"
    done

    # A page has a "next" link unless it is the last record.
    next_name=''
    if (( page_num < total_pages )); then
        next_name=$(album_page_name "$(( page_num + 1 ))")
    fi
    queue_preview_page_render_job \
        "$photos_dir" "$html_dir" "$thumbs_dir" "$blurs_dir" "$backhref" \
        "$tarball_name" "$name" "$page_num" "$header_bar" \
        "$prev_name" "$next_name" \
        render_jobs \
        "${page_photos[@]}"
}

render_album_pages() {
    local -r photos_dir="$1"; shift
    local -r html_dir="$1"; shift
    local -r thumbs_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r backhref="$1"; shift
    local -r tarball_name="$1"; shift

    local prev_name=''
    local record
    # Passed by name to record_rendered_view_page and render_view_redirects.
    # shellcheck disable=SC2034
    local -A rendered_last_views=()
    # shellcheck disable=SC2034
    local -a rendered_view_pages=()
    local -a page_records=()

    # Render job pool (max IMAGE_JOBS concurrent), addressed by the single handle
    # "render_jobs". job_pool_wait returns 1 if any render job failed.
    job_pool_init render_jobs

    # Rebuild the photo -> view-page map for this album from scratch so a
    # re-generate (or a smaller incoming set) does not keep stale entries. The
    # per-photo entries are written in _album_record_view_photo, which shellcheck
    # cannot see from here.
    # shellcheck disable=SC2034
    ALBUM_VIEW_PAGE_BY_PHOTO=()

    # Materialise the full deterministic page layout first so each preview page
    # knows up front whether it has a following page (and thus a "next" link).
    mapfile -t page_records < <(album_page_records "$photos_dir")

    # Walk the page records in order, threading prev_name from page to page so
    # each page's "prev" link points at the one before it. _render_album_page does
    # all the per-page work; the carried prev_name is just the previous page name.
    for record in "${page_records[@]}"; do
        _render_album_page \
            "$record" "$photos_dir" "$html_dir" "$thumbs_dir" "$blurs_dir" \
            "$backhref" "$tarball_name" "${#page_records[@]}" "$prev_name" \
            rendered_view_pages rendered_last_views
        prev_name=$(album_page_name "${record%%$'\t'*}")
    done

    if ! job_pool_wait render_jobs; then
        return 1
    fi
    render_view_redirects "$html_dir" rendered_view_pages rendered_last_views
    render_album_index "$photos_dir" "$html_dir" "$blurs_dir" "$backhref"
}