Skip to content

Commit

Permalink
reformated trim_markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-Robusta committed Jul 17, 2024
1 parent 2c231d0 commit 2caf575
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
28 changes: 22 additions & 6 deletions src/robusta/core/sinks/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def tabulate(*args, **kwargs):
ScanReportBlock,
TableBlock,
)
from robusta.utils.trim_markdown import trim_markdown


class Transformer:
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 0 additions & 16 deletions src/robusta/utils/trim_markdown.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test_trim_markdown.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from robusta.utils.trim_markdown import trim_markdown
from robusta.core.sinks.transformer import Transformer


@pytest.mark.parametrize(
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2caf575

Please sign in to comment.