Skip to content

Commit 6f1b8c0

Browse files
smithdc1felixxm
authored andcommitted
Refs #30686 -- Moved add_truncation_text() helper to a module level.
1 parent 88a2de3 commit 6f1b8c0

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

django/utils/text.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ def _generator():
6464
return "".join(_generator())
6565

6666

67+
def add_truncation_text(text, truncate=None):
68+
if truncate is None:
69+
truncate = pgettext(
70+
"String to return when truncating text", "%(truncated_text)s…"
71+
)
72+
if "%(truncated_text)s" in truncate:
73+
return truncate % {"truncated_text": text}
74+
# The truncation text didn't contain the %(truncated_text)s string
75+
# replacement argument so just append it to the text.
76+
if text.endswith(truncate):
77+
# But don't append the truncation text if the current text already ends
78+
# in this.
79+
return text
80+
return f"{text}{truncate}"
81+
82+
6783
class Truncator(SimpleLazyObject):
6884
"""
6985
An object used to truncate text, either by characters or words.
@@ -72,21 +88,6 @@ class Truncator(SimpleLazyObject):
7288
def __init__(self, text):
7389
super().__init__(lambda: str(text))
7490

75-
def add_truncation_text(self, text, truncate=None):
76-
if truncate is None:
77-
truncate = pgettext(
78-
"String to return when truncating text", "%(truncated_text)s…"
79-
)
80-
if "%(truncated_text)s" in truncate:
81-
return truncate % {"truncated_text": text}
82-
# The truncation text didn't contain the %(truncated_text)s string
83-
# replacement argument so just append it to the text.
84-
if text.endswith(truncate):
85-
# But don't append the truncation text if the current text already
86-
# ends in this.
87-
return text
88-
return "%s%s" % (text, truncate)
89-
9091
def chars(self, num, truncate=None, html=False):
9192
"""
9293
Return the text truncated to be no longer than the specified number
@@ -101,7 +102,7 @@ def chars(self, num, truncate=None, html=False):
101102

102103
# Calculate the length to truncate to (max length - end_text length)
103104
truncate_len = length
104-
for char in self.add_truncation_text("", truncate):
105+
for char in add_truncation_text("", truncate):
105106
if not unicodedata.combining(char):
106107
truncate_len -= 1
107108
if truncate_len == 0:
@@ -124,7 +125,7 @@ def _text_chars(self, length, truncate, text, truncate_len):
124125
end_index = i
125126
if s_len > length:
126127
# Return the truncated string
127-
return self.add_truncation_text(text[: end_index or 0], truncate)
128+
return add_truncation_text(text[: end_index or 0], truncate)
128129

129130
# Return the original string since no truncation was necessary
130131
return text
@@ -150,7 +151,7 @@ def _text_words(self, length, truncate):
150151
words = self._wrapped.split()
151152
if len(words) > length:
152153
words = words[:length]
153-
return self.add_truncation_text(" ".join(words), truncate)
154+
return add_truncation_text(" ".join(words), truncate)
154155
return " ".join(words)
155156

156157
def _truncate_html(self, length, truncate, text, truncate_len, words):
@@ -223,7 +224,7 @@ def _truncate_html(self, length, truncate, text, truncate_len, words):
223224
if current_len <= length:
224225
return text
225226
out = text[:end_text_pos]
226-
truncate_text = self.add_truncation_text("", truncate)
227+
truncate_text = add_truncation_text("", truncate)
227228
if truncate_text:
228229
out += truncate_text
229230
# Close any tags still open

0 commit comments

Comments
 (0)