WikiFunctions.Tools.ExpandTemplate C# (CSharp) Method

ExpandTemplate() public static method

Expands (substitutes) template calls using the API
public static ExpandTemplate ( string articleText, string articleTitle, string>.Dictionary regexes, bool includeComment ) : string
articleText string The text of the article
articleTitle string The title of the article
regexes string>.Dictionary Dictionary of templates to substitute
includeComment bool
return string
        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;
        }

Usage Example

Example #1
0
        public string SubstituteTemplates(string ArticleText, string ArticleTitle)
        {
            if (Regexes.Count == 0)
            {
                return(ArticleText);                    // nothing to substitute
            }
            if (chkIgnoreUnformatted.Checked)
            {
                ArticleText = RemoveUnformatted.HideUnformatted(ArticleText);
            }
            if (!chkUseExpandTemplates.Checked)
            {
                foreach (KeyValuePair <Regex, string> p in Regexes)
                {
                    ArticleText = p.Key.Replace(ArticleText, p.Value);
                }
            }
            else
            {
                ArticleText = Tools.ExpandTemplate(ArticleText, ArticleTitle, Regexes, chkIncludeComment.Checked);
            }

            if (chkIgnoreUnformatted.Checked)
            {
                ArticleText = RemoveUnformatted.AddBackUnformatted(ArticleText);
            }

            return(ArticleText);
        }
Tools