1
0
mirror of https://github.com/django/django.git synced 2025-03-09 17:02:43 +00:00

Refs #30686 -- Moved add_truncation_text() helper to a module level.

This commit is contained in:
David Smith 2023-06-23 06:40:54 +01:00 committed by Mariusz Felisiak
parent 88a2de3c39
commit 6f1b8c00d8

View File

@ -64,15 +64,7 @@ def wrap(text, width):
return "".join(_generator()) return "".join(_generator())
class Truncator(SimpleLazyObject): def add_truncation_text(text, truncate=None):
"""
An object used to truncate text, either by characters or words.
"""
def __init__(self, text):
super().__init__(lambda: str(text))
def add_truncation_text(self, text, truncate=None):
if truncate is None: if truncate is None:
truncate = pgettext( truncate = pgettext(
"String to return when truncating text", "%(truncated_text)s" "String to return when truncating text", "%(truncated_text)s"
@ -82,10 +74,19 @@ class Truncator(SimpleLazyObject):
# The truncation text didn't contain the %(truncated_text)s string # The truncation text didn't contain the %(truncated_text)s string
# replacement argument so just append it to the text. # replacement argument so just append it to the text.
if text.endswith(truncate): if text.endswith(truncate):
# But don't append the truncation text if the current text already # But don't append the truncation text if the current text already ends
# ends in this. # in this.
return text return text
return "%s%s" % (text, truncate) return f"{text}{truncate}"
class Truncator(SimpleLazyObject):
"""
An object used to truncate text, either by characters or words.
"""
def __init__(self, text):
super().__init__(lambda: str(text))
def chars(self, num, truncate=None, html=False): def chars(self, num, truncate=None, html=False):
""" """
@ -101,7 +102,7 @@ class Truncator(SimpleLazyObject):
# Calculate the length to truncate to (max length - end_text length) # Calculate the length to truncate to (max length - end_text length)
truncate_len = length truncate_len = length
for char in self.add_truncation_text("", truncate): for char in add_truncation_text("", truncate):
if not unicodedata.combining(char): if not unicodedata.combining(char):
truncate_len -= 1 truncate_len -= 1
if truncate_len == 0: if truncate_len == 0:
@ -124,7 +125,7 @@ class Truncator(SimpleLazyObject):
end_index = i end_index = i
if s_len > length: if s_len > length:
# Return the truncated string # Return the truncated string
return self.add_truncation_text(text[: end_index or 0], truncate) return add_truncation_text(text[: end_index or 0], truncate)
# Return the original string since no truncation was necessary # Return the original string since no truncation was necessary
return text return text
@ -150,7 +151,7 @@ class Truncator(SimpleLazyObject):
words = self._wrapped.split() words = self._wrapped.split()
if len(words) > length: if len(words) > length:
words = words[:length] words = words[:length]
return self.add_truncation_text(" ".join(words), truncate) return add_truncation_text(" ".join(words), truncate)
return " ".join(words) return " ".join(words)
def _truncate_html(self, length, truncate, text, truncate_len, words): def _truncate_html(self, length, truncate, text, truncate_len, words):
@ -223,7 +224,7 @@ class Truncator(SimpleLazyObject):
if current_len <= length: if current_len <= length:
return text return text
out = text[:end_text_pos] out = text[:end_text_pos]
truncate_text = self.add_truncation_text("", truncate) truncate_text = add_truncation_text("", truncate)
if truncate_text: if truncate_text:
out += truncate_text out += truncate_text
# Close any tags still open # Close any tags still open