representative_image.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import six
  2. from pelican import signals
  3. from pelican.contents import Article, Page
  4. from pelican.generators import ArticlesGenerator
  5. from bs4 import BeautifulSoup
  6. def images_extraction(instance):
  7. representativeImage = None
  8. if type(instance) in (Article, Page):
  9. if 'image' in instance.metadata:
  10. representativeImage = instance.metadata['image']
  11. # Process Summary:
  12. # If summary contains images, extract one to be the representativeImage and remove images from summary
  13. soup = BeautifulSoup(instance.summary, 'html.parser')
  14. images = soup.find_all('img')
  15. for i in images:
  16. if not representativeImage:
  17. representativeImage = i['src']
  18. i.extract()
  19. if len(images) > 0:
  20. # set _summary field which is based on metadata. summary field is only based on article's content and not settable
  21. instance._summary = six.text_type(soup)
  22. # If there are no image in summary, look for it in the content body
  23. if not representativeImage:
  24. soup = BeautifulSoup(instance._content, 'html.parser')
  25. imageTag = soup.find('img')
  26. if imageTag:
  27. representativeImage = imageTag['src']
  28. # Set the attribute to content instance
  29. instance.featured_image = representativeImage
  30. def run_plugin(generators):
  31. for generator in generators:
  32. if isinstance(generator, ArticlesGenerator):
  33. for article in generator.articles:
  34. images_extraction(article)
  35. def register():
  36. try:
  37. signals.all_generators_finalized.connect(run_plugin)
  38. except AttributeError:
  39. # NOTE: This results in #314 so shouldn't really be relied on
  40. # https://github.com/getpelican/pelican-plugins/issues/314
  41. signals.content_object_init.connect(images_extraction)