Skip to content

Commit

Permalink
fix: SUMMARY_MAX_PARAGRAPHS not respected in some combinations with…
Browse files Browse the repository at this point in the history
… `SUMMARY_MAX_LENGTH` (#3427)
  • Loading branch information
scolby33 authored Nov 27, 2024
1 parent 7096b0a commit d9652ef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -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``
2 changes: 1 addition & 1 deletion pelican/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down
30 changes: 26 additions & 4 deletions pelican/tests/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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)


Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d9652ef

Please sign in to comment.