use breadcrumbs instead of up-arrow for child to parent nav

This commit is contained in:
Nick Pegg 2024-08-04 18:58:43 -07:00
parent abf2ecc748
commit 212fe4cad2
2 changed files with 34 additions and 12 deletions

View file

@ -5,9 +5,9 @@ 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
from markdown import markdown
from PIL import Image, UnidentifiedImageError from PIL import Image, UnidentifiedImageError
from rich.progress import Progress, track from rich.progress import Progress, track
from markdown import markdown
from photojawn.config import Config from photojawn.config import Config
@ -17,6 +17,7 @@ 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
@ -90,7 +91,9 @@ 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(path=root_path, children=[], images=[], is_root=True) root_path: ImageDirectory(
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):
@ -98,7 +101,13 @@ def find_images(root_path: Path) -> ImageDirectory:
continue continue
image_dir = image_dirs.get( image_dir = image_dirs.get(
dirpath, ImageDirectory(path=dirpath, children=[], images=[]) dirpath,
ImageDirectory(
path=dirpath,
rel_path=dirpath.relative_to(root_path),
children=[],
images=[],
),
) )
for dirname in sorted(dirnames): for dirname in sorted(dirnames):
@ -199,12 +208,27 @@ def generate_html(config: Config, root_dir: ImageDirectory) -> None:
html_path = album_dir.path / "index.html" html_path = album_dir.path / "index.html"
root_path = root_dir.path.relative_to(html_path.parent, walk_up=True) root_path = root_dir.path.relative_to(html_path.parent, walk_up=True)
# TODO build breadcrumbs here, (href, name)
breadcrumbs = []
if not album_dir.is_root:
crumb_pos = album_dir.path.parent
while crumb_pos != root_dir.path:
breadcrumbs.append(
(
str(crumb_pos.relative_to(album_dir.path, walk_up=True)),
crumb_pos.name,
)
)
crumb_pos = crumb_pos.parent
breadcrumbs.reverse()
logger.debug(f"Rendering {html_path}") logger.debug(f"Rendering {html_path}")
with html_path.open("w") as f: with html_path.open("w") as f:
f.write( f.write(
album_tmpl.render( album_tmpl.render(
root_path=root_path, root_path=root_path,
album_dir=album_dir, album_dir=album_dir,
breadcrumbs=breadcrumbs,
) )
) )

View file

@ -2,15 +2,13 @@
{% block content %} {% block content %}
{% if not album_dir.is_root %} {% if not album_dir.is_root %}
<p> <h1>
<a href=".."> <a href="{{root_path}}">Home</a>
<i class="arrow arrow-up"></i> {% for href, name in breadcrumbs %}
</a> / <a href="{{href}}">{{name}}</a>
</p> {% endfor %}
{% endif %} / {{album_dir.path.name}}
</h1>
{% if not album_dir.is_root %}
<h1>{{album_dir.path.name}}</h1>
{% endif %} {% endif %}
{% if album_dir.description %} {% if album_dir.description %}