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.
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import os
|
|
import ast
|
|
from pelican import signals
|
|
|
|
|
|
def add_gallery_post(generator):
|
|
|
|
contentpath = generator.settings.get('PATH')
|
|
gallerycontentpath = os.path.join(contentpath,'images/gallery')
|
|
|
|
for article in generator.articles:
|
|
if 'gallery' in article.metadata.keys():
|
|
album = article.metadata.get('gallery')
|
|
galleryimages = []
|
|
gallerycaptions = dict()
|
|
|
|
articlegallerypath=os.path.join(gallerycontentpath, album)
|
|
|
|
if(os.path.isdir(articlegallerypath)):
|
|
for i in os.listdir(articlegallerypath):
|
|
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):
|
|
|
|
contentpath = generator.settings.get('PATH')
|
|
gallerycontentpath = os.path.join(contentpath,'images/gallery')
|
|
|
|
for page in generator.pages:
|
|
if page.metadata.get('template') == 'gallery':
|
|
gallery = dict()
|
|
|
|
for a in os.listdir(gallerycontentpath):
|
|
if os.path.isdir(os.path.join(gallerycontentpath, a)):
|
|
|
|
for i in os.listdir(os.path.join(gallerycontentpath, a)):
|
|
if os.path.isfile(os.path.join(os.path.join(gallerycontentpath, a), i)):
|
|
gallery.setdefault(a, []).append(i)
|
|
gallery[a].sort()
|
|
|
|
page.gallery=gallery
|
|
|
|
|
|
def register():
|
|
signals.article_generator_finalized.connect(add_gallery_post)
|
|
signals.page_generator_finalized.connect(generate_gallery_page)
|