author_images.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. Author images plugin for Pelican
  3. ===========================
  4. This plugin assigns the ``author.avatar`` and ``author.image`` variables to the
  5. avatar and image of the author in question. Authors are identified by email
  6. address, and avatars are images are stored in directories configured by
  7. AUTHOR_AVATARS and AUTHOR_IMAGES.
  8. """
  9. from pelican import signals
  10. from hashlib import sha256
  11. from os.path import exists
  12. EXTENSIONS = ['jpg', 'png', 'svg']
  13. def add_author_image(author, generator):
  14. hashsum = sha256(author.name).hexdigest()
  15. static = generator.settings['THEME'] + '/static/'
  16. if 'AUTHOR_AVATARS' in generator.settings.keys():
  17. avatar = generator.settings['AUTHOR_AVATARS'] + '/' + hashsum
  18. for ext in EXTENSIONS:
  19. if exists('%s%s.%s' % (static, avatar, ext)):
  20. author.avatar = '%s/%s.%s' % \
  21. (generator.settings['THEME_STATIC_DIR'], avatar, ext)
  22. break
  23. if 'AUTHOR_IMAGES' in generator.settings.keys():
  24. image = generator.settings['AUTHOR_IMAGES'] + '/' + hashsum
  25. for ext in EXTENSIONS:
  26. if exists('%s%s.%s' % (static, image, ext)):
  27. author.image = '%s/%s.%s' % \
  28. (generator.settings['THEME_STATIC_DIR'], image, ext)
  29. break
  30. def add_author_images(generator):
  31. for article in generator.articles:
  32. for author in article.authors:
  33. add_author_image(author, generator)
  34. for author, _ in generator.authors:
  35. add_author_image(author, generator)
  36. def register():
  37. signals.article_generator_finalized.connect(add_author_images)