summary.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. Summary
  3. -------
  4. This plugin allows easy, variable length summaries directly embedded into the
  5. body of your articles.
  6. """
  7. import types
  8. from pelican import signals
  9. def initialized(pelican):
  10. from pelican.settings import _DEFAULT_CONFIG
  11. _DEFAULT_CONFIG.setdefault('SUMMARY_BEGIN_MARKER',
  12. '<!-- PELICAN_BEGIN_SUMMARY -->')
  13. _DEFAULT_CONFIG.setdefault('SUMMARY_END_MARKER',
  14. '<!-- PELICAN_END_SUMMARY -->')
  15. if pelican:
  16. pelican.settings.setdefault('SUMMARY_BEGIN_MARKER',
  17. '<!-- PELICAN_BEGIN_SUMMARY -->')
  18. pelican.settings.setdefault('SUMMARY_END_MARKER',
  19. '<!-- PELICAN_END_SUMMARY -->')
  20. def content_object_init(instance):
  21. # if summary is already specified, use it
  22. if 'summary' in instance.metadata:
  23. return
  24. def _get_content(self):
  25. content = self._content
  26. if self.settings['SUMMARY_BEGIN_MARKER']:
  27. content = content.replace(
  28. self.settings['SUMMARY_BEGIN_MARKER'], '', 1)
  29. if self.settings['SUMMARY_END_MARKER']:
  30. content = content.replace(
  31. self.settings['SUMMARY_END_MARKER'], '', 1)
  32. return content
  33. instance._get_content = types.MethodType(_get_content, instance)
  34. # extract out our summary
  35. if not hasattr(instance, '_summary') and instance._content is not None:
  36. content = instance._content
  37. begin_summary = -1
  38. end_summary = -1
  39. if instance.settings['SUMMARY_BEGIN_MARKER']:
  40. begin_summary = content.find(instance.settings['SUMMARY_BEGIN_MARKER'])
  41. if instance.settings['SUMMARY_END_MARKER']:
  42. end_summary = content.find(instance.settings['SUMMARY_END_MARKER'])
  43. if begin_summary != -1 or end_summary != -1:
  44. # the beginning position has to take into account the length
  45. # of the marker
  46. begin_summary = (begin_summary +
  47. len(instance.settings['SUMMARY_BEGIN_MARKER'])
  48. if begin_summary != -1 else 0)
  49. end_summary = end_summary if end_summary != -1 else None
  50. instance._summary = content[begin_summary:end_summary]
  51. def register():
  52. signals.initialized.connect(initialized)
  53. signals.content_object_init.connect(content_object_init)