gallery.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. from pelican import signals
  3. def add_gallery_post(generator):
  4. contentpath = generator.settings.get('PATH')
  5. gallerycontentpath = os.path.join(contentpath,'images/gallery')
  6. for article in generator.articles:
  7. if 'gallery' in article.metadata.keys():
  8. album = article.metadata.get('gallery')
  9. galleryimages = []
  10. articlegallerypath=os.path.join(gallerycontentpath, album)
  11. if(os.path.isdir(articlegallerypath)):
  12. for i in os.listdir(articlegallerypath):
  13. if os.path.isfile(os.path.join(os.path.join(gallerycontentpath, album), i)):
  14. galleryimages.append(i)
  15. article.album=album
  16. article.galleryimages=galleryimages
  17. def generate_gallery_page(generator):
  18. contentpath = generator.settings.get('PATH')
  19. gallerycontentpath = os.path.join(contentpath,'images/gallery')
  20. for page in generator.pages:
  21. if page.metadata.get('template') == 'gallery':
  22. gallery=dict()
  23. for a in os.listdir(gallerycontentpath):
  24. if os.path.isdir(os.path.join(gallerycontentpath, a)):
  25. for i in os.listdir(os.path.join(gallerycontentpath, a)):
  26. if os.path.isfile(os.path.join(os.path.join(gallerycontentpath, a), i)):
  27. gallery.setdefault(a, []).append(i)
  28. page.gallery=gallery
  29. def register():
  30. signals.article_generator_finalized.connect(add_gallery_post)
  31. signals.page_generator_finalized.connect(generate_gallery_page)