Add index.html

This commit is contained in:
Paul Brinkmeier 2025-02-15 02:29:41 +01:00
parent 80f720c418
commit 82b93a3369
6 changed files with 81 additions and 8 deletions

View File

@ -1,4 +1,4 @@
.PHONY: all, clean .PHONY: all, clean-dist, clean
ifeq ($(origin IMAGE_DIR),undefined) ifeq ($(origin IMAGE_DIR),undefined)
$(error $$IMAGE_DIR is not set) $(error $$IMAGE_DIR is not set)
@ -28,20 +28,30 @@ dist_html := $(dist_images:$(DIST_DIR)/%.jpg=$(DIST_DIR)/%.html)
### RULES ### ### RULES ###
all: $(dist_html) $(dist_extra_files) all: $(dist_html) $(dist_extra_files) $(DIST_DIR)/index.html
clean: clean-dist:
rm -rf $(DIST_DIR) $(BUILD_DIR) rm -rf $(DIST_DIR)
clean: clean-dist
rm -rf $(BUILD_DIR)
$(foreach f,$(image_names),$(eval $(call copy_image_rule,$(f)))) $(foreach f,$(image_names),$(eval $(call copy_image_rule,$(f))))
$(BUILD_DIR)/index.json: $(build_metas) build-index.py
./build-index.py $(filter-out build-index.py,$^) > $@
$(BUILD_DIR)/%.meta.json: $(DIST_DIR)/%.jpg extract-metadata.py $(BUILD_DIR)/%.meta.json: $(DIST_DIR)/%.jpg extract-metadata.py
@mkdir -p $(BUILD_DIR) @mkdir -p $(BUILD_DIR)
./extract-metadata.py --root $(DIST_DIR) $< > $@ ./extract-metadata.py --root $(DIST_DIR) --dist-path $(patsubst $(BUILD_DIR)/%.meta.json,$(DIST_DIR)/%.html,$@) --key $(patsubst $(BUILD_DIR)/%.meta.json,%,$@) $< > $@
$(DIST_DIR)/%.html: $(BUILD_DIR)/%.meta.json photo.hbs $(DIST_DIR)/index.html: $(BUILD_DIR)/index.json index.hbs
@mkdir -p $(DIST_DIR) @mkdir -p $(DIST_DIR)
handlebars-cli photo.hbs "$$(cat $<)" > $@ handlebars-cli index.hbs "$$(cat $(BUILD_DIR)/index.json)" > $@
$(DIST_DIR)/%.html: $(BUILD_DIR)/%.meta.json $(BUILD_DIR)/index.json photo.hbs
@mkdir -p $(DIST_DIR)
handlebars-cli photo.hbs "$$(cat $(BUILD_DIR)/index.json | jq .$(patsubst $(BUILD_DIR)/%.meta.json,%,$<))" > $@
$(DIST_DIR)/extra/%: extra/% $(DIST_DIR)/extra/%: extra/%
@mkdir -p $(DIST_DIR)/extra @mkdir -p $(DIST_DIR)/extra

42
build-index.py Executable file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env python3
import clize
import json
import sys
from datetime import datetime
from pathlib import Path
from typing import Any
def get_exif_timestamp(meta: dict[str, Any]) -> int:
try:
dt = meta["exif"]["datetime"]
return datetime.strptime(dt, "%Y-%m-%d %H:%M").timestamp()
except:
return 0
def build_index(*meta_paths: Path):
metas = []
for meta_path in meta_paths:
with meta_path.open(encoding="utf8") as f:
meta = json.load(f)
metas.append(meta)
metas.sort(key=get_exif_timestamp)
metas_dict = {}
for i, meta in enumerate(metas):
meta["navigation"] = {
"previous": metas[i - 1]["path"]["distRelativeToRoot"],
"next": metas[i + 1 - len(metas)]["path"]["distRelativeToRoot"],
}
metas_dict[meta["key"]] = meta
print(json.dumps(metas_dict, indent=2))
if __name__ == "__main__":
clize.run(build_index)

View File

@ -57,7 +57,7 @@ def extract_stat_data(image_path: Path):
def extract_metadata(image_path: Path, *, root: Path): def extract_metadata(image_path: Path, *, root: Path, dist_path: Path, key: str):
exif_data = extract_exif_data(image_path) exif_data = extract_exif_data(image_path)
stat_data = extract_stat_data(image_path) stat_data = extract_stat_data(image_path)
@ -67,7 +67,9 @@ def extract_metadata(image_path: Path, *, root: Path):
"path": { "path": {
"name": image_path.name, "name": image_path.name,
"relativeToRoot": str(image_path.relative_to(root)), "relativeToRoot": str(image_path.relative_to(root)),
"distRelativeToRoot": str(dist_path.relative_to(root)),
}, },
"key": key,
} }
print(json.dumps(metadata, indent=2)) print(json.dumps(metadata, indent=2))

View File

@ -23,6 +23,7 @@
packages.handlebars-rust packages.handlebars-rust
pkgs.imagemagick pkgs.imagemagick
pkgs.gnumake pkgs.gnumake
pkgs.jq
(pkgs.python3.withPackages (ps: [ps.clize ps.exif])) (pkgs.python3.withPackages (ps: [ps.clize ps.exif]))
]; ];
}; };

17
index.hbs Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>photos</title>
<link rel="stylesheet" href="./extra/index.css">
</head>
<body>
<ul>
{{#each this}}
<li>
<a href="/{{ path.distRelativeToRoot }}"><img src="/{{ path.relativeToRoot }}" width=100></a>
</li>
{{/each}}
</ul>
</body>
</html>

View File

@ -12,6 +12,7 @@
</div> </div>
<div class="text-frame"> <div class="text-frame">
<h1>{{ path.name }}</h2> <h1>{{ path.name }}</h2>
<p><a href="/{{ navigation.previous }}">prev</a> · <a href="/{{ navigation.next }}">next</a> · <a href="/">up</a></p>
<p>{{ exif.datetime }}</p> <p>{{ exif.datetime }}</p>
<p> <p>
Exposure Time <strong>{{ exif.exposure_time }}s</strong><br> Exposure Time <strong>{{ exif.exposure_time }}s</strong><br>