#!/bin/sh
# gipfel2html — generate the modern, CSS-only gipfelweb pages.
#
# gipfel2html page one image page (peaks + photos)
# gipfel2html gallery thumbnail gallery index
#
# Markers are positioned in percent of the slide so the image can
# scale freely (responsive default; a CSS-only checkbox switches to
# full resolution with horizontal scrolling). Interaction is pure CSS:
# hover for mouse, :focus-within for keyboard, :target for touch.
set -e
# awk must print dots as decimal separators regardless of locale
LC_ALL=C
export LC_ALL
MODE="$1"
AWK=awk
# percent of slide dimension
pct() { $AWK -v v="$1" -v m="$2" 'BEGIN { printf "%.2f", v * 100 / m }'; }
# integer percent (for threshold comparisons)
ipct() { $AWK -v v="$1" -v m="$2" 'BEGIN { printf "%d", v * 100 / m }'; }
# meters -> "12.3 km" / "850 m"
fmt_dist() {
$AWK -v d="$1" 'BEGIN { if (d >= 1000) printf "%.1f km", d / 1000; else printf "%.0f m", d }'
}
# edge classes so popups stay inside the image: below if near the top,
# left/right aligned if near a horizontal edge
edge_classes() {
xp="$1"; yp="$2"
cls=""
[ "$yp" -lt 25 ] && cls="below"
if [ "$xp" -lt 12 ]; then cls="$cls left"
elif [ "$xp" -gt 88 ]; then cls="$cls right"
fi
echo "$cls"
}
title_of() {
# display title for image base name $1: %.title file, else the name
if [ -e "$1.title" ]; then cat "$1.title"; else echo "$1"; fi
}
head() {
cat << EOF
$1 — gipfel
EOF
}
if [ "$MODE" = "gallery" ]; then
shift
head "gipfelweb" ""
cat << EOF
EOF
# note: no `cmd | while` pipes here — the loop body increments the
# marker counters and must run in the current shell
if [ -e "$HILLS" ]; then
i=0
while IFS=" " read -r name height x y dist rest; do
case "$name" in \#*|"") continue ;; esac
xp=$(pct "$x" "$W"); yp=$(pct "$y" "$H")
cls=$(edge_classes "$(ipct "$x" "$W")" "$(ipct "$y" "$H")")
d=$(fmt_dist "$dist")
echo "
EOF
# related images: other images in which this one also appears
rel=$(awk -F'\t' -v n="${FILE}.jpg" '$1 == n { print FILENAME; exit }' \
gipfelweb/gipfel/*.imgs 2>/dev/null || true)
if [ -n "$rel" ]; then
echo '
Visible on these images
'
for f in $rel; do
img=$(basename "${f%.imgs}")
[ "$img" = "$FILE" ] && continue
echo ""
done
echo ''
fi
cat << EOF