From d0f7ab98fb36a449a68d29e949e0457321def1c6 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Sat, 6 Mar 2021 12:44:52 -0300 Subject: [PATCH] Custom sort per album --- docs/album_information.rst | 3 +++ sigal/gallery.py | 36 ++++++++++++++++++++--------------- sigal/templates/sigal.conf.py | 7 ++++--- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/docs/album_information.rst b/docs/album_information.rst index 960528a..4873826 100644 --- a/docs/album_information.rst +++ b/docs/album_information.rst @@ -10,6 +10,7 @@ named ``index.md`` : Title: Another example gallery Thumbnail: test2.jpg Author: John Doe + Sort: -name And a description with *Markdown* syntax. @@ -21,6 +22,8 @@ gallery: represent the gallery. - *Author*: the author of the gallery, if present it is used instead of the author setting. +- *Sort*: the sort order for this album. If prefixed with a '-' it will be in + reversed order. Any additional meta-data is available in the templates. For instance:: diff --git a/sigal/gallery.py b/sigal/gallery.py index 0aa1d6b..f41243d 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -404,26 +404,32 @@ class Album: def sort_subdirs(self, albums_sort_attr): if self.subdirs: - if albums_sort_attr: - root_path = self.path if self.path != '.' else '' - if albums_sort_attr.startswith("meta."): - meta_key = albums_sort_attr.split(".", 1)[1] - - def sort_key(s): - album = self.gallery.albums[join(root_path, s)] - return album.meta.get(meta_key, [''])[0] + albums_sort_attr = self.settings['albums_sort_attr'] + reverse = self.settings['albums_sort_reverse'] + if 'sort' in self.meta: + albums_sort_attr = self.meta['sort'][0] + if albums_sort_attr[0] == '-': + albums_sort_attr = albums_sort_attr[1:] + reverse = True else: - def sort_key(s): - album = self.gallery.albums[join(root_path, s)] - return getattr(album, albums_sort_attr) + reverse = False + + root_path = self.path if self.path != '.' else '' + if albums_sort_attr.startswith("meta."): + meta_key = albums_sort_attr.split(".", 1)[1] + + def sort_key(s): + album = self.gallery.albums[join(root_path, s)] + return album.meta.get(meta_key, [''])[0] - key = natsort_keygen(key=sort_key, alg=ns.LOCALE) else: - key = natsort_keygen(alg=ns.LOCALE) + def sort_key(s): + album = self.gallery.albums[join(root_path, s)] + return getattr(album, albums_sort_attr) - self.subdirs.sort(key=key, - reverse=self.settings['albums_sort_reverse']) + key = natsort_keygen(key=sort_key, alg=ns.LOCALE) + self.subdirs.sort(key=key, reverse=reverse) signals.albums_sorted.send(self) diff --git a/sigal/templates/sigal.conf.py b/sigal/templates/sigal.conf.py index 7518d7a..6230ac3 100644 --- a/sigal/templates/sigal.conf.py +++ b/sigal/templates/sigal.conf.py @@ -124,11 +124,12 @@ thumb_size = (280, 210) # Use symbolic links instead of copying the original images # orig_link = False -# Use symbolic links that are relative to the source directory instead of absolute paths +# Use symbolic links that are relative to the source directory instead of +# absolute paths # rel_link = False -# Attribute of Album objects which is used to sort medias (eg 'title'). To sort -# on a metadata key, use 'meta.key'. +# Attribute of Album objects which is used to sort medias (eg 'title'). To +# sort on a metadata key, use 'meta.key'. # albums_sort_attr = 'name' # Reverse sort for albums