summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Smith <smithdc@gmail.com>2023-06-23 06:40:54 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-07-14 10:17:14 +0200
commit6f1b8c00d8151059cc1902a92f067bbdc1673779 (patch)
tree1642e537dc69ad482a9cf2a8946d0178e48e7744
parent88a2de3c3906f5acc14bb68e98c128fc13f2f1ae (diff)
Refs #30686 -- Moved add_truncation_text() helper to a module level.
-rw-r--r--django/utils/text.py39
1 files changed, 20 insertions, 19 deletions
diff --git a/django/utils/text.py b/django/utils/text.py
index 86d3b52741..082673a0cc 100644
--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -64,6 +64,22 @@ def wrap(text, width):
return "".join(_generator())
+def add_truncation_text(text, truncate=None):
+ if truncate is None:
+ truncate = pgettext(
+ "String to return when truncating text", "%(truncated_text)s…"
+ )
+ if "%(truncated_text)s" in truncate:
+ return truncate % {"truncated_text": text}
+ # The truncation text didn't contain the %(truncated_text)s string
+ # replacement argument so just append it to the text.
+ if text.endswith(truncate):
+ # But don't append the truncation text if the current text already ends
+ # in this.
+ return text
+ return f"{text}{truncate}"
+
+
class Truncator(SimpleLazyObject):
"""
An object used to truncate text, either by characters or words.
@@ -72,21 +88,6 @@ class Truncator(SimpleLazyObject):
def __init__(self, text):
super().__init__(lambda: str(text))
- def add_truncation_text(self, text, truncate=None):
- if truncate is None:
- truncate = pgettext(
- "String to return when truncating text", "%(truncated_text)s…"
- )
- if "%(truncated_text)s" in truncate:
- return truncate % {"truncated_text": text}
- # The truncation text didn't contain the %(truncated_text)s string
- # replacement argument so just append it to the text.
- if text.endswith(truncate):
- # But don't append the truncation text if the current text already
- # ends in this.
- return text
- return "%s%s" % (text, truncate)
-
def chars(self, num, truncate=None, html=False):
"""
Return the text truncated to be no longer than the specified number
@@ -101,7 +102,7 @@ class Truncator(SimpleLazyObject):
# Calculate the length to truncate to (max length - end_text length)
truncate_len = length
- for char in self.add_truncation_text("", truncate):
+ for char in add_truncation_text("", truncate):
if not unicodedata.combining(char):
truncate_len -= 1
if truncate_len == 0:
@@ -124,7 +125,7 @@ class Truncator(SimpleLazyObject):
end_index = i
if s_len > length:
# 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 text
@@ -150,7 +151,7 @@ class Truncator(SimpleLazyObject):
words = self._wrapped.split()
if len(words) > length:
words = words[:length]
- return self.add_truncation_text(" ".join(words), truncate)
+ return add_truncation_text(" ".join(words), truncate)
return " ".join(words)
def _truncate_html(self, length, truncate, text, truncate_len, words):
@@ -223,7 +224,7 @@ class Truncator(SimpleLazyObject):
if current_len <= length:
return text
out = text[:end_text_pos]
- truncate_text = self.add_truncation_text("", truncate)
+ truncate_text = add_truncation_text("", truncate)
if truncate_text:
out += truncate_text
# Close any tags still open