representative_image.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  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. # Process Summary:
  8. # If summary contains images, extract one to be the representativeImage and remove images from summary
  9. soup = BeautifulSoup(instance.summary, 'html.parser')
  10. images = soup.find_all('img')
  11. for i in images:
  12. if not representativeImage:
  13. representativeImage = i['src']
  14. i.extract()
  15. if len(images) > 0:
  16. # set _summary field which is based on metadata. summary field is only based on article's content and not settable
  17. instance._summary = unicode(soup)
  18. # If there are no image in summary, look for it in the content body
  19. if not representativeImage:
  20. soup = BeautifulSoup(instance.content, 'html.parser')
  21. imageTag = soup.find('img')
  22. if imageTag:
  23. representativeImage = imageTag['src']
  24. # Set the attribute to content instance
  25. instance.repImage = representativeImage
  26. def register():
  27. signals.content_object_init.connect(images_extraction)