Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion elementary/messages/block_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
MentionBlock,
TextBlock,
TextStyle,
WhitespaceBlock,
)

SimpleInlineBlock = Union[str, Icon]
Expand All @@ -38,12 +39,23 @@ def BulletListBlock(
*,
icon: Union[Icon, str],
lines: List[LineBlock] = [],
indent: int = 0,
) -> LinesBlock:
whitespaces = [WhitespaceBlock()] * indent
icon_inline: InlineBlock = (
IconBlock(icon=icon) if isinstance(icon, Icon) else TextBlock(text=icon)
)
lines = [
LineBlock(inlines=[icon_inline, *line.inlines], sep=line.sep) for line in lines
LineBlock(
inlines=[
*whitespaces,
icon_inline,
TextBlock(text=" "),
line,
],
sep="",
)
for line in lines
]
return LinesBlock(lines=lines)

Expand Down
6 changes: 6 additions & 0 deletions elementary/messages/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Icon(Enum):
EYE = "eye"
GEAR = "gear"
BELL = "bell"
GEM = "gem"


class TextStyle(Enum):
Expand Down Expand Up @@ -67,12 +68,17 @@ class LineBlock(BaseBlock):
sep: str = " "


class WhitespaceBlock(BaseBlock):
type: Literal["whitespace"] = "whitespace"


InlineBlock = Union[
TextBlock,
LinkBlock,
IconBlock,
InlineCodeBlock,
MentionBlock,
WhitespaceBlock,
"LineBlock",
]

Expand Down
3 changes: 3 additions & 0 deletions elementary/messages/formats/adaptive_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TableBlock,
TextBlock,
TextStyle,
WhitespaceBlock,
)
from elementary.messages.formats.html import ICON_TO_HTML
from elementary.messages.message_body import Color, MessageBlock, MessageBody
Expand Down Expand Up @@ -55,6 +56,8 @@ def format_inline_block(block: InlineBlock) -> str:
return block.user
elif isinstance(block, LineBlock):
return format_line_block_text(block)
elif isinstance(block, WhitespaceBlock):
return " "
else:
raise ValueError(f"Unsupported inline block type: {type(block)}")

Expand Down
3 changes: 3 additions & 0 deletions elementary/messages/formats/block_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
TableBlock,
TextBlock,
TextStyle,
WhitespaceBlock,
)
from elementary.messages.formats.html import ICON_TO_HTML
from elementary.messages.message_body import Color, MessageBlock, MessageBody
Expand Down Expand Up @@ -83,6 +84,8 @@ def _format_inline_block(self, block: InlineBlock) -> str:
return block.user
elif isinstance(block, LineBlock):
return self._format_line_block_text(block)
elif isinstance(block, WhitespaceBlock):
return " "
else:
raise ValueError(f"Unsupported inline block type: {type(block)}")

Expand Down
1 change: 1 addition & 0 deletions elementary/messages/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Icon.EYE: "👁️",
Icon.GEAR: "⚙️",
Icon.BELL: "🔔",
Icon.GEM: "💎",
}

for icon in Icon:
Expand Down
1 change: 1 addition & 0 deletions elementary/messages/messaging_integrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ If your platform's message format is not yet supported:
- ExpandableBlock: Collapsible sections
- MentionBlock: Mention a user
- TableBlock: Table of data
- WhitespaceBlock: Whitespace for indentation
```
3. Add tests in `tests/unit/messages/formats/`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"items": [
{
"type": "TextBlock",
"text": "RED_TRIANGLE \ud83d\udd3a X \u274c WARNING \u26a0\ufe0f EXCLAMATION \u2757 CHECK \u2705 MAGNIFYING_GLASS \ud83d\udd0e HAMMER_AND_WRENCH \ud83d\udee0\ufe0f POLICE_LIGHT \ud83d\udea8 INFO \u2139\ufe0f EYE \ud83d\udc41\ufe0f GEAR \u2699\ufe0f BELL \ud83d\udd14",
"text": "RED_TRIANGLE \ud83d\udd3a X \u274c WARNING \u26a0\ufe0f EXCLAMATION \u2757 CHECK \u2705 MAGNIFYING_GLASS \ud83d\udd0e HAMMER_AND_WRENCH \ud83d\udee0\ufe0f POLICE_LIGHT \ud83d\udea8 INFO \u2139\ufe0f EYE \ud83d\udc41\ufe0f GEAR \u2699\ufe0f BELL \ud83d\udd14 GEM \ud83d\udc8e",
"wrap": true
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": "AdaptiveCard",
"body": [
{
"type": "Container",
"separator": true,
"items": [
{
"type": "TextBlock",
"text": "This should not be indented",
"wrap": true
},
{
"type": "TextBlock",
"text": "  This should be indented",
"wrap": true
}
]
}
],
"version": "1.5"
}
23 changes: 23 additions & 0 deletions tests/unit/messages/formats/base_test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
TableBlock,
TextBlock,
TextStyle,
WhitespaceBlock,
)
from elementary.messages.message_body import Color, MessageBody

Expand Down Expand Up @@ -378,3 +379,25 @@ def test_format_table_block(self, text_length: int, column_count: int):
)
result = self.format(message_body)
self.assert_expected_value(result, expected_file_path)

def test_format_message_body_whitespace_block(self):
message_body = MessageBody(
blocks=[
LinesBlock(
lines=[
LineBlock(
inlines=[TextBlock(text="This should not be indented")]
),
LineBlock(
inlines=[
WhitespaceBlock(),
TextBlock(text="This should be indented"),
]
),
]
)
]
)
expected_file_path = self.get_expected_file_path("whitespace_block")
result = self.format(message_body)
self.assert_expected_value(result, expected_file_path)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "section",
"text": {
"type": "mrkdwn",
"text": "RED_TRIANGLE \ud83d\udd3a X \u274c WARNING \u26a0\ufe0f EXCLAMATION \u2757 CHECK \u2705 MAGNIFYING_GLASS \ud83d\udd0e HAMMER_AND_WRENCH \ud83d\udee0\ufe0f POLICE_LIGHT \ud83d\udea8 INFO \u2139\ufe0f EYE \ud83d\udc41\ufe0f GEAR \u2699\ufe0f BELL \ud83d\udd14"
"text": "RED_TRIANGLE \ud83d\udd3a X \u274c WARNING \u26a0\ufe0f EXCLAMATION \u2757 CHECK \u2705 MAGNIFYING_GLASS \ud83d\udd0e HAMMER_AND_WRENCH \ud83d\udee0\ufe0f POLICE_LIGHT \ud83d\udea8 INFO \u2139\ufe0f EYE \ud83d\udc41\ufe0f GEAR \u2699\ufe0f BELL \ud83d\udd14 GEM \ud83d\udc8e"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This should not be indented\n This should be indented"
}
}
],
"attachments": [
{
"blocks": []
}
]
}
Loading