WikiFunctions.Parse.MetaDataSorter.MoveTemplateToSeeAlsoSection C# (CSharp) Method

MoveTemplateToSeeAlsoSection() public static method

Moves template calls to the top of the "see also" section of the article
public static MoveTemplateToSeeAlsoSection ( string articleText, Regex TemplateToMove ) : string
articleText string The article text
TemplateToMove System.Text.RegularExpressions.Regex The template calls to move
return string
        public static string MoveTemplateToSeeAlsoSection(string articleText, Regex TemplateToMove)
        {
            MatchCollection mc = TemplateToMove.Matches(articleText);
            // need to have a 'see also' section to move the template to
            if (mc.Count < 1)
                return articleText;

            string originalArticletext = articleText;
            bool templateMoved = false;

            foreach (Match m in mc)
            {
                string TemplateFound = m.Value;
                Match sa = SeeAlsoSection.Match(articleText);
                string seeAlsoSectionString = sa.Value;
                int seeAlsoIndex = sa.Index;

                // if SeeAlsoSection didn't match then 'see also' must be last section
                if (seeAlsoSectionString.Length == 0)
                {
                    Match sae = SeeAlsoToEnd.Match(articleText);
                    seeAlsoSectionString = sae.Value;
                    seeAlsoIndex = sae.Index;
                }

                // if still not found then no "see also" section to move templates to
                if (seeAlsoSectionString.Length == 0)
                    break;

                // only move templates NOT currently in 'see also'
                if (m.Index < seeAlsoIndex || m.Index > (seeAlsoIndex + seeAlsoSectionString.Length))
                {
                    // remove template, also remove newline after template if template on its own line
                    articleText = Regex.Replace(articleText, @"^" + Regex.Escape(TemplateFound) + @" *(?:\r\n)?", "", RegexOptions.Multiline);

                    articleText = articleText.Replace(TemplateFound, "");

                    // place template at top of see also section
                    articleText = WikiRegexes.SeeAlso.Replace(articleText, "$0" + Tools.Newline(TemplateFound));
                    templateMoved = true;
                }
            }

            if (templateMoved && Tools.UnformattedTextNotChanged(originalArticletext, articleText))
                return articleText;

            return originalArticletext;
        }