Skip to content

Commit 85d3442

Browse files
committed
boxes/helper: Fixed generation of quote fences.
This commit incorporated some refactoring as well as fixing correct quote fence generation. Fences are generated after searching for maximum length occurence of ` and wrapping the quote block with the generated fence. This allows for proper quoting of messages containing a code-block or another quote inside them. This is the first part of commit for this Pull-request.
1 parent 53eed29 commit 85d3442

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

zulipterminal/helper.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import OrderedDict, defaultdict
66
from functools import wraps
77
from itertools import chain, combinations
8-
from re import ASCII, match
8+
from re import ASCII, MULTILINE, findall, match
99
from threading import Thread
1010
from typing import (
1111
Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, List, Set, Tuple,
@@ -657,6 +657,19 @@ def hash_util_decode(string: str) -> str:
657657
return unquote(string.replace('.', '%'))
658658

659659

660+
def get_unused_fence(content: Any) -> Any:
661+
fence_length_regex = '^ {0,3}(`{3,})'
662+
max_length_fence = 3
663+
664+
matches = findall(fence_length_regex, content,
665+
flags=MULTILINE)
666+
if len(matches) != 0:
667+
max_length_fence = max(max_length_fence,
668+
len(max(matches, key=len)) + 1)
669+
670+
return '`' * max_length_fence
671+
672+
660673
def all_user_ids_in_pm(message: Any) -> Any:
661674
"""
662675
Returns a sorted list of all user_ids invloved in

zulipterminal/ui_tools/boxes.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
STREAM_TOPIC_SEPARATOR, TIME_MENTION_MARKER,
2323
)
2424
from zulipterminal.helper import (
25-
Message, absolute_url_of_message, format_string, match_emoji, match_group,
26-
match_stream, match_topics, match_user,
25+
Message, absolute_url_of_message, format_string, get_unused_fence,
26+
match_emoji, match_group, match_stream, match_topics, match_user,
2727
)
2828
from zulipterminal.ui_tools.buttons import EditModeButton
2929
from zulipterminal.ui_tools.tables import render_table
@@ -1200,16 +1200,16 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
12001200
# ```quote
12011201
# message_content
12021202
# ```
1203-
quote = '@_**{}|{}** '.format(self.message['sender_full_name'],
1204-
self.message['sender_id'])
1205-
1206-
# FIXME: The type and generation of link needs to be discussed.
1207-
# For interoperability webapp link should be given, but
1208-
# that would not narrow to the quoted message inside ZT.
12091203
absolute_url = absolute_url_of_message(self.model, self.message)
1210-
quote += '[said]({}):\n'.format(absolute_url)
1211-
quote += '```quote\n' + self.model.client.get_raw_message(
1212-
self.message['id'])['raw_content'] + '\n```\n'
1204+
fence = get_unused_fence(self.model.client.get_raw_message(
1205+
self.message['id'])['raw_content'])
1206+
1207+
quote = '@_**{0}|{1}** [said]({2}):\n{3}quote\n{4}\n{3}\n'.format(
1208+
self.message['sender_full_name'],
1209+
self.message['sender_id'], absolute_url, fence,
1210+
self.model.client.get_raw_message(
1211+
self.message['id'])['raw_content'])
1212+
12131213
self.model.controller.view.write_box.msg_write_box.set_edit_text(
12141214
quote)
12151215
self.model.controller.view.write_box.msg_write_box.set_edit_pos(

0 commit comments

Comments
 (0)