Add support for nested covers - if there are no images in a dir, pick the cover from the first child dir

This commit is contained in:
Nick Pegg 2024-08-05 20:00:06 -07:00
parent c08c5609ca
commit f60a14ae21
3 changed files with 23 additions and 12 deletions

View file

@ -101,7 +101,7 @@ def parse_args() -> Namespace:
) )
args = parser.parse_args() args = parser.parse_args()
if not hasattr(args, 'action'): if not hasattr(args, "action"):
parser.print_help() parser.print_help()
sys.exit(0) sys.exit(0)

View file

@ -1,7 +1,7 @@
import logging import logging
import os
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from pprint import pformat
from typing import Iterator, Optional from typing import Iterator, Optional
from jinja2 import Environment, FileSystemLoader, select_autoescape from jinja2 import Environment, FileSystemLoader, select_autoescape
@ -17,7 +17,6 @@ logger = logging.getLogger(__name__)
@dataclass @dataclass
class ImageDirectory: class ImageDirectory:
path: Path path: Path
rel_path: Path # Path relative to the root dir
children: list["ImageDirectory"] children: list["ImageDirectory"]
images: list["ImagePath"] images: list["ImagePath"]
is_root: bool = False is_root: bool = False
@ -75,11 +74,19 @@ def generate(config: Config, album_path: Path) -> None:
""" """
Main generation function Main generation function
""" """
root_dir = find_images(album_path) # Change the working directory to the album_path so that all paths are relative to
logger.debug(pformat(root_dir)) # it when we find images. We need to do this because all the paths in HTML need to
# be relative to it and we don't want to have to do a bunch of path gymnastics to
# re-relative all those paths.
orig_wd = Path.cwd()
os.chdir(album_path)
root_dir = find_images(Path("."))
generate_thumbnails(config, root_dir) generate_thumbnails(config, root_dir)
generate_html(config, root_dir) generate_html(config, root_dir)
os.chdir(orig_wd)
def find_images(root_path: Path) -> ImageDirectory: def find_images(root_path: Path) -> ImageDirectory:
""" """
@ -91,9 +98,7 @@ def find_images(root_path: Path) -> ImageDirectory:
# image_dirs keeps track of all directories we find with images in them, so we can # image_dirs keeps track of all directories we find with images in them, so we can
# attach them as children to parent directories # attach them as children to parent directories
image_dirs: dict[Path, ImageDirectory] = { image_dirs: dict[Path, ImageDirectory] = {
root_path: ImageDirectory( root_path: ImageDirectory(path=root_path, children=[], images=[], is_root=True)
path=root_path, rel_path=Path("."), children=[], images=[], is_root=True
)
} }
for dirpath, dirnames, filenames in root_path.walk(top_down=False): for dirpath, dirnames, filenames in root_path.walk(top_down=False):
@ -104,7 +109,6 @@ def find_images(root_path: Path) -> ImageDirectory:
dirpath, dirpath,
ImageDirectory( ImageDirectory(
path=dirpath, path=dirpath,
rel_path=dirpath.relative_to(root_path),
children=[], children=[],
images=[], images=[],
), ),
@ -141,8 +145,14 @@ def find_images(root_path: Path) -> ImageDirectory:
image_dir.images.append(ip) image_dir.images.append(ip)
if image_dir.cover_path is None and len(image_dir.images) > 0: if image_dir.cover_path is None:
image_dir.cover_path = image_dir.images[0] if len(image_dir.images) > 0:
image_dir.cover_path = image_dir.images[0]
elif len(image_dir.children) > 0:
cover = image_dir.children[0].cover_path
logger.debug(f"nested cover path for {image_dir.path.name}: {cover}")
image_dir.cover_path = cover
image_dirs[image_dir.path] = image_dir image_dirs[image_dir.path] = image_dir
return image_dirs[root_path] return image_dirs[root_path]

View file

@ -22,7 +22,8 @@
<a href="{{child.path.name}}/"> <a href="{{child.path.name}}/">
<div> <div>
{% if child.cover_path %} {% if child.cover_path %}
<img src="{{child.path.name}}/slides/{{child.cover_path.thumbnail_filename()}}" /> <img
src="{{child.cover_path.path.parent.relative_to(album_dir.path)}}/slides/{{child.cover_path.thumbnail_filename()}}" />
{% endif %} {% endif %}
</div> </div>
<div> <div>