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.
This commit is contained in:
Scott Colby 2024-11-23 11:32:00 +01:00
commit 4c36dafaf6
3 changed files with 30 additions and 5 deletions

3
RELEASE.md Normal file
View file

@ -0,0 +1,3 @@
Release type: patch
Fix SUMMARY_MAX_PARAGRAPHS not being respected in some combinations with SUMMARY_MAX_LENGTH.

View file

@ -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"],
)

View file

@ -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 <p>
TEST_CONTENT = str(generate_lorem_ipsum(n=1))
# generate 3 test paragraphs, each enclosed with <p>
# 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,