diff --git a/Makefile b/Makefile index cadbc6e..7463928 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all, clean +.PHONY: all, clean-dist, clean ifeq ($(origin IMAGE_DIR),undefined) $(error $$IMAGE_DIR is not set) @@ -28,20 +28,30 @@ dist_html := $(dist_images:$(DIST_DIR)/%.jpg=$(DIST_DIR)/%.html) ### RULES ### -all: $(dist_html) $(dist_extra_files) +all: $(dist_html) $(dist_extra_files) $(DIST_DIR)/index.html -clean: - rm -rf $(DIST_DIR) $(BUILD_DIR) +clean-dist: + rm -rf $(DIST_DIR) + +clean: clean-dist + rm -rf $(BUILD_DIR) $(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 @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) - 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/% @mkdir -p $(DIST_DIR)/extra diff --git a/build-index.py b/build-index.py new file mode 100755 index 0000000..76c7fe6 --- /dev/null +++ b/build-index.py @@ -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) diff --git a/extract-metadata.py b/extract-metadata.py index 3b0f6ac..d26faa7 100755 --- a/extract-metadata.py +++ b/extract-metadata.py @@ -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) stat_data = extract_stat_data(image_path) @@ -67,7 +67,9 @@ def extract_metadata(image_path: Path, *, root: Path): "path": { "name": image_path.name, "relativeToRoot": str(image_path.relative_to(root)), + "distRelativeToRoot": str(dist_path.relative_to(root)), }, + "key": key, } print(json.dumps(metadata, indent=2)) diff --git a/flake.nix b/flake.nix index 0b2c4a1..17fab5e 100644 --- a/flake.nix +++ b/flake.nix @@ -23,6 +23,7 @@ packages.handlebars-rust pkgs.imagemagick pkgs.gnumake + pkgs.jq (pkgs.python3.withPackages (ps: [ps.clize ps.exif])) ]; }; diff --git a/index.hbs b/index.hbs new file mode 100644 index 0000000..f9ebbb7 --- /dev/null +++ b/index.hbs @@ -0,0 +1,17 @@ + + +
+ +