gallery.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 = sorted(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. gallery[a].sort()
  29. page.gallery=gallery
  30. def register():
  31. signals.article_generator_finalized.connect(add_gallery_post)
  32. signals.page_generator_finalized.connect(generate_gallery_page)