From f0b468d9c22eea43e36cd14960c23cb2c30cb335 Mon Sep 17 00:00:00 2001 From: Numerlor <25886452+Numerlor@users.noreply.github.com> Date: Sat, 23 Jan 2021 06:08:01 +0100 Subject: Avoid errors when the first element is longer than the truncation index --- bot/exts/info/doc/_parsing.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bot/exts/info/doc/_parsing.py b/bot/exts/info/doc/_parsing.py index 45a81a4cb..0251b0105 100644 --- a/bot/exts/info/doc/_parsing.py +++ b/bot/exts/info/doc/_parsing.py @@ -195,9 +195,16 @@ def _get_truncated_description( return result # Determine the actual truncation index. - # Truncate at the last Markdown element that comes before the truncation index. - markdown_truncate_index = max(cut for cut in markdown_element_ends if cut < truncate_index) - return result[:markdown_truncate_index].strip(_TRUNCATE_STRIP_CHARACTERS) + "..." + possible_truncation_indices = [cut for cut in markdown_element_ends if cut < truncate_index] + if not possible_truncation_indices: + # In case there is no Markdown element ending before the truncation index, use shorten as a fallback. + truncated_result = textwrap.shorten(result, truncate_index) + else: + # Truncate at the last Markdown element that comes before the truncation index. + markdown_truncate_index = max(possible_truncation_indices) + truncated_result = result[:markdown_truncate_index] + + return truncated_result.strip(_TRUNCATE_STRIP_CHARACTERS) + "..." def _create_markdown(signatures: Optional[List[str]], description: Iterable[Tag], url: str) -> str: -- cgit v1.2.3