Skip to content

Commit

Permalink
boxes/helper: Fixed generation of quote fences.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zee-bit committed Dec 24, 2020
1 parent ceb4711 commit 456b883
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
15 changes: 14 additions & 1 deletion zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import OrderedDict, defaultdict
from functools import wraps
from itertools import chain, combinations
from re import ASCII, match
from re import ASCII, MULTILINE, findall, match
from threading import Thread
from typing import (
Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, List, Set, Tuple,
Expand Down Expand Up @@ -657,6 +657,19 @@ def hash_util_decode(string: str) -> str:
return unquote(string.replace('.', '%'))


def get_unused_fence(content: Any) -> Any:
fence_length_regex = '^ {0,3}(`{3,})'
max_length_fence = 3

matches = findall(fence_length_regex, content,
flags=MULTILINE)
if len(matches) != 0:
max_length_fence = max(max_length_fence,
len(max(matches, key=len)) + 1)

return '`' * max_length_fence


def all_user_ids_in_pm(message: Any) -> Any:
"""
Returns a sorted list of all user_ids invloved in
Expand Down
21 changes: 10 additions & 11 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
STREAM_TOPIC_SEPARATOR, TIME_MENTION_MARKER,
)
from zulipterminal.helper import (
Message, absolute_url_of_message, format_string, match_emoji, match_group,
match_stream, match_topics, match_user,
Message, absolute_url_of_message, format_string, get_unused_fence,
match_emoji, match_group, match_stream, match_topics, match_user,
)
from zulipterminal.ui_tools.buttons import EditModeButton
from zulipterminal.ui_tools.tables import render_table
Expand Down Expand Up @@ -1192,16 +1192,15 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
# ```quote
# message_content
# ```
quote = '@_**{}|{}** '.format(self.message['sender_full_name'],
self.message['sender_id'])

# FIXME: The type and generation of link needs to be discussed.
# For interoperability webapp link should be given, but
# that would not narrow to the quoted message inside ZT.
absolute_url = absolute_url_of_message(self.model, self.message)
quote += '[said]({}):\n'.format(absolute_url)
quote += '```quote\n' + self.model.client.get_raw_message(
self.message['id'])['raw_content'] + '\n```\n'
quote = '@_**{}|{}** [said]({}):\n'.format(
self.message['sender_full_name'],
self.message['sender_id'], absolute_url)

fence = get_unused_fence(self.model.client.get_raw_message(
self.message['id'])['raw_content'])
quote += f'{fence}quote\n' + self.model.client.get_raw_message(
self.message['id'])['raw_content'] + f'\n{fence}\n'
self.model.controller.view.write_box.msg_write_box.set_edit_text(
quote)
self.model.controller.view.write_box.msg_write_box.set_edit_pos(
Expand Down

0 comments on commit 456b883

Please sign in to comment.