diff --git a/src/robusta/core/sinks/transformer.py b/src/robusta/core/sinks/transformer.py index 5859e2e50..c56f4da28 100644 --- a/src/robusta/core/sinks/transformer.py +++ b/src/robusta/core/sinks/transformer.py @@ -27,7 +27,6 @@ def tabulate(*args, **kwargs): ScanReportBlock, TableBlock, ) -from robusta.utils.trim_markdown import trim_markdown class Transformer: @@ -58,14 +57,31 @@ def apply_length_limit(msg: str, max_length: int, truncator: Optional[str] = Non truncator = truncator or "..." return msg[: max_length - len(truncator)] + truncator + @staticmethod + def trim_markdown(text: str, max_length: int, suffix: str = "...") -> str: + if len(text) < max_length: + return text + if '```' not in text: + return Transformer.apply_length_limit(text, max_length, suffix) + suffix_len = len(suffix) + code_markdown_len = len('```') + tuncate_index = max_length - suffix_len + + # if there is a code annotation near the end of the string + if '```' in text[tuncate_index - code_markdown_len*2:tuncate_index]: + tuncate_index = tuncate_index - code_markdown_len*2 + + code_annotation_truncat_count = text.count('```', __start=tuncate_index) + needs_end_markdown_string = (code_annotation_truncat_count % 2 == 1) # if there is an odd number of markdowns on the right + if needs_end_markdown_string: + return (text[:tuncate_index - code_markdown_len - suffix_len] + '```' + suffix)[:tuncate_index] + else: + return text[:tuncate_index - suffix_len] + suffix + @staticmethod def apply_length_limit_to_markdown(msg: str, max_length: int, truncator: str = "...") -> str: try: - if len(msg) < max_length: - return msg - if '```' not in msg: - return Transformer.apply_length_limit(msg, max_length, truncator) - return trim_markdown(msg, max_length, truncator) + return Transformer.trim_markdown(msg, max_length, truncator) except: return Transformer.apply_length_limit(msg, max_length, truncator) diff --git a/src/robusta/utils/trim_markdown.py b/src/robusta/utils/trim_markdown.py deleted file mode 100644 index bdb6132c4..000000000 --- a/src/robusta/utils/trim_markdown.py +++ /dev/null @@ -1,16 +0,0 @@ - -def trim_markdown(text: str, max_length: int, suffix: str = "...") -> str: - suffix_len = len(suffix) - code_markdown_len = len('```') - tuncate_index = max_length - suffix_len - - # if there is a code annotation near the end of the string - if '```' in text[tuncate_index - code_markdown_len*2:tuncate_index]: - tuncate_index = tuncate_index - code_markdown_len*2 - - code_annotation_truncat_count = text.count('```', __start=tuncate_index) - needs_end_markdown_string = (code_annotation_truncat_count % 2 == 1) # if there is an odd number of markdowns on the right - if needs_end_markdown_string: - return (text[:tuncate_index - code_markdown_len - suffix_len] + '```' + suffix)[:tuncate_index] - else: - return text[:tuncate_index - suffix_len] + suffix diff --git a/tests/test_trim_markdown.py b/tests/test_trim_markdown.py index c708e9c32..c7be88927 100644 --- a/tests/test_trim_markdown.py +++ b/tests/test_trim_markdown.py @@ -1,6 +1,6 @@ import pytest -from robusta.utils.trim_markdown import trim_markdown +from robusta.core.sinks.transformer import Transformer @pytest.mark.parametrize( @@ -42,7 +42,7 @@ ]) def test_trim_markdown(max_length: int, expected_output: str): text = "```oh``` hello ```world``` and then ```something```" - trimmed = trim_markdown(text, max_length, "##") + trimmed = Transformer.trim_markdown(text, max_length, "##") assert trimmed == expected_output assert len(trimmed) <= max_length