slack importer: Add regex for mail links.

`<mailto:foo@foo.com>` is changed to `mailto:foo@foo.com`.
This commit is contained in:
Rhea Parekh 2018-01-21 17:00:54 +05:30 committed by showell
parent df1bb5fd27
commit 11b549e566
2 changed files with 31 additions and 0 deletions

View File

@ -15,6 +15,16 @@
"input": "<http://datausa.io/> this is a good website.", "input": "<http://datausa.io/> this is a good website.",
"conversion_output": "http://datausa.io/ this is a good website." "conversion_output": "http://datausa.io/ this is a good website."
}, },
{
"name": "mailto_format1",
"input": "email me at <mailto:random@gmail.com|random@gmail.com>",
"conversion_output": "email me at mailto:random@gmail.com"
},
{
"name": "mailto_format2",
"input": "email me at <mailto:random@gmail.com>",
"conversion_output": "email me at mailto:random@gmail.com"
},
{ {
"name": "valid_strikethrough_test", "name": "valid_strikethrough_test",
"input": "(~str ike!~, text. ~Also ~)", "input": "(~str ike!~, text. ~Also ~)",

View File

@ -15,6 +15,14 @@ LINK_REGEX = r"""
(\|)?(?:\|([^>]+))? # char after pipe (for slack links) (\|)?(?:\|([^>]+))? # char after pipe (for slack links)
(>) (>)
""" """
SLACK_MAILTO_REGEX = r"""
<((mailto:)? # match `<mailto:`
([\w\.-]+@[\w\.-]+(\.[\w]+)+)) # match email
(\|)? # match pipe
([\w\.-]+@[\w\.-]+(\.[\w]+)+)?> # match email
"""
SLACK_USERMENTION_REGEX = r""" SLACK_USERMENTION_REGEX = r"""
(<@) # Start with '<@' (<@) # Start with '<@'
([a-zA-Z0-9]+) # Here we have the Slack id ([a-zA-Z0-9]+) # Here we have the Slack id
@ -83,6 +91,8 @@ def convert_to_zulip_markdown(text: str, users: List[ZerverFieldsT],
# Check and convert link format # Check and convert link format
text, has_link = convert_link_format(text) text, has_link = convert_link_format(text)
# convert `<mailto:foo@foo.com>` to `mailto:foo@foo.com`
text, has_link = convert_mailto_format(text)
return text, mentioned_users_id, has_link return text, mentioned_users_id, has_link
@ -126,3 +136,14 @@ def convert_link_format(text: str) -> Tuple[str, bool]:
has_link = True has_link = True
text = text.replace(match.group(0), converted_text) text = text.replace(match.group(0), converted_text)
return text, has_link return text, has_link
def convert_mailto_format(text: str) -> Tuple[str, bool]:
"""
1. Converts '<mailto:foo@foo.com>' to 'mailto:foo@foo.com'
2. Converts '<mailto:foo@foo.com|foo@foo.com>' to 'mailto:foo@foo.com'
"""
has_link = False
for match in re.finditer(SLACK_MAILTO_REGEX, text, re.VERBOSE):
has_link = True
text = text.replace(match.group(0), match.group(1))
return text, has_link