public static string ExpandTemplate(string articleText, string articleTitle, Dictionary<Regex, string> regexes,
bool includeComment)
{
foreach (KeyValuePair<Regex, string> p in regexes)
{
string originalArticleText = "";
while (!originalArticleText.Equals(articleText))
{
originalArticleText = articleText;
// avoid matching on previously commented out calls
Match m = p.Key.Match(ReplaceWithSpaces(articleText, WikiRegexes.Comments));
if (!m.Success)
continue;
string call = m.Value, result;
if (Globals.UnitTestMode)
result = "Expanded template test return";
else
{
string expandUri = Variables.URLApi + "?action=expandtemplates&prop=wikitext&format=json&title=" +
WikiEncode(articleTitle) + "&text=" + HttpUtility.UrlEncode(call);
try
{
result = HttpUtility.HtmlDecode(
JObject.Parse(GetHTML(expandUri))["expandtemplates"]["wikitext"].ToString()
);
}
catch
{
continue;
}
}
bool skipArticle;
result = new Parsers().Unicodify(result, out skipArticle);
if (includeComment)
result = result + "<!-- " + call + " -->";
articleText = articleText.Replace(call, result);
}
}
return articleText;
}