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

MoveTemplate() public static method

Moves matching templates in the zeroth section to the top of the article (en only)
public static MoveTemplate ( string articleText, Regex templateRegex ) : string
articleText string The wiki text of the article.
templateRegex System.Text.RegularExpressions.Regex Regex matching the templates to be moved
return string
        public static string MoveTemplate(string articleText, Regex templateRegex)
        {
            string originalArticletext = articleText;

            // get the zeroth section (text upto first heading)
            string zerothSection = Tools.GetZerothSection(articleText);

            // avoid moving commented out templates
            if (!Variables.LangCode.Equals("en") || !templateRegex.IsMatch(WikiRegexes.Comments.Replace(zerothSection, "")))
                return articleText;

            // get the rest of the article including first heading (may be null if article has no headings)
            string restOfArticle = articleText.Substring(zerothSection.Length);

            string strTemplates = "";

            foreach (Match m in templateRegex.Matches(zerothSection))
            {
                strTemplates += m.Value + "\r\n";

                // remove colons before template
                zerothSection = zerothSection.Replace(":" + m.Value + "\r\n", "");

                // additionally, remove whitespace after template
                zerothSection = Regex.Replace(zerothSection, Regex.Escape(m.Value) + @" *(?:\r\n)?", "");
            }

            articleText = strTemplates + zerothSection + restOfArticle;

            // avoid moving commented out templates, round 2
            if (Tools.UnformattedTextNotChanged(originalArticletext, articleText))
                return articleText;

            return originalArticletext;
        }