diff --git a/AUTHORS b/AUTHORS index a7c1413..b15084f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -32,5 +32,6 @@ alphabetical order): - @trapperhoney - Thomas Misilo - Tobias Preuss +- Toke Høiland-Jørgensen (@tohojo) - Vikram Shirgur - Yuce Tekol diff --git a/sigal/gallery.py b/sigal/gallery.py index 9b65e25..08066a8 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -314,7 +314,12 @@ class Album(UnicodeMixin): if self.subdirs: if albums_sort_attr: root_path = self.path if self.path != '.' else '' - key = lambda s: strxfrm(getattr( + if albums_sort_attr.startswith("meta."): + meta_key = albums_sort_attr.split(".", 1)[1] + key = lambda s: strxfrm( + self.gallery.albums[join(root_path, s)].meta.get(meta_key, [''])[0]) + else: + key = lambda s: strxfrm(getattr( self.gallery.albums[join(root_path, s)], albums_sort_attr)) else: key = strxfrm @@ -328,6 +333,9 @@ class Album(UnicodeMixin): if self.medias: if medias_sort_attr == 'date': key = lambda s: s.date or datetime.now() + elif medias_sort_attr.startswith('meta.'): + meta_key = medias_sort_attr.split(".", 1)[1] + key = lambda s: strxfrm(s.meta.get(meta_key, [''])[0]) else: key = lambda s: strxfrm(getattr(s, medias_sort_attr)) diff --git a/sigal/templates/sigal.conf.py b/sigal/templates/sigal.conf.py index 8dca8be..a733ecc 100644 --- a/sigal/templates/sigal.conf.py +++ b/sigal/templates/sigal.conf.py @@ -101,14 +101,16 @@ thumb_size = (280, 210) # Use symbolic links instead of copying the original images # orig_link = False -# Attribute of Album objects which is used to sort medias (eg 'title'). +# 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 # albums_sort_reverse = False # Attribute of Media objects which is used to sort medias. 'date' can be used -# to sort with EXIF dates. +# to sort with EXIF dates, and 'meta.key' to sort on a metadata key (which then +# must exist for all images). # medias_sort_attr = 'filename' # Reverse sort for medias diff --git a/sigal/utils.py b/sigal/utils.py index c7721ec..e86faf5 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -90,7 +90,10 @@ def read_markdown(filename): pass else: output['meta'] = meta - output['title'] = md.Meta.get('title', [''])[0] + try: + output['title'] = md.Meta['title'][0] + except KeyError: + pass return output diff --git a/tests/sample/pictures/dir1/test1/index.md b/tests/sample/pictures/dir1/test1/index.md index 7c1de62..d80649b 100644 --- a/tests/sample/pictures/dir1/test1/index.md +++ b/tests/sample/pictures/dir1/test1/index.md @@ -1,2 +1,3 @@ Title: An example sub-category Thumbnail: 11.jpg +Order: 03 diff --git a/tests/sample/pictures/dir1/test2/21.md b/tests/sample/pictures/dir1/test2/21.md new file mode 100644 index 0000000..576d6c7 --- /dev/null +++ b/tests/sample/pictures/dir1/test2/21.md @@ -0,0 +1 @@ +Order: 01 diff --git a/tests/sample/pictures/dir1/test2/22.md b/tests/sample/pictures/dir1/test2/22.md new file mode 100644 index 0000000..0d4b373 --- /dev/null +++ b/tests/sample/pictures/dir1/test2/22.md @@ -0,0 +1 @@ +Order: 02 diff --git a/tests/sample/pictures/dir1/test2/index.md b/tests/sample/pictures/dir1/test2/index.md new file mode 100644 index 0000000..576d6c7 --- /dev/null +++ b/tests/sample/pictures/dir1/test2/index.md @@ -0,0 +1 @@ +Order: 01 diff --git a/tests/sample/pictures/dir1/test3/index.md b/tests/sample/pictures/dir1/test3/index.md index 848e37c..df41e5b 100644 --- a/tests/sample/pictures/dir1/test3/index.md +++ b/tests/sample/pictures/dir1/test3/index.md @@ -1 +1,2 @@ Title: 01 First title alphabetically +Order: 02 diff --git a/tests/test_gallery.py b/tests/test_gallery.py index c3096e0..bc2c171 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -191,6 +191,18 @@ def test_albums_sort(settings): a.sort_subdirs('title') assert [im.title for im in a.albums] == list(reversed(titles)) + orders = ['01', '02', '03'] + orders.sort() + settings['albums_sort_reverse'] = False + a = Album('dir1', settings, album['subdirs'], album['medias'], gal) + a.sort_subdirs('meta.order') + assert [d.meta['order'][0] for d in a.albums] == orders + + settings['albums_sort_reverse'] = True + a = Album('dir1', settings, album['subdirs'], album['medias'], gal) + a.sort_subdirs('meta.order') + assert [d.meta['order'][0] for d in a.albums] == list(reversed(orders)) + def test_medias_sort(settings): gal = Gallery(settings, ncpu=1) @@ -208,6 +220,12 @@ def test_medias_sort(settings): assert [im.filename for im in a.images] == ['22.jpg', '21.jpg', 'archlinux-kiss-1024x640.png'] + settings['medias_sort_attr'] = 'meta.order' + settings['medias_sort_reverse'] = False + a = Album('dir1/test2', settings, album['subdirs'], album['medias'], gal) + a.sort_medias(settings['medias_sort_attr']) + assert [im.filename for im in a.images] == ['archlinux-kiss-1024x640.png', '21.jpg', '22.jpg'] + def test_gallery(settings, tmpdir): "Test the Gallery class." diff --git a/tests/test_utils.py b/tests/test_utils.py index b87e7cc..83e3f04 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -58,7 +58,7 @@ def test_read_markdown_empty_file(tmpdir): src = tmpdir.join("file.txt") src.write("content") m = utils.read_markdown(str(src)) - assert m['title'] == '' + assert 'title' not in m assert m['meta'] == {} assert m['description'] == '
content
'