Skip to content

Commit

Permalink
Update how the full menu is generated
Browse files Browse the repository at this point in the history
Use existing albums attribute on each album to get subalbums.
The albums attribute is sorted by the configured albums_sort_attr.
Add custom sorting and title to the demo gallery.
Remove need for adding the full_tree to the Gallery.
Simplify menu template to be based on albums and their subalbums.
  • Loading branch information
153957 committed Feb 17, 2024
1 parent 54a189f commit 75be088
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 63 deletions.
2 changes: 1 addition & 1 deletion demo/sigal.conf.py
Expand Up @@ -21,7 +21,7 @@
thumb_dir = 'thumbnails'
thumb_size = (280, 140)
thumb_fit = False
albums_sort_attr = 'name'
albums_sort_attr = ['meta.order', 'name']
medias_sort_attr = 'date'
ignore_directories: list[str] = []
ignore_files: list[str] = []
Expand Down
1 change: 1 addition & 0 deletions demo/source/Nine levels deep/index.md
@@ -0,0 +1 @@
title: Inferno
1 change: 1 addition & 0 deletions demo/source/Sequences/Candle/index.md
@@ -0,0 +1 @@
order: 2
1 change: 1 addition & 0 deletions demo/source/Sequences/Tree/index.md
@@ -0,0 +1 @@
order: 1
53 changes: 32 additions & 21 deletions tests/test_full_menu.py
Expand Up @@ -13,26 +13,6 @@ def get_demo_gallery(self) -> Gallery:
settings = read_settings(demo_settings)
return Gallery(settings, ncpu=1)

def test_full_tree(self) -> None:
gallery = self.get_demo_gallery()

full_menu.full_tree(gallery)

with self.subTest('Top level albums'):
self.assertEqual(
['Long menu', 'Nine levels deep', 'Poles', 'Sequences', 'Time-Lapse'],
list(gallery.full_tree.keys()),
)

with self.subTest('Subalbums'):
self.assertEqual(
['Candle', 'Tree'],
list(gallery.full_tree['Sequences']['subalbums'].keys()),
)

with self.subTest('References to Album objects'):
self.assertEqual(gallery.albums['Poles'], gallery.full_tree['Poles']['self'])

def test_path_to_root(self) -> None:
gallery = self.get_demo_gallery()

Expand Down Expand Up @@ -69,10 +49,41 @@ def test_path_from_root(self) -> None:
full_menu.path_from_root(album)
self.assertEqual(album.path, album.path_from_root)

def test_title_from_metadata(self) -> None:
gallery = self.get_demo_gallery()

with self.subTest('Use directory name as title', album='Poles'):
album = gallery.albums['Poles']
self.assertEqual('Poles', album.title)

with self.subTest('Custom title', album='Nine levels deep'):
album = gallery.albums['Nine levels deep']
self.assertEqual('Inferno', album.title)

def test_sorted_using_meta(self) -> None:
gallery = self.get_demo_gallery()

with self.subTest('Sorted by name'):
self.assertEqual(
[gallery.albums['Sequences/Tree'], gallery.albums['Sequences/Candle']],
gallery.albums['Sequences'].albums,
)

with self.subTest('Custom order via metadata'):
self.assertEqual(
[
gallery.albums['Long menu'],
gallery.albums['Nine levels deep'],
gallery.albums['Poles'],
gallery.albums['Sequences'],
gallery.albums['Time-Lapse'],
],
gallery.albums['.'].albums,
)

@mock.patch('theme_153957.full_menu.signals')
def test_register(self, mock_signals: mock.MagicMock) -> None:
full_menu.register({})

mock_signals.gallery_initialized.connect.assert_called_once_with(full_menu.full_tree)
mock_signals.album_initialized.connect.assert_any_call(full_menu.path_to_root)
mock_signals.album_initialized.connect.assert_any_call(full_menu.path_from_root)
31 changes: 2 additions & 29 deletions theme_153957/full_menu.py
@@ -1,37 +1,11 @@
"""Add full menu to gallery
"""Add full menu to gallery"""

Limitations:
- Currently only supports sorting albums by name in normal order (can not be reversed).
"""

import operator
import os

from typing import Any

from sigal import signals
from sigal.gallery import Album, Gallery


def full_tree(gallery: Gallery) -> None:
"""full menu tree"""

sorted_tree = sorted(gallery.albums.items(), key=operator.itemgetter(0))

gallery.full_tree = {}

for name, album in sorted_tree:
if name == '.':
continue
ancestors = album.path.split('/')[:-1]
current_ancestor = gallery.full_tree
for ancestor in ancestors:
current_ancestor = current_ancestor[ancestor]['subalbums']
current_ancestor[album.name] = {
'self': album,
'subalbums': {},
}
from sigal.gallery import Album


def path_to_root(album: Album) -> None:
Expand All @@ -53,6 +27,5 @@ def path_from_root(album: Album) -> None:


def register(settings: dict[str, Any]) -> None:
signals.gallery_initialized.connect(full_tree)
signals.album_initialized.connect(path_to_root)
signals.album_initialized.connect(path_from_root)
7 changes: 4 additions & 3 deletions theme_153957/templates/base.html
@@ -1,4 +1,5 @@
{% set source_dir = album.gallery.settings.source.split('/')[-1] %}
{% set top_album = album.gallery.albums['.'] %}

<!doctype html>
<html lang="en">
Expand All @@ -15,15 +16,15 @@
</head>
<body>
{{ settings.body_prefix|default('')|safe }}
<div id="main"{% if not album.gallery.full_tree %} class="no_menu"{% endif %}>
{% if album.gallery.full_tree %}
<div id="main"{% if not top_album.albums %} class="no_menu"{% endif %}>
{% if top_album.albums %}
<div id="menu">
{% if album.gallery.title %}
<p class="title">
<a href="{{ album.path_to_root }}">{{ album.gallery.title }}</a>
</p>
{% endif %}
{% with albums=album.gallery.full_tree, depth=0 %}
{% with menu_album=top_album, depth=0 %}
{% include './menu.html' %}
{% endwith %}
</div>
Expand Down
22 changes: 13 additions & 9 deletions theme_153957/templates/menu.html
@@ -1,21 +1,25 @@
{% if albums %}
{% if menu_album.albums %}
<ul
{% if album.breadcrumb|length == 0 %}
class="current_siblings"
class="current_siblings"
{% else %}
{% for album_dict in albums.values() %}
{% if album_dict.self == album %}
{% for subalbum in menu_album.albums %}
{% if subalbum == album %}
class="current_siblings"
{% endif %}
{% endfor %}
{% endif %}
>
{% for album_dict in albums.values() %}
{% with current_ancestor=(album.breadcrumb|length and depth < album.breadcrumb|length and album_dict.self.title == album.breadcrumb[depth][1]) %}
<li class="album_title{% if album_dict.self == album %} current{% endif %}{% if current_ancestor %} current_ancestor{% endif %}">
<a href="{{ album.path_to_root|urlencode }}{{ album_dict.self.path_from_root|urlencode }}">{{ album_dict.self.title }}</a>
{% for subalbum in menu_album.albums %}
{% with current_ancestor=(
album.breadcrumb|length
and depth < album.breadcrumb|length
and subalbum.title == album.breadcrumb[depth][1]
) %}
<li class="album_title{% if subalbum == album %} current{% endif %}{% if current_ancestor %} current_ancestor{% endif %}">
<a href="{{ album.path_to_root|urlencode }}{{ subalbum.path_from_root|urlencode }}">{{ subalbum.title }}</a>
{% if current_ancestor %}
{% with albums=album_dict.subalbums, depth=depth+1 %}
{% with menu_album=subalbum, depth=depth+1 %}
{% include './menu.html' %}
{% endwith %}
{% endif %}
Expand Down

0 comments on commit 75be088

Please sign in to comment.