testing new mention regex

This commit is contained in:
Nicolas Constant 2021-02-02 00:24:33 -05:00
parent c7bf5f79f8
commit c409a93b18
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
3 changed files with 29 additions and 19 deletions

View file

@ -5,6 +5,6 @@ namespace BirdsiteLive.Common.Regexes
public class UserRegexes
{
public static readonly Regex TwitterAccount = new Regex(@"^[a-zA-Z0-9_]+$");
public static readonly Regex Mention = new Regex(@"(.)(@[a-zA-Z0-9_]+)(\s|$|[,;:!?/|-]|(. ))");
public static readonly Regex Mention = new Regex(@"(.?)@([a-zA-Z0-9_]+)(\s|$|[<,;:!?/|-]|(. ))");
}
}

View file

@ -21,7 +21,7 @@ namespace BirdsiteLive.Domain.Tools
//private readonly Regex _hastagRegex = new Regex(@"(?<=[\s>]|^)#(\w*[a-zA-Z0-9_ー]+\w*)\b(?!;)");
//private readonly Regex _hastagRegex = new Regex(@"(?<=[\s>]|^)#(\w*[a-zA-Z0-9_ー]+)\b(?!;)");
private readonly Regex _mentionRegex = new Regex(@"\W(\@[a-zA-Z0-9_ー]+\b)(?!;)");
//private readonly Regex _mentionRegex = new Regex(@"\W(\@[a-zA-Z0-9_ー]+\b)(?!;)");
//private readonly Regex _mentionRegex = new Regex(@"@\w+");
//private readonly Regex _mentionRegex = new Regex(@"(?<=[\s>]|^)@(\w*[a-zA-Z0-9_ー]+\w*)\b(?!;)");
//private readonly Regex _mentionRegex = new Regex(@"(?<=[\s>]|^)@(\w*[a-zA-Z0-9_ー]+)\b(?!;)");
@ -110,17 +110,25 @@ namespace BirdsiteLive.Domain.Tools
//messageContent = Regex.Replace(messageContent, m.ToString(),
// $@" <a href=""{url}"" class=""mention hashtag"" rel=""tag"">#<span>{tag}</span></a>");
messageContent = Regex.Replace(messageContent, m.Groups[0].ToString(),
messageContent = Regex.Replace(messageContent, Regex.Escape(m.Groups[0].ToString()),
$@"{m.Groups[1]}<a href=""{url}"" class=""mention hashtag"" rel=""tag"">#<span>{tag}</span></a>{m.Groups[3]}");
}
// Extract Mentions
if (extractMentions)
{
var mentionMatch = OrderByLength(_mentionRegex.Matches(messageContent));
var mentionMatch = OrderByLength(UserRegexes.Mention.Matches(messageContent));
foreach (Match m in mentionMatch.OrderByDescending(x => x.Length))
{
var mention = m.ToString().Replace("@", string.Empty).Replace("\n", string.Empty).Trim();
var mention = m.Groups[2].ToString();
//var mention = m.ToString().Replace("@", string.Empty).Replace("\n", string.Empty).Trim();
if (!UserRegexes.TwitterAccount.IsMatch(mention))
{
_logger.LogError("Parsing Mention failed: {Mention} on {Content}", mention, messageContent);
continue;
}
var url = $"https://{_instanceSettings.Domain}/users/{mention}";
var name = $"@{mention}@{_instanceSettings.Domain}";
@ -131,16 +139,19 @@ namespace BirdsiteLive.Domain.Tools
type = "Mention"
});
messageContent = Regex.Replace(messageContent, m.ToString(),
$@" <span class=""h-card""><a href=""https://{_instanceSettings.Domain}/@{mention}"" class=""u-url mention"">@<span>{mention}</span></a></span>");
//messageContent = Regex.Replace(messageContent, m.ToString(),
// $@" <span class=""h-card""><a href=""https://{_instanceSettings.Domain}/@{mention}"" class=""u-url mention"">@<span>{mention}</span></a></span>");
messageContent = Regex.Replace(messageContent, Regex.Escape(m.Groups[0].ToString()),
$@"{m.Groups[1]}<span class=""h-card""><a href=""https://{_instanceSettings.Domain}/@{mention}"" class=""u-url mention"">@<span>{mention}</span></a></span>{m.Groups[3]}");
}
}
// Clean up return lines
messageContent = Regex.Replace(messageContent, @"<p> ", "<p>");
messageContent = Regex.Replace(messageContent, @"<br/> ", "<br/>");
messageContent = Regex.Replace(messageContent, @" ", " ");
messageContent = Regex.Replace(messageContent, @" ", " ");
//// Clean up return lines
//messageContent = Regex.Replace(messageContent, @"<p> ", "<p>");
//messageContent = Regex.Replace(messageContent, @"<br/> ", "<br/>");
//messageContent = Regex.Replace(messageContent, @" ", " ");
//messageContent = Regex.Replace(messageContent, @" ", " ");
return (messageContent.Trim(), tags.ToArray());
}

View file

@ -267,7 +267,7 @@ namespace BirdsiteLive.Domain.Tests.Tools
public void Extract_SingleMentionTag_Test()
{
#region Stubs
var message = $"Bla!{Environment.NewLine}@mynickname";
var message = $"Bla!{Environment.NewLine}@mynickname";
#endregion
#region Mocks
@ -293,7 +293,7 @@ namespace BirdsiteLive.Domain.Tests.Tools
public void Extract_SingleMentionTag_SpecialChar_Test()
{
#region Stubs
var message = $"Bla!{Environment.NewLine}@my___nickname";
var message = $"Bla!{Environment.NewLine}@my___nickname";
#endregion
#region Mocks
@ -391,8 +391,7 @@ namespace BirdsiteLive.Domain.Tests.Tools
Assert.IsTrue(result.content.Contains(@"<a href=""https://domain.name/tags/dada"" class=""mention hashtag"" rel=""tag"">#<span>dada</span></a>"));
#endregion
}
[TestMethod]
public void Extract_Emoji_Test()
{
@ -412,9 +411,9 @@ namespace BirdsiteLive.Domain.Tests.Tools
logger.VerifyAll();
Assert.AreEqual(1, result.tags.Length);
Assert.IsTrue(result.content.Contains(
@"😤 <span class=""h-card""><a href=""https://domain.name/@mynickname"" class=""u-url mention"">@<span>mynickname</span></a></span>"));
@"😤<span class=""h-card""><a href=""https://domain.name/@mynickname"" class=""u-url mention"">@<span>mynickname</span></a></span>"));
Assert.IsTrue(result.content.Contains(@"😎 😍 🤗 🤩 😘"));
Assert.IsTrue(result.content.Contains(@"😎😍🤗🤩😘"));
#endregion
}
@ -436,7 +435,7 @@ namespace BirdsiteLive.Domain.Tests.Tools
#region Validations
logger.VerifyAll();
Assert.AreEqual(1, result.tags.Length);
Assert.IsTrue(result.content.Equals(@"bla ( <span class=""h-card""><a href=""https://domain.name/@mynickname"" class=""u-url mention"">@<span>mynickname</span></a></span> test)"));
Assert.IsTrue(result.content.Equals(@"bla (<span class=""h-card""><a href=""https://domain.name/@mynickname"" class=""u-url mention"">@<span>mynickname</span></a></span> test)"));
#endregion
}
}