From a8830b8825fda62d9d247e82fc4c5071df9a4153 Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Thu, 29 May 2014 15:18:22 +0200 Subject: [PATCH] gallery: Add per-image caption/title support. To add caption/title to a picture, in your article simply add a 'gallerycaptions' tag in your metadata, whose value is a dictionnary of (filename, title) tuples. Example: gallery: gallerycaptions: { 'image_name1': 'caption1', 'image_name4': 'caption4', } Then you will be able to use article.gallerycaptions.get('image_nameX') in the templates, to retrieve the title associated to the image. --- gallery/README.md | 22 ++++++++++++++++++++-- gallery/gallery.py | 6 ++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/gallery/README.md b/gallery/README.md index 09ac0fd..ecb4e83 100644 --- a/gallery/README.md +++ b/gallery/README.md @@ -16,7 +16,11 @@ Gallery Attach an album to an article/post by placing a gallery metadata tag with the name of the album. gallery:album_name - + +Optionaly you can also add (on one(1) line!) + + gallerycaptions:{'file1':'title1', 'file4':'title4'} + The template has access to the album name. article.album @@ -25,6 +29,10 @@ And the filename of images within an album. article.albumimages +And the titles associated to the pictures, as a dictionary. + + article.gallerycaptions + ###Gallery Page Create a page and a gallery template (named gallery.html). And inform pelican to use the gallery template for the page. @@ -43,7 +51,11 @@ The dictionary key is the name of the album and the lists contain the filenames.

{{ article.album }}

@@ -62,7 +74,13 @@ The dictionary key is the name of the album and the lists contain the filenames. title:Foo gallery:albumname - + +or, to add captions: + + title:Foo + gallery:albumname + gallerycaptions:{'file1':'title1', 'file4':'title4'} + ###pages/gallery.md title:All Images diff --git a/gallery/gallery.py b/gallery/gallery.py index 3bde1c5..d72c338 100644 --- a/gallery/gallery.py +++ b/gallery/gallery.py @@ -1,4 +1,5 @@ import os +import ast from pelican import signals @@ -11,6 +12,7 @@ def add_gallery_post(generator): if 'gallery' in article.metadata.keys(): album = article.metadata.get('gallery') galleryimages = [] + gallerycaptions = dict() articlegallerypath=os.path.join(gallerycontentpath, album) @@ -19,8 +21,12 @@ def add_gallery_post(generator): if os.path.isfile(os.path.join(os.path.join(gallerycontentpath, album), i)): galleryimages.append(i) + if 'gallerycaptions' in article.metadata.keys(): + gallerycaptions = ast.literal_eval(article.metadata.get('gallerycaptions')) + article.album = album article.galleryimages = sorted(galleryimages) + article.gallerycaptions = gallerycaptions def generate_gallery_page(generator):