gallery.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os
  2. from pelican import signals
  3. def get_content_path(pelican):
  4. return pelican.settings.get('PATH')
  5. def get_gallery_path(pelican):
  6. gallery_path = pelican.settings.get('GALLERY_PATH', 'images/gallery')
  7. content_path = get_content_path(pelican)
  8. return os.path.join(content_path, gallery_path)
  9. def add_gallery_post(generator):
  10. gallerycontentpath = get_gallery_path(generator)
  11. for article in generator.articles:
  12. if 'gallery' in article.metadata.keys():
  13. album = article.metadata.get('gallery')
  14. galleryimages = []
  15. articlegallerypath=os.path.join(gallerycontentpath, album)
  16. if(os.path.isdir(articlegallerypath)):
  17. for i in os.listdir(articlegallerypath):
  18. if not i.startswith('.') and os.path.isfile(os.path.join(os.path.join(gallerycontentpath, album), i)):
  19. galleryimages.append(i)
  20. article.album = album
  21. article.galleryimages = sorted(galleryimages)
  22. def add_gallery_page(generator):
  23. gallerycontentpath = get_gallery_path(generator)
  24. for page in generator.pages:
  25. if 'gallery' in page.metadata.keys():
  26. album = page.metadata.get('gallery')
  27. galleryimages = []
  28. pagegallerypath=os.path.join(gallerycontentpath, album)
  29. if(os.path.isdir(pagegallerypath)):
  30. for i in os.listdir(pagegallerypath):
  31. if not i.startswith('.') and os.path.isfile(os.path.join(os.path.join(gallerycontentpath, album), i)):
  32. galleryimages.append(i)
  33. page.album = album
  34. page.galleryimages = sorted(galleryimages)
  35. def generate_gallery_page(generator):
  36. gallerycontentpath = get_gallery_path(generator)
  37. for page in generator.pages:
  38. if page.metadata.get('template') == 'gallery':
  39. gallery = dict()
  40. for a in os.listdir(gallerycontentpath):
  41. if not a.startswith('.') and os.path.isdir(os.path.join(gallerycontentpath, a)):
  42. for i in os.listdir(os.path.join(gallerycontentpath, a)):
  43. if not a.startswith('.') and os.path.isfile(os.path.join(os.path.join(gallerycontentpath, a), i)):
  44. gallery.setdefault(a, []).append(i)
  45. gallery[a].sort()
  46. page.gallery=gallery
  47. def register():
  48. signals.article_generator_finalized.connect(add_gallery_post)
  49. signals.page_generator_finalized.connect(generate_gallery_page)
  50. signals.page_generator_finalized.connect(add_gallery_page)