WikiFunctions.Tools.DuplicateWikiLinks C# (CSharp) Method

DuplicateWikiLinks() public static method

Returns a sorted list of duplicate wikilinks in the input text (links converted to first letter upper) in format: name (count)
public static DuplicateWikiLinks ( string text ) : List
text string
return List
        public static List<string> DuplicateWikiLinks(string text)
        {
            List<string> allWikiLinks = Parsers.GetAllWikiLinks(text);

            // If any links in commented out text etc. need to ignore these
            // For performance, extract all unformatted text, then extract links from these and remove from list of all links
            List<string> allUnformattedText = (from Match m in WikiRegexes.UnformattedText.Matches(text)
                                        select m.Value).ToList();

            List<string> linksInUnformattedText = (from Match m in WikiRegexes.SimpleWikiLink.Matches(string.Join(" ", allUnformattedText.ToArray()))
                select m.Value).ToList();

            foreach (string l in linksInUnformattedText)
                allWikiLinks.Remove(l);

            // Make first character uppercase so that [[proton]] and [[Proton]] are marked as duplicate
            allWikiLinks = allWikiLinks.Select(l => Tools.TurnFirstToUpper((l.Contains('|') ? l.Substring(0, l.IndexOf('|')) : l).Trim("[] ".ToCharArray()))).ToList();

            // Take all links found and generate dictionary of link name and count for those with more than one link
            Dictionary<string, int> dupeLinks = allWikiLinks.GroupBy(x => x).Where(g => g.Count() > 1).ToDictionary(x => x.Key, y => y.Count());

            // create list of "Name (count)" from Dictionary
            // don't count wikilinked dates or targetless links as duplicate links
            List<string> dupeWikiLinks = dupeLinks.Where(x => x.Key.Length > 0 && !WikiRegexes.Dates.IsMatch(x.Key) && !WikiRegexes.Dates2.IsMatch(x.Key)).Select(x => x.Key + @" (" + x.Value + @")").ToList();

            // ensure list is sorted
            dupeWikiLinks.Sort();

            return dupeWikiLinks;
        }
Tools