summary.py 3.4 KB

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