← back to Parked Landers
swap-html.py
31 lines
#!/usr/bin/env python3
"""Replace placeholder gallery tiles with real <img> tags in each lander, for images that exist."""
import os, re, glob
BASE = os.path.expanduser("~/Projects/parked-landers/sites")
CSS_RULE = ".tile img{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;display:block}"
swapped = 0
for idx in sorted(glob.glob(os.path.join(BASE, "*", "index.html"))):
dom = os.path.basename(os.path.dirname(idx))
imgdir = os.path.join(os.path.dirname(idx), "img")
html = open(idx).read()
tiles = []
for i in range(1, 7):
fp = os.path.join(imgdir, f"{i:02d}.jpg")
if os.path.exists(fp):
tiles.append(f'<div class="tile"><img src="img/{i:02d}.jpg" alt="{dom} — image {i:02d}" loading="lazy"></div>')
else: # keep placeholder if the image is missing
tiles.append(f'<div class="tile"><span class="ph">Placeholder {i:02d}</span>'
f'<span class="lbl">1600 × 1200 · swap real image</span></div>')
new_gallery = '<div class="gallery" id="gallery">' + "".join(tiles) + '</div>'
html2 = re.sub(r'<div class="gallery" id="gallery">.*?</div>\s*(?=<div class="about">)',
new_gallery + "\n ", html, count=1, flags=re.S)
if CSS_RULE not in html2:
html2 = html2.replace("</style>", CSS_RULE + "\n</style>", 1)
# update the gallery caption to drop "placeholders" once all 6 are real
if all(os.path.exists(os.path.join(imgdir, f"{i:02d}.jpg")) for i in range(1, 7)):
html2 = html2.replace("6 images · placeholders — swap real assets before launch", "6 images")
if html2 != html:
open(idx, "w").write(html2); swapped += 1
print(f"swapped {dom} ({sum(1 for i in range(1,7) if os.path.exists(os.path.join(imgdir,f'{i:02d}.jpg')))}/6 real)")
print(f"done — {swapped} files updated")