WikiFunctions.Parse.Parsers.GetTemplate C# (CSharp) Method

GetTemplate() public static method

Finds first occurrence of a given template in article text. Handles nested templates correctly.
public static GetTemplate ( string articleText, string template ) : string
articleText string Source text
template string Name of template, can be regex without a group capture
return string
        public static string GetTemplate(string articleText, string template)
        {
            Regex search = new Regex(@"(\{\{\s*" + Tools.CaseInsensitive(template) + @"\s*)(?:\||\}|<)", RegexOptions.Singleline);

            // remove commented out templates etc. before searching
            string articleTextCleaned = WikiRegexes.UnformattedText.Replace(articleText, "");

            if (search.IsMatch(articleTextCleaned))
            {
                // extract from original article text
                Match m = search.Match(articleText);

                return m.Success ? ExtractTemplate(articleText, m) : "";
            }

            return "";
        }

Usage Example

Example #1
0
        /// <summary>
        /// Extracts the persondata template from the articleText, along with the persondata comment, if present on the line before
        /// </summary>
        /// <param name="articleText">The wiki text of the article.</param>
        /// <returns></returns>
        public static string RemovePersonData(ref string articleText)
        {
            string strPersonData = (Variables.LangCode.Equals("de")
                                ? Parsers.GetTemplate(articleText, "[Pp]ersonendaten")
                                : Parsers.GetTemplate(articleText, "[Pp]ersondata"));

            if (!string.IsNullOrEmpty(strPersonData))
            {
                articleText = articleText.Replace(strPersonData, "");

                // detection of duplicate persondata template
                if (Variables.LangCode.Equals("en"))
                {
                    string PersonData2 = Parsers.GetTemplate(articleText, "[Pp]ersondata");

                    if (!string.IsNullOrEmpty(PersonData2))
                    {
                        articleText    = articleText.Replace(PersonData2, "");
                        strPersonData += Tools.Newline(PersonData2);
                    }
                }
            }

            // https://en.wikipedia.org/wiki/Wikipedia_talk:AutoWikiBrowser/Bugs/Archive_11#Persondata_comments
            // catch the persondata comment the line before it so that the comment and template aren't separated
            if (articleText.Contains(WikiRegexes.PersonDataCommentEN) && Variables.LangCode == "en")
            {
                articleText   = articleText.Replace(WikiRegexes.PersonDataCommentEN, "");
                strPersonData = WikiRegexes.PersonDataCommentEN + strPersonData;
            }

            return(strPersonData);
        }
All Usage Examples Of WikiFunctions.Parse.Parsers::GetTemplate
Parsers