From d9652ef109d900cba2bbba8d701b9f4343c81065 Mon Sep 17 00:00:00 2001 From: Scott Colby Date: Wed, 27 Nov 2024 14:51:03 -0500 Subject: [PATCH] fix: `SUMMARY_MAX_PARAGRAPHS` not respected in some combinations with `SUMMARY_MAX_LENGTH` (#3427) --- RELEASE.md | 4 ++++ pelican/contents.py | 2 +- pelican/tests/test_contents.py | 30 ++++++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 000000000..61dcffa29 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,4 @@ +Release type: patch + +- Change ``IGNORE_FILES`` setting default to ignore all hidden files +- 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 bf7b3c3fb..2abdf492e 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -451,7 +451,7 @@ def get_summary(self, siteurl: str) -> str: 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 4defafa46..97653e33e 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -13,8 +13,12 @@ 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 @@ def test_summary_paragraph(self): 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 @@ def test_summary_paragraph_max_length(self): 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,