WikiFunctions.Tools.SplitToSections C# (CSharp) Method

SplitToSections() public static method

Splits wikitext to sections based on any level wiki heading. Includes zeroth section
public static SplitToSections ( string articleText ) : string[]
articleText string Page text
return string[]
        public static string[] SplitToSections(string articleText)
        {
            List<string> sections = new List<string>();

            int lastmatchpos = 0;
            foreach (Match m in WikiRegexes.Headings.Matches(articleText))
            {
                if (m.Index > 0) // Don't add empty first section if page starts with heading
                    sections.Add(articleText.Substring(lastmatchpos, m.Index - lastmatchpos));
                lastmatchpos = m.Index;
            }

            // Add text of final section, plus extra newline at end
            sections.Add(articleText.Substring(lastmatchpos) + "\r\n");

            return sections.ToArray();
        }

Usage Example

        /// <summary>
        /// Returns the name of modified section or empty string if more than one section has changed
        /// </summary>
        /// <param name="originalText"></param>
        /// <param name="articleText"></param>
        /// <returns></returns>
        public static string ModifiedSection(string originalText, string articleText)
        {
            string[] sectionsBefore = Tools.SplitToSections(originalText);
            if (sectionsBefore.Length == 0)
                return "";

            string[] sectionsAfter = Tools.SplitToSections(articleText);

            // if number of sections has changed, can't provide section edit summary
            if (sectionsAfter.Length != sectionsBefore.Length)
                return "";

            int sectionsChanged = 0, sectionChangeNumber = 0;

            for (int i = 0; i < sectionsAfter.Length; i++)
            {
                if (!sectionsBefore[i].Equals(sectionsAfter[i]))
                {
                    sectionsChanged++;
                    sectionChangeNumber = i;
                }

                // if multiple sections changed, can't provide section edit summary
                if (sectionsChanged > 1)
                    return "";
            }

            // so SectionsChanged == 1, get heading name from regex
            return WikiRegexes.Headings.Match(sectionsAfter[sectionChangeNumber]).Groups[1].Value.Trim();
        }
Tools