mirror of https://github.com/zulip/zulip.git
mention: Refactor `USER_GROUP_MENTIONS_RE` and simplify its related code path.
Earlier, USER_GROUP_MENTIONS_RE was: r"(?<![^\s\'\"\(,:<])@(\*[^\*]+\*)" For the syntax: *foo*, this was unnecessarily capturing it as *foo* and the extraction of `foo` was done using another helper function: `extract_user_group`. This is now changed as: r"(?<![^\s\'\"\(,:<])@(\*(?P<match>[^\*]+)\*)" and extraction of `foo` can be done just by using the named capture group `match`. This change also helps to simplify its related code path.
This commit is contained in:
parent
d5a36ac5e2
commit
f56fca308a
|
@ -52,7 +52,7 @@ from zerver.lib.emoji import EMOTICON_RE, codepoint_to_name, name_to_codepoint,
|
|||
from zerver.lib.exceptions import MarkdownRenderingException
|
||||
from zerver.lib.markdown import fenced_code
|
||||
from zerver.lib.markdown.fenced_code import FENCE_RE
|
||||
from zerver.lib.mention import extract_user_group, possible_mentions, possible_user_group_mentions
|
||||
from zerver.lib.mention import possible_mentions, possible_user_group_mentions
|
||||
from zerver.lib.subdomains import is_static_or_current_realm_url
|
||||
from zerver.lib.tex import render_tex
|
||||
from zerver.lib.thumbnail import user_uploads_or_external
|
||||
|
@ -1869,14 +1869,20 @@ class UserMentionPattern(markdown.inlinepatterns.InlineProcessor):
|
|||
|
||||
|
||||
class UserGroupMentionPattern(markdown.inlinepatterns.InlineProcessor):
|
||||
def __init__(self, compiled_re: Pattern[str], md: markdown.Markdown) -> None:
|
||||
# This is similar to the superclass's small __init__ function,
|
||||
# but we skip the compilation step and let the caller give us
|
||||
# a compiled regex.
|
||||
self.compiled_re = compiled_re
|
||||
self.md = md
|
||||
|
||||
def handleMatch( # type: ignore[override] # supertype incompatible with supersupertype
|
||||
self, m: Match[str], data: str
|
||||
) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]:
|
||||
match = m.group(1)
|
||||
name = m.group("match")
|
||||
db_data = self.md.zulip_db_data
|
||||
|
||||
if self.md.zulip_message and db_data is not None:
|
||||
name = extract_user_group(match)
|
||||
user_group = db_data["mention_data"].get_user_group(name)
|
||||
if user_group:
|
||||
self.md.zulip_message.mentions_user_group_ids.add(user_group.id)
|
||||
|
|
|
@ -4,7 +4,7 @@ from typing import Match, Optional, Set, Tuple
|
|||
# Match multi-word string between @** ** or match any one-word
|
||||
# sequences after @
|
||||
MENTIONS_RE = re.compile(r"(?<![^\s\'\"\(,:<])@(?P<silent>_?)(\*\*(?P<match>[^\*]+)\*\*)")
|
||||
USER_GROUP_MENTIONS_RE = r"(?<![^\s\'\"\(,:<])@(\*[^\*]+\*)"
|
||||
USER_GROUP_MENTIONS_RE = re.compile(r"(?<![^\s\'\"\(,:<])@(\*(?P<match>[^\*]+)\*)")
|
||||
|
||||
wildcards = ["all", "everyone", "stream"]
|
||||
|
||||
|
@ -33,10 +33,5 @@ def possible_mentions(content: str) -> Tuple[Set[str], bool]:
|
|||
return texts, message_has_wildcards
|
||||
|
||||
|
||||
def extract_user_group(matched_text: str) -> str:
|
||||
return matched_text[1:-1]
|
||||
|
||||
|
||||
def possible_user_group_mentions(content: str) -> Set[str]:
|
||||
matches = re.findall(USER_GROUP_MENTIONS_RE, content)
|
||||
return {extract_user_group(match) for match in matches}
|
||||
return {m.group("match") for m in USER_GROUP_MENTIONS_RE.finditer(content)}
|
||||
|
|
Loading…
Reference in New Issue