MCPcopy
hub / github.com/django/django / chars

Method chars

django/utils/text.py:193–212  ·  view source on GitHub ↗

Return the text truncated to be no longer than the specified number of characters. `truncate` specifies what should be used to notify that the string has been truncated, defaulting to a translatable string of an ellipsis.

(self, num, truncate=None, html=False)

Source from the content-addressed store, hash-verified

191 super().__init__(lambda: str(text))
192
193 def chars(self, num, truncate=None, html=False):
194 """
195 Return the text truncated to be no longer than the specified number
196 of characters.
197
198 `truncate` specifies what should be used to notify that the string has
199 been truncated, defaulting to a translatable string of an ellipsis.
200 """
201 self._setup()
202 length = int(num)
203 if length <= 0:
204 return ""
205 text = unicodedata.normalize("NFC", self._wrapped)
206
207 if html:
208 parser = TruncateCharsHTMLParser(length=length, replacement=truncate)
209 parser.feed(text)
210 parser.close()
211 return "".join(parser.output)
212 return self._text_chars(length, truncate, text)
213
214 def _text_chars(self, length, truncate, text):
215 """Truncate a string after a certain number of chars."""

Calls 7

_text_charsMethod · 0.95
_setupMethod · 0.45
normalizeMethod · 0.45
feedMethod · 0.45
closeMethod · 0.45
joinMethod · 0.45