private static void handleAdvanced(Regex regex, MessageFormatterResult result, int startIndex = 0) { foreach (Match m in regex.Matches(result.Text, startIndex)) { var index = m.Index; var link = m.Groups["link"].Value; var indexLength = link.Length; var details = getLinkDetails(link); result.Links.Add(new Link(link, index, indexLength, details.Action, details.Argument)); } }
private static void handleMatches(Regex regex, string display, string link, MessageFormatterResult result, int startIndex = 0, LinkAction?linkActionOverride = null, char[] escapeChars = null) { int captureOffset = 0; foreach (Match m in regex.Matches(result.Text, startIndex)) { var index = m.Index - captureOffset; var displayText = string.Format(display, m.Groups[0], m.Groups["text"].Value, m.Groups["url"].Value).Trim(); var linkText = string.Format(link, m.Groups[0], m.Groups["text"].Value, m.Groups["url"].Value).Trim(); if (displayText.Length == 0 || linkText.Length == 0) { continue; } // Remove backslash escapes in front of the characters provided in escapeChars if (escapeChars != null) { displayText = escapeChars.Aggregate(displayText, (current, c) => current.Replace($"\\{c}", c.ToString())); } // Check for encapsulated links if (result.Links.Find(l => (l.Index <= index && l.Index + l.Length >= index + m.Length) || (index <= l.Index && index + m.Length >= l.Index + l.Length)) == null) { result.Text = result.Text.Remove(index, m.Length).Insert(index, displayText); //since we just changed the line display text, offset any already processed links. result.Links.ForEach(l => l.Index -= l.Index > index ? m.Length - displayText.Length : 0); var details = GetLinkDetails(linkText); result.Links.Add(new Link(linkText, index, displayText.Length, linkActionOverride ?? details.Action, details.Argument)); //adjust the offset for processing the current matches group. captureOffset += m.Length - displayText.Length; } } }
private static void handleAdvanced(Regex regex, MessageFormatterResult result, int startIndex = 0) { foreach (Match m in regex.Matches(result.Text, startIndex)) { var index = m.Index; var linkText = m.Groups["link"].Value; var indexLength = linkText.Length; var details = GetLinkDetails(linkText); var link = new Link(linkText, index, indexLength, details.Action, details.Argument); // sometimes an already-processed formatted link can reduce to a simple URL, too // (example: [mean example - https://osu.ppy.sh](https://osu.ppy.sh)) // therefore we need to check if any of the pre-existing links contains the raw one we found if (result.Links.All(existingLink => !existingLink.Overlaps(link))) { result.Links.Add(link); } } }
private static void handleMatches(Regex regex, string display, string link, MessageFormatterResult result, int startIndex = 0, LinkAction?linkActionOverride = null) { int captureOffset = 0; foreach (Match m in regex.Matches(result.Text, startIndex)) { var index = m.Index - captureOffset; var displayText = string.Format(display, m.Groups[0], m.Groups.Count > 1 ? m.Groups[1].Value : "", m.Groups.Count > 2 ? m.Groups[2].Value : "").Trim(); var linkText = string.Format(link, m.Groups[0], m.Groups.Count > 1 ? m.Groups[1].Value : "", m.Groups.Count > 2 ? m.Groups[2].Value : "").Trim(); if (displayText.Length == 0 || linkText.Length == 0) { continue; } // Check for encapsulated links if (result.Links.Find(l => l.Index <= index && l.Index + l.Length >= index + m.Length || index <= l.Index && index + m.Length >= l.Index + l.Length) == null) { result.Text = result.Text.Remove(index, m.Length).Insert(index, displayText); //since we just changed the line display text, offset any already processed links. result.Links.ForEach(l => l.Index -= l.Index > index ? m.Length - displayText.Length : 0); var details = getLinkDetails(linkText); result.Links.Add(new Link(linkText, index, displayText.Length, linkActionOverride ?? details.Action, details.Argument)); //adjust the offset for processing the current matches group. captureOffset += m.Length - displayText.Length; } } }
private static MessageFormatterResult format(string toFormat, int startIndex = 0, int space = 3) { var result = new MessageFormatterResult(toFormat); // handle the [link display] format handleMatches(new_link_regex, "{1}", "{2}", result, startIndex, escapeChars: new[] { '[', ']' }); // handle the standard markdown []() format handleMatches(markdown_link_regex, "{1}", "{2}", result, startIndex, escapeChars: new[] { '[', ']' }); // handle the ()[] link format handleMatches(old_link_regex, "{1}", "{2}", result, startIndex, escapeChars: new[] { '(', ')' }); // handle wiki links handleMatches(wiki_regex, "{1}", "https://osu.ppy.sh/wiki/{1}", result, startIndex); // handle bare links handleAdvanced(advanced_link_regex, result, startIndex); // handle editor times handleMatches(time_regex, "{0}", "osu://edit/{0}", result, startIndex, LinkAction.OpenEditorTimestamp); // handle channels handleMatches(channel_regex, "{0}", "osu://chan/{0}", result, startIndex, LinkAction.OpenChannel); var empty = ""; while (space-- > 0) { empty += "\0"; } handleMatches(emoji_regex, empty, "{0}", result, startIndex); return(result); }