libravatar.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """Libravatar plugin for Pelican"""
  2. ## Copyright (C) 2015 Rafael Laboissiere <rafael@laboissiere.net>
  3. ##
  4. ## This program is free software: you can redistribute it and/or modify it
  5. ## under the terms of the GNU General Affero Public License as published by
  6. ## the Free Software Foundation, either version 3 of the License, or (at
  7. ## your option) any later version.
  8. ##
  9. ## This program is distributed in the hope that it will be useful, but
  10. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ## Affero General Public License for more details.
  13. ##
  14. ## You should have received a copy of the GNU Affero General Public License
  15. ## along with this program. If not, see http://www.gnu.org/licenses/.
  16. import hashlib
  17. from pelican import signals
  18. def initialize (pelicanobj):
  19. """Initialize the Libravatar plugin"""
  20. pelicanobj.settings.setdefault ('LIBRAVATAR_MISSING', None)
  21. pelicanobj.settings.setdefault ('LIBRAVATAR_SIZE', None)
  22. def add_libravatar (generator, metadata):
  23. """Article generator connector for the Libravatar plugin"""
  24. missing = generator.settings.get ('LIBRAVATAR_MISSING')
  25. size = str (generator.settings.get ('LIBRAVATAR_SIZE'))
  26. ## Check the presence of the Email header
  27. if 'email' not in metadata.keys ():
  28. try:
  29. metadata ['email'] = generator.settings.get ('AUTHOR_EMAIL')
  30. except:
  31. pass
  32. ## Add the Libravatar URL
  33. if metadata ['email']:
  34. ## Compose URL using the MD5 hash
  35. md5 = hashlib.md5 (metadata ['email'].lower ()).hexdigest ()
  36. url = 'http://cdn.libravatar.org/avatar/' + md5
  37. ## Add eventual "missing picture" option
  38. if missing or size:
  39. url = url + '?'
  40. if missing:
  41. url = url + 'd=' + missing
  42. if size:
  43. url = url + '&'
  44. if size:
  45. url = url + 's=' + size
  46. ## Add URL to the article's metadata
  47. metadata ['author_libravatar'] = url
  48. def register ():
  49. """Register the Libravatar plugin with Pelican"""
  50. signals.initialized.connect (initialize)
  51. signals.article_generator_context.connect (add_libravatar)