gallery.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """
  2. Copyright (c) Emmet McPoland <emcpoland@gmail.com>
  3. Gallery
  4. ==================
  5. * Allows an article to contain an album of pictures.
  6. * All albums can also be syndicated into a central gallery page.
  7. ##How to Use
  8. 1. Group images into folders, with each folder representing an album.
  9. 2. Place all album folders within a folder named gallery, which resides within the images folder.
  10. ./content/images/gallery/album_name
  11. ###Articles
  12. Attach an album to an article/post by placing a gallery stub with the name of the album.
  13. gallery:album_name
  14. The template has access to the album name.
  15. article.album
  16. And the filename of images within an album.
  17. article.albumimages
  18. ###Gallery Page
  19. Create a page and a gallery template (named gallery.html). And inform pelican to use the gallery template for the page.
  20. template:gallery
  21. The template has access to a dictionary of lists.
  22. The dictionary key is the name of the album and the lists contain the filenames.
  23. page.gallery
  24. ##Examples
  25. ###article.html
  26. {% for album, images in page.gallery.iteritems() %}
  27. <h2><a href="{{ SITEURL }}/pages/gallery.html#{{ article.album }}">{{ article.album }}</a></h2>
  28. <ul>
  29. {% for image in article.galleryimages %}
  30. <li><a class="{{ article.album }} cboxElement" href="{{ SITEURL }}/static/images/gallery/{{ article.album }}/{{ image }}"><img src="{{ SITEURL }}/static/images/gallery200x200/{{ article.album }}/{{ image }}"></a></li>
  31. {% endfor %}
  32. </ul>
  33. {% endfor %}
  34. ###gallery.html
  35. {% for album, images in page.gallery.iteritems() %}
  36. <h2><a name="{{ album }}">{{ album }}</a></h2>
  37. <ul>
  38. {% for image in images %}
  39. <li><a class="{{ album }} cboxElement" href="{{ SITEURL }}/static/images/gallery/{{album}}/{{ image }}" title="{{ image }}"><img src="{{ SITEURL }}/static/images/gallery200x200/{{album}}/{{ image }}"></a></li>
  40. {% endfor %}
  41. </ul>
  42. {% endfor %}
  43. ###posts/foo.md
  44. title:Foo
  45. gallery:albumname
  46. ###pages/gallery.md
  47. title:All Images
  48. template:gallery
  49. ##Reasoning
  50. The album name and filenames are returned as opposed to the direct path to the images,
  51. to allow flexibility of different thumbnail sizes to be used it different locations of a website.
  52. href="{{ SITEURL }}/static/images/gallery/{{album}}/{{ image }}"
  53. href="{{ SITEURL }}/static/images/gallery200x200/{{album}}/{{ image }}"
  54. It also allows a thumbnail to link to the full image,
  55. as well as the filename extension to be stripped and the title of an image to be displayed along side the title of an album.
  56. ##Recommendation
  57. It is recommended to use this extension along with the thumbnailer plugin.
  58. RESIZE = [
  59. ('gallery', False, 200,200),
  60. ]
  61. You may also wish to use this along with a gallery plugin such as [Colorbox](http://www.jacklmoore.com/colorbox/).
  62. ##In Use
  63. * [SESIF Article](http://sesif.github.io/my-super-title.html)
  64. * [SESIF Gallery](http://sesif.github.io/pages/gallery.html)
  65. * [SESIF Source](http://github.com/SESIF/SESIF.github.io/tree/source)
  66. """
  67. import os
  68. from pelican import signals
  69. __author__ = "Emmet McPoland"
  70. def add_gallery_post(generator):
  71. contentpath = generator.settings.get('PATH')
  72. gallerycontentpath = os.path.join(contentpath,'images/gallery')
  73. for article in generator.articles:
  74. if 'gallery' in article.metadata.keys():
  75. album = article.metadata.get('gallery')
  76. galleryimages = []
  77. articlegallerypath=os.path.join(gallerycontentpath, album)
  78. if(os.path.isdir(articlegallerypath)):
  79. for i in os.listdir(articlegallerypath):
  80. if os.path.isfile(os.path.join(os.path.join(gallerycontentpath, album), i)):
  81. galleryimages.append(i)
  82. article.album=album
  83. article.galleryimages=galleryimages
  84. def generate_gallery_page(generator):
  85. contentpath = generator.settings.get('PATH')
  86. gallerycontentpath = os.path.join(contentpath,'images/gallery')
  87. for page in generator.pages:
  88. if page.metadata.get('template') == 'gallery':
  89. gallery=dict()
  90. for a in os.listdir(gallerycontentpath):
  91. if os.path.isdir(os.path.join(gallerycontentpath, a)):
  92. for i in os.listdir(os.path.join(gallerycontentpath, a)):
  93. if os.path.isfile(os.path.join(os.path.join(gallerycontentpath, a), i)):
  94. gallery.setdefault(a, []).append(i)
  95. page.gallery=gallery
  96. def register():
  97. signals.article_generator_finalized.connect(add_gallery_post)
  98. signals.pages_generator_finalized.connect(generate_gallery_page)