basic handling of github image links

This commit is contained in:
13xforever 2020-01-17 02:26:48 +05:00
parent 96cee79f03
commit 835478d019
2 changed files with 20 additions and 0 deletions

View File

@ -14,6 +14,7 @@ namespace CompatBot.EventHandlers
{
private const RegexOptions DefaultOptions = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.ExplicitCapture;
public static readonly Regex IssueMention = new Regex(@"(?<issue_mention>\b(issue|pr|pull[ \-]request|bug)\s*#?\s*(?<number>\d+)|(?=\W|^)#(?<also_number>\d{4})|(https?://)github.com/RPCS3/rpcs3/(issues|pull)/(?<another_number>\d+))\b", DefaultOptions);
public static readonly Regex ImageMarkup = new Regex(@"(?<img_markup>!\[(?<img_caption>[^\]]+)\]\((?<img_link>\w+://[^\)]+)\))", DefaultOptions);
private static readonly Regex IssueLink = new Regex(@"github.com/RPCS3/rpcs3/issues/(?<number>\d+)", DefaultOptions);
private static readonly GithubClient.Client Client = new GithubClient.Client();

View File

@ -83,6 +83,25 @@ namespace CompatBot.Utils.ResultFormatters
}
}
}
if (!string.IsNullOrEmpty(desc)
&& GithubLinksHandler.ImageMarkup.Matches(desc) is MatchCollection imgMatches
&& imgMatches.Any())
{
var uniqueLinks = new HashSet<string>(10);
foreach (Match m in imgMatches)
{
if (m.Groups["img_markup"]?.Value is string str
&& !string.IsNullOrEmpty(str)
&& uniqueLinks.Add(str))
{
var caption = m.Groups["img_caption"].Value;
var link = m.Groups["img_link"].Value;
if (!string.IsNullOrEmpty(caption))
caption = " " + caption;
desc = desc.Replace(str, $"[🖼{caption}]({link})");
}
}
}
desc = desc.Trim(EmbedPager.MaxDescriptionLength);
builder ??= new DiscordEmbedBuilder {Title = prDesc, Url = url, Description = desc, Color = Config.Colors.DownloadLinks};
var currentCommit = currentPrInfo?.MergeCommitSha;