feed_summary.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. """
  3. Feed Summary
  4. ============
  5. This plugin allows summaries to be used in feeds instead of the full length article.
  6. """
  7. from __future__ import unicode_literals
  8. from jinja2 import Markup
  9. import six
  10. if not six.PY3:
  11. from urlparse import urlparse
  12. else:
  13. from urllib.parse import urlparse
  14. from pelican import signals
  15. from pelican.writers import Writer
  16. from pelican.utils import set_date_tzinfo
  17. from .magic_set import magic_set
  18. class FeedSummaryWriter(Writer):
  19. def _add_item_to_the_feed(self, feed, item):
  20. if self.settings['FEED_USE_SUMMARY']:
  21. title = Markup(item.title).striptags()
  22. link = '%s/%s' % (self.site_url, item.url)
  23. feed.add_item(
  24. title=title,
  25. link=link,
  26. unique_id='tag:%s,%s:%s' % (urlparse(link).netloc,
  27. item.date.date(),
  28. urlparse(link).path.lstrip('/')),
  29. description=item.summary if hasattr(item, 'summary') else item.get_content(self.site_url),
  30. categories=item.tags if hasattr(item, 'tags') else None,
  31. author_name=getattr(item, 'author', ''),
  32. pubdate=set_date_tzinfo(item.modified if hasattr(item, 'modified') else item.date,
  33. self.settings.get('TIMEZONE', None)))
  34. else:
  35. super(FeedSummaryWriter, self)._add_item_to_the_feed(feed, item)
  36. def set_feed_use_summary_default(pelican_object):
  37. # modifying DEFAULT_CONFIG doesn't have any effect at this point in pelican setup
  38. # everybody who uses DEFAULT_CONFIG is already used/copied it or uses the pelican_object.settings copy.
  39. pelican_object.settings.setdefault('FEED_USE_SUMMARY', False)
  40. def patch_pelican_writer(pelican_object):
  41. @magic_set(pelican_object)
  42. def get_writer(self):
  43. return FeedSummaryWriter(self.output_path,settings=self.settings)
  44. def register():
  45. signals.initialized.connect(set_feed_use_summary_default)
  46. signals.initialized.connect(patch_pelican_writer)