Пример #1
0
 public MDSection ParentForLevel(int level)
 {
     if (Parent == null || level > Level)
     {
         return(null);
     }
     return(Parent.Level < level? Parent : Parent.ParentForLevel(level));
 }
Пример #2
0
 public MDSection Parse()
 {
     Root = new MDSection("", 0);
     while (!file.EndOfStream)
     {
         var line = file.ReadLine();
         //check for section
         var match = section.Match(line);
         if (match.Success)
         {
             var level = match.Groups[1].Value.Length;
             var title = ConvertEmphasis(match.Groups[2].Value);
             if (sec == null)
             {
                 sec = Root.AddSubsection(title, level);                                 //first section
             }
             else if (level > sec.Level)
             {
                 sec.Flush(); sec = sec.AddSubsection(title, level);
             }                                                                           //child
             else
             {
                 sec.Flush(); sec = sec.ParentForLevel(level).AddSubsection(title, level);
             }                                                                                                      //sibling or cousin
         }
         else
         {
             sec.AddLine(line);
         }
     }
     if (sec != null)
     {
         sec.Flush();
     }
     //if the document has a single top section, chroot to it
     if (Root.NoText && Root.Subsections.Count == 1)
     {
         Root        = Root.Subsections[0];
         Root.Parent = null;
     }
     return(Root);
 }