summary.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """
  2. Summary
  3. -------
  4. This plugin allows easy, variable length summaries directly embedded into the
  5. body of your articles.
  6. """
  7. from __future__ import unicode_literals
  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 there is no content, there's nothing to do
  23. if hasattr(instance, '_summary'):
  24. instance.has_summary = True
  25. return
  26. if not instance._content:
  27. instance.has_summary = False
  28. return
  29. begin_marker = instance.settings['SUMMARY_BEGIN_MARKER']
  30. end_marker = instance.settings['SUMMARY_END_MARKER']
  31. # extract out our summary
  32. content = instance._content
  33. begin_summary = -1
  34. end_summary = -1
  35. if begin_marker:
  36. begin_summary = content.find(begin_marker)
  37. if end_marker:
  38. end_summary = content.find(end_marker)
  39. if begin_summary == -1 and end_summary == -1:
  40. instance.has_summary = False
  41. return
  42. # skip over the begin marker, if present
  43. if begin_summary == -1:
  44. begin_summary = 0
  45. else:
  46. begin_summary = begin_summary + len(begin_marker)
  47. if end_summary == -1:
  48. end_summary = None
  49. summary = content[begin_summary:end_summary]
  50. # remove the markers from the content
  51. if begin_summary:
  52. content = content.replace(begin_marker, '', 1)
  53. if end_summary:
  54. content = content.replace(end_marker, '', 1)
  55. instance._content = content
  56. instance._summary = summary
  57. instance.has_summary = True
  58. def register():
  59. signals.initialized.connect(initialized)
  60. signals.content_object_init.connect(content_object_init)