Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

mention: Refactor `MENTIONS_RE` and simplify its related code path.

akshatdaltond5a36ac5e239
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py
index 919d4227880..347ff58ef4f 100644
--- a/zerver/lib/markdown/__init__.py
+++ b/zerver/lib/markdown/__init__.py
@@ -1678,7 +1678,6 @@ class BlockQuoteProcessor(markdown.blockprocessors.BlockQuoteProcessor):
# Original regex for blockquote is RE = re.compile(r'(^|\n)[ ]{0,3}>[ ]?(.*)')
RE = re.compile(r"(^|\n)(?!(?:[ ]{0,3}>\s*(?:$|\n))*(?:$|\n))" r"[ ]{0,3}>[ ]?(.*)")
- mention_re = re.compile(mention.MENTIONS_RE)
# run() is very slightly forked from the base class; see notes below.
def run(self, parent: Element, blocks: List[str]) -> None:
@@ -1708,7 +1707,7 @@ class BlockQuoteProcessor(markdown.blockprocessors.BlockQuoteProcessor):
def clean(self, line: str) -> str:
# Silence all the mentions inside blockquotes
- line = re.sub(self.mention_re, lambda m: "@_{}".format(m.group("match")), line)
+ line = mention.MENTIONS_RE.sub(lambda m: "@_**{}**".format(m.group("match")), line)
# And then run the upstream processor's code for removing the '>'
return super().clean(line)
@@ -1809,19 +1808,20 @@ class LinkifierPattern(markdown.inlinepatterns.Pattern):
class UserMentionPattern(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("match")
+ name = m.group("match")
silent = m.group("silent") == "_"
-
db_data = self.md.zulip_db_data
if self.md.zulip_message and db_data is not None:
- if match.startswith("**") and match.endswith("**"):
- name = match[2:-2]
- else:
- return None, None, None
-
wildcard = mention.user_mention_matches_wildcard(name)
# For @**|id** and @**name|id** mention syntaxes.
diff --git a/zerver/lib/mention.py b/zerver/lib/mention.py
index 1ea12da872b..98cfbad5524 100644
--- a/zerver/lib/mention.py
+++ b/zerver/lib/mention.py
@@ -1,9 +1,9 @@
import re
-from typing import Optional, Set, Tuple
+from typing import Match, Optional, Set, Tuple
# Match multi-word string between @** ** or match any one-word
# sequences after @
-MENTIONS_RE = r"(?<![^\s\'\"\(,:<])@(?P<silent>_?)(?P<match>\*\*[^\*]+\*\*)"
+MENTIONS_RE = re.compile(r"(?<![^\s\'\"\(,:<])@(?P<silent>_?)(\*\*(?P<match>[^\*]+)\*\*)")
USER_GROUP_MENTIONS_RE = r"(?<![^\s\'\"\(,:<])@(\*[^\*]+\*)"
wildcards = ["all", "everyone", "stream"]
@@ -13,25 +13,19 @@ def user_mention_matches_wildcard(mention: str) -> bool:
return mention in wildcards
-def extract_mention_text(m: Tuple[str, str]) -> Tuple[Optional[str], bool]:
- # re.findall provides tuples of match elements; we want the second
- # to get the main mention content.
- s = m[1]
- if s.startswith("**") and s.endswith("**"):
- text = s[2:-2]
- if text in wildcards:
- return None, True
- return text, False
- return None, False
+def extract_mention_text(m: Match[str]) -> Tuple[Optional[str], bool]:
+ text = m.group("match")
+ if text in wildcards:
+ return None, True
+ return text, False
def possible_mentions(content: str) -> Tuple[Set[str], bool]:
- matches = re.findall(MENTIONS_RE, content)
# mention texts can either be names, or an extended name|id syntax.
texts = set()
message_has_wildcards = False
- for match in matches:
- text, is_wildcard = extract_mention_text(match)
+ for m in MENTIONS_RE.finditer(content):
+ text, is_wildcard = extract_mention_text(m)
if text:
texts.add(text)
if is_wildcard: