summary.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from pelican.generators import ArticlesGenerator, StaticGenerator, PagesGenerator
  10. def initialized(pelican):
  11. from pelican.settings import DEFAULT_CONFIG
  12. DEFAULT_CONFIG.setdefault('SUMMARY_BEGIN_MARKER',
  13. '<!-- PELICAN_BEGIN_SUMMARY -->')
  14. DEFAULT_CONFIG.setdefault('SUMMARY_END_MARKER',
  15. '<!-- PELICAN_END_SUMMARY -->')
  16. DEFAULT_CONFIG.setdefault('SUMMARY_USE_FIRST_PARAGRAPH', False)
  17. if pelican:
  18. pelican.settings.setdefault('SUMMARY_BEGIN_MARKER',
  19. '<!-- PELICAN_BEGIN_SUMMARY -->')
  20. pelican.settings.setdefault('SUMMARY_END_MARKER',
  21. '<!-- PELICAN_END_SUMMARY -->')
  22. pelican.settings.setdefault('SUMMARY_USE_FIRST_PARAGRAPH', False)
  23. def extract_summary(instance):
  24. # if summary is already specified, use it
  25. # if there is no content, there's nothing to do
  26. if hasattr(instance, '_summary'):
  27. instance.has_summary = True
  28. return
  29. if not instance._content:
  30. instance.has_summary = False
  31. return
  32. begin_marker = instance.settings['SUMMARY_BEGIN_MARKER']
  33. end_marker = instance.settings['SUMMARY_END_MARKER']
  34. use_first_paragraph = instance.settings['SUMMARY_USE_FIRST_PARAGRAPH']
  35. remove_markers = True
  36. content = instance._content
  37. begin_summary = -1
  38. end_summary = -1
  39. if begin_marker:
  40. begin_summary = content.find(begin_marker)
  41. if end_marker:
  42. end_summary = content.find(end_marker)
  43. if begin_summary == -1 and end_summary == -1 and use_first_paragraph:
  44. begin_marker, end_marker = '<p>', '</p>'
  45. remove_markers = False
  46. begin_summary = content.find(begin_marker)
  47. end_summary = content.find(end_marker)
  48. if begin_summary == -1 and end_summary == -1:
  49. instance.has_summary = False
  50. return
  51. # skip over the begin marker, if present
  52. if begin_summary == -1:
  53. begin_summary = 0
  54. else:
  55. begin_summary = begin_summary + len(begin_marker)
  56. if end_summary == -1:
  57. end_summary = None
  58. summary = content[begin_summary:end_summary]
  59. if remove_markers:
  60. # remove the markers from the content
  61. if begin_summary:
  62. content = content.replace(begin_marker, '', 1)
  63. if end_summary:
  64. content = content.replace(end_marker, '', 1)
  65. instance._content = content
  66. instance._summary = summary
  67. instance.has_summary = True
  68. def run_plugin(generators):
  69. for generator in generators:
  70. if isinstance(generator, ArticlesGenerator):
  71. for article in generator.articles:
  72. extract_summary(article)
  73. elif isinstance(generator, PagesGenerator):
  74. for page in generator.pages:
  75. extract_summary(page)
  76. def register():
  77. signals.initialized.connect(initialized)
  78. try:
  79. signals.all_generators_finalized.connect(run_plugin)
  80. except AttributeError:
  81. # NOTE: This results in #314 so shouldn't really be relied on
  82. # https://github.com/getpelican/pelican-plugins/issues/314
  83. signals.content_object_init.connect(extract_summary)