From f90dad098b591a4c2e55ede3af6526c007de6229 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Fri, 5 Jan 2024 17:31:35 +0100 Subject: Make URL format configurable --- slack/config.py | 7 +++++++ slack/slack_message.py | 13 ++++++------- tests/conftest.py | 12 ++++++++++++ tests/test_render_blocks.py | 6 ++++-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/slack/config.py b/slack/config.py index 7defa99..93c3060 100644 --- a/slack/config.py +++ b/slack/config.py @@ -210,6 +210,13 @@ class SlackConfigSectionLook: string_values=["emoji", "name", "both"], ) + self.render_url_as = WeeChatOption( + self._section, + "render_url_as", + "format to render URLs (note: content is evaluated, see /help eval; ${url} is replaced by the URL link and ${text} is replaced by the URL text); the default format renders only the URL if the text is empty or is contained in the URL, otherwise it renders the text first and then the URL in parentheses", + "${if: ${text} == || ${url} =- ${text} ?${url}:${text} (${url})}", + ) + self.replace_space_in_nicks_with = WeeChatOption( self._section, "replace_space_in_nicks_with", diff --git a/slack/slack_message.py b/slack/slack_message.py index ddc91fc..8300770 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -76,13 +76,12 @@ def format_date(timestamp: int, token_string: str, link: Optional[str] = None) - def format_url(url: str, text: Optional[str]) -> str: - if text is not None: - if url.endswith(text): - return text - else: - return f"{url} ({text})" - else: - return url + return weechat.string_eval_expression( + shared.config.look.render_url_as.value, + {}, + {"url": url, "text": text or ""}, + {}, + ) def convert_int_to_letter(num: int) -> str: diff --git a/tests/conftest.py b/tests/conftest.py index 891dc67..981faab 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -116,6 +116,17 @@ def info_get(info_name: str, arguments: str): return "" +def string_eval_expression( + expr: str, + pointers: Dict[str, str], + extra_vars: Dict[str, str], + options: Dict[str, str], +) -> str: + for key, value in extra_vars.items(): + expr = expr.replace(f"${{{key}}}", value) + return expr + + weechat.config_new_option = config_new_option weechat.config_option_set = config_option_set weechat.config_boolean = config_boolean @@ -124,6 +135,7 @@ weechat.config_string = config_string weechat.config_color = config_color weechat.color = color weechat.info_get = info_get +weechat.string_eval_expression = string_eval_expression shared.weechat_version = 0x03080000 shared.weechat_callbacks = {} diff --git a/tests/test_render_blocks.py b/tests/test_render_blocks.py index 6b1fc0f..63e4df5 100644 --- a/tests/test_render_blocks.py +++ b/tests/test_render_blocks.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, List import pytest +from slack.shared import shared from slack.slack_message import SlackMessage from tests.conftest import ( color_reset, @@ -128,7 +129,7 @@ cases: List[Case] = [ "normal <[color:bold]>*bold*<[color:-bold]> <[color:italic]>_italic_" "<[color:-italic]> ~strikethrough~ <[color:bold]><[color:italic]>" "*_~bold-italic-strikethrough~_*<[color:-italic]><[color:-bold]> " - "https://vg.no (link)", + "link (https://vg.no)", "1. number", "2. list", "> some quote", @@ -447,7 +448,8 @@ cases: List[Case] = [ @pytest.mark.parametrize("case", cases) -def test_unfurl_refs(case: Case, message1_in_channel_public: SlackMessage): +def test_render_blocks(case: Case, message1_in_channel_public: SlackMessage): + shared.config.look.render_url_as.value = "${text} (${url})" parsed = message1_in_channel_public._render_blocks( # pyright: ignore [reportPrivateUsage] case["blocks"] ) -- cgit