1
0
Fork 0
forked from github/pelican

Introduce paragraph count summary (#2761)

Co-authored-by: Justin Mayer <entroP@gmail.com>
This commit is contained in:
Agathe 2024-06-25 15:07:41 +02:00 committed by GitHub
commit 513abbfdc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 80 additions and 2 deletions

View file

@ -631,6 +631,25 @@ def truncate_html_words(s: str, num: int, end_text: str = "…") -> str:
return out
def truncate_html_paragraphs(s, count):
"""Truncate HTML to a certain number of paragraphs.
:param count: number of paragraphs to keep
Newlines in the HTML are preserved.
"""
paragraphs = []
tag_stop = 0
substr = s[:]
for _ in range(count):
substr = substr[tag_stop:]
tag_start = substr.find("<p>")
tag_stop = substr.find("</p>") + len("</p>")
paragraphs.append(substr[tag_start:tag_stop])
return "".join(paragraphs)
def process_translations(
content_list: list[Content],
translation_id: str | Collection[str] | None = None,