From 4c36dafaf69f9205dd452bae91c1248fc43424c1 Mon Sep 17 00:00:00 2001 From: Scott Colby Date: Sat, 23 Nov 2024 11:32:00 +0100 Subject: [PATCH] Fix SUMMARY_MAX_PARAGRAPHS with SUMMARY_MAX_LENGTH When, for example, the content is 2 paragraphs, each consisting of 30 words, and SUMMARY_MAX_PARAGRAPHS=1 and SUMMARY_MAX_LENGTH=50, the current behavior produces a summary with 50 words and 2 paragraphs, effectively ignoring SUMMARY_MAX_PARAGRAPHS. This change inverts this behavior: the resulting summary from the above situation will consist of the entire first 30 word paragraph. In the case where the first paragraph(s) is longer than SUMMARY_MAX_LENGTH, the summary will still be truncated to that length. This change brings the actual behavior in line with the documented behavior. --- RELEASE.md | 3 +++ pelican/contents.py | 2 +- pelican/tests/test_contents.py | 30 ++++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 00000000..b73b338e --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,3 @@ +Release type: patch + +Fix SUMMARY_MAX_PARAGRAPHS not being respected in some combinations with SUMMARY_MAX_LENGTH. diff --git a/pelican/contents.py b/pelican/contents.py index 0769f875..a7a8e015 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -450,7 +450,7 @@ class Content: return content return truncate_html_words( - self.content, + content, self.settings["SUMMARY_MAX_LENGTH"], self.settings["SUMMARY_END_SUFFIX"], ) diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index 06d1a690..cefadd9f 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -13,8 +13,12 @@ from pelican.settings import DEFAULT_CONFIG from pelican.tests.support import LoggedTestCase, get_context, get_settings, unittest from pelican.utils import path_to_url, posixize_path, truncate_html_words -# generate one paragraph, enclosed with

-TEST_CONTENT = str(generate_lorem_ipsum(n=1)) +# generate 3 test paragraphs, each enclosed with

+# save the first paragraph separately for testing the summary generation algorithm +# NOTE: these values are nondeterministic between test runs +TEST_CONTENT_FIRST_PARAGRAPH = str(generate_lorem_ipsum(n=1)) +TEST_CONTENT_REMAINING_PARAGRAPHS = str(generate_lorem_ipsum(n=2)) +TEST_CONTENT = TEST_CONTENT_FIRST_PARAGRAPH + TEST_CONTENT_REMAINING_PARAGRAPHS TEST_SUMMARY = generate_lorem_ipsum(n=1, html=False) @@ -126,7 +130,7 @@ class TestPage(TestBase): settings["SUMMARY_MAX_PARAGRAPHS"] = 1 settings["SUMMARY_MAX_LENGTH"] = None page = Page(**page_kwargs) - self.assertEqual(page.summary, TEST_CONTENT) + self.assertEqual(page.summary, TEST_CONTENT_FIRST_PARAGRAPH) def test_summary_paragraph_max_length(self): # If both SUMMARY_MAX_PARAGRAPHS and SUMMARY_MAX_LENGTH are set, @@ -139,7 +143,25 @@ class TestPage(TestBase): settings["SUMMARY_MAX_PARAGRAPHS"] = 1 settings["SUMMARY_MAX_LENGTH"] = 10 page = Page(**page_kwargs) - self.assertEqual(page.summary, truncate_html_words(TEST_CONTENT, 10)) + self.assertEqual( + page.summary, truncate_html_words(TEST_CONTENT_FIRST_PARAGRAPH, 10) + ) + + def test_summary_paragraph_long_max_length(self): + # If both SUMMARY_MAX_PARAGRAPHS and SUMMARY_MAX_LENGTH are set, + # and the first SUMMARY_MAX_PARAGRAPHS paragraphs have fewer words + # than SUMMARY_MAX_LENGTH, the generated summary should still only + # contain the given number of paragraphs. + page_kwargs = self._copy_page_kwargs() + settings = get_settings() + page_kwargs["settings"] = settings + del page_kwargs["metadata"]["summary"] + settings["SUMMARY_MAX_PARAGRAPHS"] = 1 + settings["SUMMARY_MAX_LENGTH"] = 50 + page = Page(**page_kwargs) + self.assertEqual( + page.summary, truncate_html_words(TEST_CONTENT_FIRST_PARAGRAPH, 50) + ) def test_summary_end_suffix(self): # If a :SUMMARY_END_SUFFIX: is set, and there is no other summary,