clean_summary.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """
  2. Clean Summary
  3. -------------
  4. adds option to specify maximum number of images to appear in article summary
  5. also adds option to include an image by default if one exists in your article
  6. """
  7. from pelican import signals
  8. from pelican.contents import Content, Article
  9. from pelican.generators import ArticlesGenerator
  10. from bs4 import BeautifulSoup
  11. from six import text_type
  12. def init(pelican):
  13. global maximum_images
  14. global minimum_one
  15. maximum_images = pelican.settings.get('CLEAN_SUMMARY_MAXIMUM', 0)
  16. minimum_one = pelican.settings.get('CLEAN_SUMMARY_MINIMUM_ONE', False)
  17. def clean_summary(instance):
  18. if type(instance) == Article:
  19. summary = instance.summary
  20. summary = BeautifulSoup(instance.summary, 'html.parser')
  21. images = summary.findAll('img')
  22. if (len(images) > maximum_images):
  23. for image in images[maximum_images:]:
  24. image.extract()
  25. if len(images) < 1 and minimum_one: #try to find one
  26. content = BeautifulSoup(instance.content, 'html.parser')
  27. first_image = content.find('img')
  28. if first_image:
  29. summary.insert(0, first_image)
  30. instance._summary = text_type(summary)
  31. def run_plugin(generators):
  32. for generator in generators:
  33. if isinstance(generator, ArticlesGenerator):
  34. for article in generator.articles:
  35. clean_summary(article)
  36. def register():
  37. signals.initialized.connect(init)
  38. try:
  39. signals.all_generators_finalized.connect(run_plugin)
  40. except AttributeError:
  41. # NOTE: This may result in #314 so shouldn't really be relied on
  42. # https://github.com/getpelican/pelican-plugins/issues/314
  43. signals.content_object_init.connect(clean_summary)