diff options
author | lovetox <philipp@hoerist.com> | 2022-06-21 00:07:38 +0300 |
---|---|---|
committer | lovetox <philipp@hoerist.com> | 2022-06-21 00:08:05 +0300 |
commit | 5462da5a803fdc87932aaaa28065ebbfd6941ba0 (patch) | |
tree | ea6e88ccaf0eab5444e02628e4ad5edb09e7ec8b | |
parent | 78c6711a2d50c91dc4717242e1333a48329bd03a (diff) |
fix: Styling: Process URIs when using /me command
Fixes #10988
-rw-r--r-- | gajim/common/styling.py | 15 | ||||
-rw-r--r-- | gajim/gtk/conversation/plain_widget.py | 38 |
2 files changed, 41 insertions, 12 deletions
diff --git a/gajim/common/styling.py b/gajim/common/styling.py index 440891228..fb7f79352 100644 --- a/gajim/common/styling.py +++ b/gajim/common/styling.py @@ -219,6 +219,21 @@ def process(text: Union[str, bytes], level: int = 0) -> ParsingResult: return ParsingResult(text, blocks) +def process_uris(text: Union[str, bytes]) -> list[BaseUri]: + if isinstance(text, bytes): + text = text.decode() + + uris: list[BaseUri] = [] + offset = 0 + offset_bytes = 0 + for line in text.splitlines(keepends=True): + uris += _parse_uris(line, offset, offset_bytes) + offset += len(line) + offset_bytes += len(line.encode()) + + return uris + + def _parse_blocks(text: str, level: int) -> list[Block]: blocks: list[Block] = [] text_len = len(text) diff --git a/gajim/gtk/conversation/plain_widget.py b/gajim/gtk/conversation/plain_widget.py index cf0793151..4b827de83 100644 --- a/gajim/gtk/conversation/plain_widget.py +++ b/gajim/gtk/conversation/plain_widget.py @@ -31,7 +31,9 @@ from gajim.common.const import StyleAttr from gajim.common.helpers import open_uri from gajim.common.helpers import parse_uri from gajim.common.structs import URI +from gajim.common.styling import BaseUri from gajim.common.styling import PlainBlock +from gajim.common.styling import process_uris from ..menus import get_conv_action_context_menu from ..menus import get_conv_uri_context_menu @@ -70,9 +72,7 @@ class PlainWidget(Gtk.Box): self._text_widget.print_text_with_styling(block) def add_action_phrase(self, text: str, nickname: str) -> None: - text = text.replace('/me', f'* {nickname}', 1) - text = GLib.markup_escape_text(text) - self._text_widget.add_action_phrase(text) + self._text_widget.add_action_phrase(text, nickname) class MessageLabel(Gtk.Label): @@ -106,16 +106,19 @@ class MessageLabel(Gtk.Label): menu.prepend(action_menu_item) menu.show_all() - def print_text_with_styling(self, block: PlainBlock) -> None: - text = '' - after = GLib.markup_escape_text(block.text.strip()) - for uri in block.uris: + def _build_link_markup(self, text: str, uris: list[BaseUri]) -> str: + markup_text = '' + after = GLib.markup_escape_text(text.strip()) + for uri in uris: uri_escaped = GLib.markup_escape_text(uri.text) before, _, after = after.partition(uri_escaped) - text += before - text += uri.get_markup_string() - text += after + markup_text += before + markup_text += uri.get_markup_string() + markup_text += after + return markup_text + def print_text_with_styling(self, block: PlainBlock) -> None: + text = self._build_link_markup(block.text, block.uris) self.set_markup(text) if len(self.get_text()) > 10000: @@ -124,7 +127,10 @@ class MessageLabel(Gtk.Label): self.set_attributes(make_pango_attributes(block)) - def add_action_phrase(self, text: str) -> None: + def add_action_phrase(self, text: str, nickname: str) -> None: + text = text.replace('/me', f'* {nickname}', 1) + uris = process_uris(text) + text = self._build_link_markup(text, uris) self.set_markup(f'<i>{text}</i>') def _on_activate_link(self, _label: Gtk.Label, uri: str) -> int: @@ -274,10 +280,18 @@ class MessageTextview(Gtk.TextView): buffer_.delete_mark(start_mark) buffer_.delete_mark(end_mark) - def add_action_phrase(self, text: str) -> None: + def add_action_phrase(self, text: str, nickname: str) -> None: + text = text.replace('/me', f'* {nickname}', 1) + buffer_ = self.get_buffer() buffer_.insert(buffer_.get_start_iter(), text.strip()) + uris = process_uris(text) + for uri in uris: + start_iter = buffer_.get_iter_at_offset(uri.start) + end_iter = buffer_.get_iter_at_offset(uri.end) + buffer_.apply_tag_by_name(uri.name, start_iter, end_iter) + start_iter = buffer_.get_start_iter() end_iter = buffer_.get_end_iter() buffer_.apply_tag_by_name('emphasis', start_iter, end_iter) |