gallery.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 not i.startswith('.') and 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 add_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 'gallery' in page.metadata.keys():
  22. album = page.metadata.get('gallery')
  23. galleryimages = []
  24. pagegallerypath=os.path.join(gallerycontentpath, album)
  25. if(os.path.isdir(pagegallerypath)):
  26. for i in os.listdir(pagegallerypath):
  27. if not i.startswith('.') and os.path.isfile(os.path.join(os.path.join(gallerycontentpath, album), i)):
  28. galleryimages.append(i)
  29. page.album = album
  30. page.galleryimages = sorted(galleryimages)
  31. def generate_gallery_page(generator):
  32. contentpath = generator.settings.get('PATH')
  33. gallerycontentpath = os.path.join(contentpath,'images/gallery')
  34. for page in generator.pages:
  35. if page.metadata.get('template') == 'gallery':
  36. gallery = dict()
  37. for a in os.listdir(gallerycontentpath):
  38. if not a.startswith('.') and os.path.isdir(os.path.join(gallerycontentpath, a)):
  39. for i in os.listdir(os.path.join(gallerycontentpath, a)):
  40. if not a.startswith('.') and os.path.isfile(os.path.join(os.path.join(gallerycontentpath, a), i)):
  41. gallery.setdefault(a, []).append(i)
  42. gallery[a].sort()
  43. page.gallery=gallery
  44. def register():
  45. signals.article_generator_finalized.connect(add_gallery_post)
  46. signals.page_generator_finalized.connect(generate_gallery_page)
  47. signals.page_generator_finalized.connect(add_gallery_page)