diff options
author | 2021-03-16 02:35:20 +0100 | |
---|---|---|
committer | 2021-03-16 02:47:58 +0100 | |
commit | ba91d5a530aa9958b7549cc03fecfb95112d52ca (patch) | |
tree | 8cc17c3c9c696f306c484f14b6c0ce5f5dfa5937 | |
parent | Update the symbol_name when fetching the DocItem from get_symbol_item (diff) |
Replace shorten with custom algo to find good cutoff points
shorten collapses the whitespace, causing issues with codeblocks
-rw-r--r-- | bot/exts/info/doc/_parsing.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/bot/exts/info/doc/_parsing.py b/bot/exts/info/doc/_parsing.py index b06aebd45..b3402f655 100644 --- a/bot/exts/info/doc/_parsing.py +++ b/bot/exts/info/doc/_parsing.py @@ -188,8 +188,21 @@ def _get_truncated_description( # Determine the actual truncation index. 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, placeholder="") + # In case there is no Markdown element ending before the truncation index, try to find a good cutoff point. + force_truncated = result[:truncate_index] + # If there is an incomplete codeblock, cut it out. + if force_truncated.count("```") % 2: + force_truncated = force_truncated[:force_truncated.rfind("```")] + # Search for substrings to truncate at, with decreasing desirability. + for string_ in ("\n\n", "\n", ". ", ", ", ",", " "): + cutoff = force_truncated.rfind(string_) + + if cutoff != -1: + truncated_result = force_truncated[:cutoff] + break + else: + truncated_result = force_truncated + else: # Truncate at the last Markdown element that comes before the truncation index. markdown_truncate_index = possible_truncation_indices[-1] |