representative_image.py 1.8 KB

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