representative_image.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from pelican import signals
  2. from pelican.contents import Content, Article
  3. from bs4 import BeautifulSoup
  4. def images_extraction(instance):
  5. representativeImage = None
  6. if type(instance) == Article:
  7. if 'image' in instance.metadata:
  8. representativeImage = instance.metadata['image']
  9. # Process Summary:
  10. # If summary contains images, extract one to be the representativeImage and remove images from summary
  11. soup = BeautifulSoup(instance.summary, 'html.parser')
  12. images = soup.find_all('img')
  13. for i in images:
  14. if not representativeImage:
  15. representativeImage = i['src']
  16. i.extract()
  17. if len(images) > 0:
  18. # set _summary field which is based on metadata. summary field is only based on article's content and not settable
  19. instance._summary = unicode(soup)
  20. # If there are no image in summary, look for it in the content body
  21. if not representativeImage:
  22. soup = BeautifulSoup(instance.content, 'html.parser')
  23. imageTag = soup.find('img')
  24. if imageTag:
  25. representativeImage = imageTag['src']
  26. # Set the attribute to content instance
  27. instance.featured_image = representativeImage
  28. def register():
  29. signals.content_object_init.connect(images_extraction)