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: <Gallery Name>
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.
This commit is contained in:
2014-05-29 15:18:22 +02:00
parent 7b3851a62b
commit a8830b8825
2 changed files with 26 additions and 2 deletions

View File

@@ -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.
<h2><a href="{{ SITEURL }}/pages/gallery.html#{{ article.album }}">{{ article.album }}</a></h2>
<ul>
{% for image in article.galleryimages %}
{% if article.gallerycaptions.get(image) %}
<li><a class="{{ article.album }} cboxElement" href="{{ SITEURL }}/static/images/gallery/{{ article.album }}/{{ image }}" title="{{ article.gallerycaptions.get(image) }}">{{ article.gallerycaptions.get(image) }}<br><img src="{{ SITEURL }}/static/images/gallery200x200/{{ article.album }}/{{ image }}"></a></li>
{% else %}
<li><a class="{{ article.album }} cboxElement" href="{{ SITEURL }}/static/images/gallery/{{ article.album }}/{{ image }}"><img src="{{ SITEURL }}/static/images/gallery200x200/{{ article.album }}/{{ image }}"></a></li>
{% endif %}
{% endfor %}
</ul>
@@ -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

View File

@@ -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):