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)
| 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.""" |