@@ -64,6 +64,22 @@ def _generator():
64
64
return "" .join (_generator ())
65
65
66
66
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
+
67
83
class Truncator (SimpleLazyObject ):
68
84
"""
69
85
An object used to truncate text, either by characters or words.
@@ -72,21 +88,6 @@ class Truncator(SimpleLazyObject):
72
88
def __init__ (self , text ):
73
89
super ().__init__ (lambda : str (text ))
74
90
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
-
90
91
def chars (self , num , truncate = None , html = False ):
91
92
"""
92
93
Return the text truncated to be no longer than the specified number
@@ -101,7 +102,7 @@ def chars(self, num, truncate=None, html=False):
101
102
102
103
# Calculate the length to truncate to (max length - end_text length)
103
104
truncate_len = length
104
- for char in self . add_truncation_text ("" , truncate ):
105
+ for char in add_truncation_text ("" , truncate ):
105
106
if not unicodedata .combining (char ):
106
107
truncate_len -= 1
107
108
if truncate_len == 0 :
@@ -124,7 +125,7 @@ def _text_chars(self, length, truncate, text, truncate_len):
124
125
end_index = i
125
126
if s_len > length :
126
127
# 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 )
128
129
129
130
# Return the original string since no truncation was necessary
130
131
return text
@@ -150,7 +151,7 @@ def _text_words(self, length, truncate):
150
151
words = self ._wrapped .split ()
151
152
if len (words ) > length :
152
153
words = words [:length ]
153
- return self . add_truncation_text (" " .join (words ), truncate )
154
+ return add_truncation_text (" " .join (words ), truncate )
154
155
return " " .join (words )
155
156
156
157
def _truncate_html (self , length , truncate , text , truncate_len , words ):
@@ -223,7 +224,7 @@ def _truncate_html(self, length, truncate, text, truncate_len, words):
223
224
if current_len <= length :
224
225
return text
225
226
out = text [:end_text_pos ]
226
- truncate_text = self . add_truncation_text ("" , truncate )
227
+ truncate_text = add_truncation_text ("" , truncate )
227
228
if truncate_text :
228
229
out += truncate_text
229
230
# Close any tags still open
0 commit comments