Menu

how to append text to a particular section of a page?

Help
Sanketh
2013-12-04
2015-03-15
  • Sanketh

    Sanketh - 2013-12-04

    For example, a page has 5 sections (section1...
    Section5). I want to append some text to section4. How do I do this with dotnetwikibot?

    Thanks in advance,
    Sanketh

     
  • CodeDriller

    CodeDriller - 2013-12-05

    There is no special function for this action. After having loaded the text with Load() function you'll have to find a proper place for insertion yourself. You can use regular expressions or traditional .NET functions like IndexOf(). There is also a useful helper DotNetWikiBot function called Bot.GetMatchesPositions() - http://dotnetwikibot.sourceforge.net/help/html/M_DotNetWikiBot_Bot_GetMatchesPositions.htm . You can use it to find sections and subsections like this:

    List<int> sectionPositions = Bot.GetMatchesPositions(page.text, "\n==", false);
    
     

    Last edit: CodeDriller 2013-12-05
  • Juergen Thomas

    Juergen Thomas - 2015-03-15

    I thank CodeDriller for this hint and add the following code to the page class.

        public List<int> SectionPositions {get; private set; }
        public List<string> SectionContents {get; private set; }
    
        public void ReadSections()
        {
            //  get the positions where a new wiki section starts
            SectionPositions = Bot.GetMatchesPositions(text, "\n=", false);
            //  if there is text before the first heading, insert a section 0
            if(SectionPositions[0] > 0) {
                SectionPositions.Insert(0, 0);
            }
            //  divide all the text to each section;
            //  using Linq may be more easier, but I don't know how
            SectionContents = new List<string>(SectionPositions.Count);
            int i1;
            for(int i = 0; i < SectionPositions.Count; i++) {
                i1 = (i < SectionPositions.Count-1 ? 
                          SectionPositions[i+1] - SectionPositions[i] : 
                          text.Length - SectionPositions[i]);
                SectionContents.Add(text.Substring(SectionPositions[i], i1));
            }
        }
    

    A specific section text is called by SectionContents[sectionNo]. Juergen

    By the way, I use "partial class Page" in order to simplify those additions by adding my own cs file.

     

    Last edit: Juergen Thomas 2015-03-15

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.