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;
}