From 0eccf41ff404029bd6f39f06b0a14f1643ddbdda Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 16 Jun 2026 22:34:54 +0300 Subject: wn0 batch preview-thumbnail rendering per page Each thumbnail on a preview page (page-N.html) used to be rendered by its own "template preview" call, paying the full source_template_file cost -- an "env -i bash" invocation -- per thumbnail. With MAXPREVIEWS thumbnails per page that was N template renders per page just for the grid. render_full_preview_page now builds the markup for ALL of a page's thumbnails in bash (build_preview_thumbnail / append_preview_thumbnail) and emits the whole grid in ONE render via a new previewpage.tmpl that takes the pre-built HTML through a context_raw "preview_thumbs" field -- the same pattern the stats filter galleries (camera.tmpl) already use. Per-thumbnail markup is byte-identical to the old preview.tmpl output: same structure, order, HTML escaping and seeded "slow" animation class. Header and footer stay as their own template calls, so a page now costs ~1 previewpage render + header/footer instead of N + chrome. The parallel job-pool integration and failure contract are unchanged: each preview page is still one background render job. Added the render_preview_thumbs_html field spec (hn0 dispatch pattern, context_raw kind), registered previewpage in the validate_template_dir required templates and in the required-context-vars test expectations, and pointed the four generation template-failure tests at previewpage.tmpl (generation no longer renders preview.tmpl). The standalone "template preview" engine unit tests keep exercising preview.tmpl, which still ships. Verified byte-identical output: generated the fixture album (including a spaces/special-char filename) twice with RANDOM_SEED=42 using the parent commit's bin/shuriken vs the new bin; every .html file is identical. just test, just shellcheck, just check-generated and git diff --check all pass. Co-Authored-By: Claude Opus 4.8 --- bin/shuriken | 70 +++++++++++++++++++++++++------- share/templates/default/previewpage.tmpl | 14 +++++++ src/lib/album-render.source.sh | 68 ++++++++++++++++++++++++------- src/lib/config.validate.source.sh | 1 + src/lib/template.source.sh | 1 + tests/cli.sh | 12 +++--- 6 files changed, 133 insertions(+), 33 deletions(-) create mode 100644 share/templates/default/previewpage.tmpl diff --git a/bin/shuriken b/bin/shuriken index e7109db..1b4fcf7 100755 --- a/bin/shuriken +++ b/bin/shuriken @@ -660,6 +660,7 @@ declare -ra TEMPLATE_RENDER_FIELD_SPECS=( 'render_photos_dir_html|context_html|photos_dir|photos_dir|splash details view' 'render_prev_html|context_html|prev|prev|prev' 'render_preview_num_html|context_html|preview_num|preview_num|preview details view' + 'render_preview_thumbs_html|context_raw|preview_thumbs|preview_thumbs|previewpage' 'render_redirect_page_html|context_html|redirect_page|redirect_page|redirect' 'render_show_header_bar|context_raw|show_header_bar|show_header_bar|header' 'render_stats_body_html|context_raw|stats_body|stats_body|stats' @@ -2335,12 +2336,19 @@ render_full_preview_page() { 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. + local preview_thumbs='' for photo in "$@"; do (( ++preview_num )) - render_preview_thumbnail \ - "$page_name" "$html_dir" "$thumbs_dir" "$backhref" \ - "$page_num" "$preview_num" "$photo" + append_preview_thumbnail preview_thumbs \ + "$thumbs_dir" "$backhref" "$page_num" "$preview_num" "$photo" done + template previewpage "$page_name.html" \ + html_dir "$html_dir" \ + preview_thumbs "$preview_thumbs" if [ -n "$next_page" ]; then finish_preview_page_with_next \ @@ -2404,25 +2412,58 @@ queue_preview_page_render_job() { render_job_labels_ref["$!"]="template render job for preview $page_name" } -render_preview_thumbnail() { - local -r page_name="$1"; shift - local -r html_dir="$1"; shift +# Append one thumbnail's markup to a page's accumulating thumbnail-grid buffer. +# Produces exactly the bytes the old per-thumbnail preview.tmpl emitted (the +# block), so batching +# all thumbnails into one previewpage render stays byte-identical. Every +# interpolated value is HTML-escaped like the template's context_html fields; the +# seeded "slow" animation class is preserved exactly. Blocks are separated by a +# newline; the previewpage template adds the single trailing newline, matching +# the old N sequential renders. +append_preview_thumbnail() { + local -n buffer_ref="$1"; shift local -r thumbs_dir="$1"; shift local -r backhref="$1"; shift local -r page_num="$1"; shift local -r preview_num="$1"; shift local -r photo_file="$1"; shift local animation_class + local block animation_class=$(random_animation_css_class slow "$photo_file") - template preview "$page_name.html" \ - html_dir "$html_dir" \ - backhref "$backhref" \ - thumbs_dir "$thumbs_dir" \ - page_num "$page_num" \ - preview_num "$preview_num" \ - photo "$photo_file" \ - animation_class "$animation_class" + block=$(build_preview_thumbnail \ + "$thumbs_dir" "$backhref" "$page_num" "$preview_num" "$photo_file" \ + "$animation_class") + if [ -z "$buffer_ref" ]; then + buffer_ref="$block" + else + buffer_ref+=$'\n'"$block" + fi +} + +# Render the HTML for a single preview thumbnail (HTML-escaping every value the +# way preview.tmpl's context_html fields did). Returned without a trailing +# newline so callers control separators. +build_preview_thumbnail() { + local -r thumbs_dir="$1"; shift + local -r backhref="$1"; shift + local -r page_num="$1"; shift + local -r preview_num="$1"; shift + local -r photo_file="$1"; shift + local -r animation_class="$1"; shift + local photo_html + local anim_html + local backhref_html + local thumbs_dir_html + + photo_html=$(_html_escape "$photo_file") + anim_html=$(_html_escape "$animation_class") + backhref_html=$(_html_escape "$backhref") + thumbs_dir_html=$(_html_escape "$thumbs_dir") + printf '\n' \ + "'$photo_html'" "'$page_num-$preview_num.html'" + printf " \n" \ + "$anim_html" "$backhref_html" "$thumbs_dir_html" "$photo_html" } render_view_page() { @@ -4848,6 +4889,7 @@ validate_template_dir() { next prev preview + previewpage redirect view ) diff --git a/share/templates/default/previewpage.tmpl b/share/templates/default/previewpage.tmpl new file mode 100644 index 0000000..9f42f84 --- /dev/null +++ b/share/templates/default/previewpage.tmpl @@ -0,0 +1,14 @@ +# Preview-page thumbnail grid (task wn0). The album preview page (page-N.html) +# used to render EACH thumbnail with its own "template preview" call, paying the +# full source_template_file cost (an "env -i bash" invocation) per thumbnail. +# render_full_preview_page now pre-builds the markup for ALL of a page's +# thumbnails in bash (render_preview_thumbnail / build into one string) and hands +# the whole grid in through the raw context field render_preview_thumbs_html, so +# this template emits every thumbnail for the page in ONE render call instead of +# N. The per-thumbnail markup is byte-identical to the old preview.tmpl output +# (same structure, order +# and seeded animation classes). The surrounding header/footer still come from +# their own templates, which render_full_preview_page emits around this one. +cat < block), so batching +# all thumbnails into one previewpage render stays byte-identical. Every +# interpolated value is HTML-escaped like the template's context_html fields; the +# seeded "slow" animation class is preserved exactly. Blocks are separated by a +# newline; the previewpage template adds the single trailing newline, matching +# the old N sequential renders. +append_preview_thumbnail() { + local -n buffer_ref="$1"; shift local -r thumbs_dir="$1"; shift local -r backhref="$1"; shift local -r page_num="$1"; shift local -r preview_num="$1"; shift local -r photo_file="$1"; shift local animation_class + local block animation_class=$(random_animation_css_class slow "$photo_file") - template preview "$page_name.html" \ - html_dir "$html_dir" \ - backhref "$backhref" \ - thumbs_dir "$thumbs_dir" \ - page_num "$page_num" \ - preview_num "$preview_num" \ - photo "$photo_file" \ - animation_class "$animation_class" + block=$(build_preview_thumbnail \ + "$thumbs_dir" "$backhref" "$page_num" "$preview_num" "$photo_file" \ + "$animation_class") + if [ -z "$buffer_ref" ]; then + buffer_ref="$block" + else + buffer_ref+=$'\n'"$block" + fi +} + +# Render the HTML for a single preview thumbnail (HTML-escaping every value the +# way preview.tmpl's context_html fields did). Returned without a trailing +# newline so callers control separators. +build_preview_thumbnail() { + local -r thumbs_dir="$1"; shift + local -r backhref="$1"; shift + local -r page_num="$1"; shift + local -r preview_num="$1"; shift + local -r photo_file="$1"; shift + local -r animation_class="$1"; shift + local photo_html + local anim_html + local backhref_html + local thumbs_dir_html + + photo_html=$(_html_escape "$photo_file") + anim_html=$(_html_escape "$animation_class") + backhref_html=$(_html_escape "$backhref") + thumbs_dir_html=$(_html_escape "$thumbs_dir") + printf '\n' \ + "'$photo_html'" "'$page_num-$preview_num.html'" + printf " \n" \ + "$anim_html" "$backhref_html" "$thumbs_dir_html" "$photo_html" } render_view_page() { diff --git a/src/lib/config.validate.source.sh b/src/lib/config.validate.source.sh index fda20f0..7519094 100644 --- a/src/lib/config.validate.source.sh +++ b/src/lib/config.validate.source.sh @@ -101,6 +101,7 @@ validate_template_dir() { next prev preview + previewpage redirect view ) diff --git a/src/lib/template.source.sh b/src/lib/template.source.sh index 13a2988..cd44a45 100644 --- a/src/lib/template.source.sh +++ b/src/lib/template.source.sh @@ -165,6 +165,7 @@ declare -ra TEMPLATE_RENDER_FIELD_SPECS=( 'render_photos_dir_html|context_html|photos_dir|photos_dir|splash details view' 'render_prev_html|context_html|prev|prev|prev' 'render_preview_num_html|context_html|preview_num|preview_num|preview details view' + 'render_preview_thumbs_html|context_raw|preview_thumbs|preview_thumbs|previewpage' 'render_redirect_page_html|context_html|redirect_page|redirect_page|redirect' 'render_show_header_bar|context_raw|show_header_bar|show_header_bar|header' 'render_stats_body_html|context_raw|stats_body|stats_body|stats' diff --git a/tests/cli.sh b/tests/cli.sh index fdd2cec..f7ad977 100755 --- a/tests/cli.sh +++ b/tests/cli.sh @@ -3780,7 +3780,7 @@ test_generate_template_failure_preserves_dist() { PATH="$fake_bin:$PATH" \ test::generate_fixture_images "$TEST_TMPDIR/incoming" cp -R "$TEST_REPO_ROOT/share/templates/default" "$template_dir" - printf 'return 42\n' > "$template_dir/preview.tmpl" + printf 'return 42\n' > "$template_dir/previewpage.tmpl" mkdir -p "$TEST_TMPDIR/dist" printf 'old index\n' > "$TEST_TMPDIR/dist/index.html" test::write_album_config \ @@ -3794,7 +3794,7 @@ test_generate_template_failure_preserves_dist() { test::capture_failure_output "$TEST_SHURIKEN" --generate ) - test::assert_contains 'Rendering preview template into ' "$output" + test::assert_contains 'Rendering previewpage template into ' "$output" test "$(<"$TEST_TMPDIR/dist/index.html")" = 'old index' test::assert_path_absent "$TEST_TMPDIR/dist/photos/01-landscape.jpg" test::assert_path_absent "$TEST_TMPDIR/dist/shuriken.json" @@ -3819,7 +3819,7 @@ test_generate_templates_cannot_read_generation_locals() { cp -R "$TEST_REPO_ROOT/share/templates/default" "$template_dir" # shellcheck disable=SC2016 printf 'printf "legacy num: %%s\\n" "${num}"\n' \ - > "$template_dir/preview.tmpl" + > "$template_dir/previewpage.tmpl" mkdir -p "$TEST_TMPDIR/dist" printf 'old index\n' > "$TEST_TMPDIR/dist/index.html" test::write_album_config \ @@ -3858,7 +3858,7 @@ test_generate_templates_cannot_read_renderer_internals() { cp -R "$TEST_REPO_ROOT/share/templates/default" "$template_dir" # shellcheck disable=SC2016 printf 'printf "context key: %%s\\n" "${context_key}"\n' \ - > "$template_dir/preview.tmpl" + > "$template_dir/previewpage.tmpl" mkdir -p "$TEST_TMPDIR/dist" printf 'old index\n' > "$TEST_TMPDIR/dist/index.html" test::write_album_config \ @@ -3897,7 +3897,7 @@ test_generate_templates_cannot_read_serialized_context_hook() { cp -R "$TEST_REPO_ROOT/share/templates/default" "$template_dir" # shellcheck disable=SC2016 printf 'printf "bash env: %%s\\n" "${BASH_ENV}"\n' \ - > "$template_dir/preview.tmpl" + > "$template_dir/previewpage.tmpl" mkdir -p "$TEST_TMPDIR/dist" printf 'old index\n' > "$TEST_TMPDIR/dist/index.html" test::write_album_config \ @@ -3932,6 +3932,7 @@ header:backhref background_image blurs_dir html_dir show_header_bar next:html_dir next prev:html_dir prev preview:animation_class backhref html_dir page_num photo preview_num thumbs_dir +previewpage:html_dir preview_thumbs redirect:html_dir redirect_page splash:backhref background_image blurs_dir enter_page html_dir photo photos_dir stats:backhref html_dir stats_body @@ -3951,6 +3952,7 @@ declare -a template_names=( next prev preview + previewpage redirect splash stats -- cgit v1.2.3