Переглянути джерело

Add SUMMARY_USE_FIRST_PARAGRAPH

Makes it possible to only use the first paragraph of a post as its
summary if no marker is found.
Thomas Jost 8 роки тому
батько
коміт
2ae76fd64f
3 змінених файлів з 31 додано та 5 видалено
  1. 3 0
      summary/Readme.rst
  2. 16 5
      summary/summary.py
  3. 12 0
      summary/test_summary.py

+ 3 - 0
summary/Readme.rst

@@ -26,6 +26,9 @@ beginning marker was found, it starts at the top of the body. It is possible
 to leave out the end marker instead, in which case the summary will start at the
 beginning marker and continue to the end of the body.
 
+If no beginning or end marker is found, and if ``SUMMARY_USE_FIRST_PARAGRAPH``
+is enabled in the settings, the summary will be the first paragraph of the post.
+
 The plugin also sets a ``has_summary`` attribute on every article. It is True
 for articles with an explicitly-defined summary, and False otherwise.  (It is
 also False for an article truncated by ``SUMMARY_MAX_LENGTH``.)  Your templates

+ 16 - 5
summary/summary.py

@@ -16,11 +16,13 @@ def initialized(pelican):
                               '<!-- PELICAN_BEGIN_SUMMARY -->')
     DEFAULT_CONFIG.setdefault('SUMMARY_END_MARKER',
                               '<!-- PELICAN_END_SUMMARY -->')
+    DEFAULT_CONFIG.setdefault('SUMMARY_USE_FIRST_PARAGRAPH', False)
     if pelican:
         pelican.settings.setdefault('SUMMARY_BEGIN_MARKER',
                                     '<!-- PELICAN_BEGIN_SUMMARY -->')
         pelican.settings.setdefault('SUMMARY_END_MARKER',
                                     '<!-- PELICAN_END_SUMMARY -->')
+        pelican.settings.setdefault('SUMMARY_USE_FIRST_PARAGRAPH', False)
 
 def extract_summary(instance):
     # if summary is already specified, use it
@@ -35,6 +37,8 @@ def extract_summary(instance):
 
     begin_marker = instance.settings['SUMMARY_BEGIN_MARKER']
     end_marker   = instance.settings['SUMMARY_END_MARKER']
+    use_first_paragraph = instance.settings['SUMMARY_USE_FIRST_PARAGRAPH']
+    remove_markers = True
 
     content = instance._content
     begin_summary = -1
@@ -44,6 +48,12 @@ def extract_summary(instance):
     if end_marker:
         end_summary = content.find(end_marker)
 
+    if begin_summary == -1 and end_summary == -1 and use_first_paragraph:
+        begin_marker, end_marker = '<p>', '</p>'
+        remove_markers = False
+        begin_summary = content.find(begin_marker)
+        end_summary = content.find(end_marker)
+
     if begin_summary == -1 and end_summary == -1:
         instance.has_summary = False
         return
@@ -59,11 +69,12 @@ def extract_summary(instance):
 
     summary = content[begin_summary:end_summary]
 
-    # remove the markers from the content
-    if begin_summary:
-        content = content.replace(begin_marker, '', 1)
-    if end_summary:
-        content = content.replace(end_marker, '', 1)
+    if remove_markers:
+        # remove the markers from the content
+        if begin_summary:
+            content = content.replace(begin_marker, '', 1)
+        if end_summary:
+            content = content.replace(end_marker, '', 1)
 
     instance._content = content
     instance._summary = summary

+ 12 - 0
summary/test_summary.py

@@ -18,6 +18,7 @@ class TestSummary(unittest.TestCase):
     def setUp(self):
         super(TestSummary, self).setUp()
         pelican.settings.DEFAULT_CONFIG['SUMMARY_MAX_LENGTH'] = None
+        pelican.settings.DEFAULT_CONFIG['SUMMARY_USE_FIRST_PARAGRAPH'] = False
 
         summary.register()
         summary.initialized(None)
@@ -79,6 +80,17 @@ class TestSummary(unittest.TestCase):
         self.assertEqual(page.summary, TEST_SUMMARY)
         self.assertEqual(page.content, 'FOOBAR' + TEST_SUMMARY + TEST_CONTENT)
 
+    def test_use_first_paragraph(self):
+        page_kwargs = self._copy_page_kwargs()
+        del page_kwargs['metadata']['summary']
+        pelican.settings.DEFAULT_CONFIG['SUMMARY_USE_FIRST_PARAGRAPH'] = True
+        page_kwargs['content'] = '<p>' + TEST_SUMMARY + '</p>' + TEST_CONTENT
+        page = Page(**page_kwargs)
+        summary.extract_summary(page)
+        # test both the summary and the marker removal
+        self.assertEqual(page.summary, TEST_SUMMARY)
+        self.assertEqual(page.content, '<p>' + TEST_SUMMARY + '</p>' + TEST_CONTENT)
+
 
 if __name__ == '__main__':
     unittest.main()