public Section(ParsedItem iHeading, Section iParentSection) { cPImHeading = iHeading; cSecParentSection = iParentSection; cLisSubItems = new List<ParsedItem>(); cLisSubSections = new List<Section>(); }
private static String RecursiveToString(Section iSection, Int32 iMaxDepth) { StringBuilder pSBrString = new StringBuilder(); if(iMaxDepth == -1 || (Int32)iSection.Heading.LineType <= iMaxDepth) { pSBrString.AppendLine(iSection.Heading.Line.ToString()); foreach (Section curSection in iSection.SubSections) { pSBrString.Append(RecursiveToString(curSection, iMaxDepth)); } } return (pSBrString.ToString()); }
public static Parser Parse(String[] iMarkdownLines) { Parser pParParser = new Parser(); LineType pLTeLastLineType = LineType.none; for(Int32 pIntCurLine = 0; pIntCurLine < iMarkdownLines.Length; pIntCurLine++) { String pStrCurLine = iMarkdownLines[pIntCurLine]; LineType pLTeLineType = GetLineType(pStrCurLine); switch (pLTeLineType) { case LineType.listitem: { if (pLTeLastLineType == LineType.listitem) { if(pIntCurLine < (iMarkdownLines.Length - 1)) { String pStrNextLine = iMarkdownLines[pIntCurLine + 1]; LineType pLTeNextLineType = GetLineType(pStrNextLine); if(pLTeNextLineType != LineType.listitem) { pLTeLastLineType = LineType.listitemend; } } } else { pLTeLineType = LineType.listitemstart; } break; } } ParsedItem pPImItem = new ParsedItem() { Line = pStrCurLine, LineType = pLTeLineType }; pParParser.cLisItems.Add(pPImItem); pLTeLastLineType = pLTeLineType; } Section pSecCurParentSection = null; ParsedItem pPImLastItem = null; for (Int32 pIntParsedItem = 0; pIntParsedItem < pParParser.cLisItems.Count; pIntParsedItem++) { ParsedItem pPImCurItem = pParParser.cLisItems[pIntParsedItem]; if(pPImLastItem != null) { if(pSecCurParentSection != null) { switch(pPImCurItem.LineType) { case LineType.body: { pSecCurParentSection.SubItems.Add(pPImCurItem); break; } case LineType.heading1: case LineType.heading2: case LineType.heading3: case LineType.heading4: case LineType.heading5: case LineType.heading6: { //is this heading higher or lower than our current section heading if((Int32)pPImCurItem.LineType > (Int32)pSecCurParentSection.Heading.LineType) { Section pSecSection = new Section(pPImCurItem, pSecCurParentSection); pSecCurParentSection.SubSections.Add(pSecSection); pSecCurParentSection = pSecSection; } else { Boolean pBlnDropDown = true; while(pBlnDropDown) { if(pSecCurParentSection.ParentSection != null) { Int32 pIntParentSectionHeadingLevel = (Int32)pSecCurParentSection.ParentSection.Heading.LineType; Int32 pIntCurHeadingLevel = (Int32)pPImCurItem.LineType; if(pIntParentSectionHeadingLevel == (pIntCurHeadingLevel - 1)) { Section pSecSection = new Section(pPImCurItem, pSecCurParentSection); pSecCurParentSection.SubSections.Add(pSecSection); pSecCurParentSection = pSecSection; pBlnDropDown = false; } else { pSecCurParentSection = pSecCurParentSection.ParentSection; } } else { Section pSecSection = new Section(pPImCurItem, pSecCurParentSection); pSecCurParentSection.SubSections.Add(pSecSection); pSecCurParentSection = pSecSection; pBlnDropDown = false; } } } break; } case LineType.listitem: case LineType.listitemend: case LineType.listitemstart: { pSecCurParentSection.SubItems.Add(pPImCurItem); break; } case LineType.image: { pSecCurParentSection.SubItems.Add(pPImCurItem); break; } case LineType.blank: { pSecCurParentSection.SubItems.Add(pPImCurItem); break; } default: { throw new Exception(String.Format("Cannot parse LineType '{0}' as section.", pPImCurItem.LineType)); } } } else { throw new Exception("No section has been defined."); } } else { if(pPImCurItem.LineType == LineType.heading1) { Section pSecSection = new Section(pPImCurItem, null); pParParser.cLisSections.Add(pSecSection); pSecCurParentSection = pSecSection; } else { throw new Exception("Document must start with a heading 1 item."); } } pPImLastItem = pPImCurItem; } return (pParParser); }
private static void RecursiveCreateOrderedSectionList(Section iSection, List<Section> iListOutput) { iListOutput.Add(iSection); foreach (Section curSection in iSection.SubSections) { RecursiveCreateOrderedSectionList(curSection, iListOutput); } }
public static Parser Parse(String[] iMarkdownLines) { Parser pParParser = new Parser(); LineType pLTeLastLineType = LineType.none; for (Int32 pIntCurLine = 0; pIntCurLine < iMarkdownLines.Length; pIntCurLine++) { String pStrCurLine = iMarkdownLines[pIntCurLine]; LineType pLTeLineType = GetLineType(pStrCurLine); switch (pLTeLineType) { case LineType.listitem: { if (pLTeLastLineType == LineType.listitem) { if (pIntCurLine < (iMarkdownLines.Length - 1)) { String pStrNextLine = iMarkdownLines[pIntCurLine + 1]; LineType pLTeNextLineType = GetLineType(pStrNextLine); if (pLTeNextLineType != LineType.listitem) { pLTeLastLineType = LineType.listitemend; } } } else { pLTeLineType = LineType.listitemstart; } break; } } ParsedItem pPImItem = new ParsedItem() { Line = pStrCurLine, LineType = pLTeLineType }; pParParser.cLisItems.Add(pPImItem); pLTeLastLineType = pLTeLineType; } Section pSecCurParentSection = null; ParsedItem pPImLastItem = null; for (Int32 pIntParsedItem = 0; pIntParsedItem < pParParser.cLisItems.Count; pIntParsedItem++) { ParsedItem pPImCurItem = pParParser.cLisItems[pIntParsedItem]; if (pPImLastItem != null) { if (pSecCurParentSection != null) { switch (pPImCurItem.LineType) { case LineType.body: { pSecCurParentSection.SubItems.Add(pPImCurItem); break; } case LineType.heading1: case LineType.heading2: case LineType.heading3: case LineType.heading4: case LineType.heading5: case LineType.heading6: { //is this heading higher or lower than our current section heading if ((Int32)pPImCurItem.LineType > (Int32)pSecCurParentSection.Heading.LineType) { Section pSecSection = new Section(pPImCurItem, pSecCurParentSection); pSecCurParentSection.SubSections.Add(pSecSection); pSecCurParentSection = pSecSection; } else { Boolean pBlnDropDown = true; while (pBlnDropDown) { if (pSecCurParentSection.ParentSection != null) { Int32 pIntParentSectionHeadingLevel = (Int32)pSecCurParentSection.ParentSection.Heading.LineType; Int32 pIntCurHeadingLevel = (Int32)pPImCurItem.LineType; if (pIntParentSectionHeadingLevel == (pIntCurHeadingLevel - 1)) { Section pSecSection = new Section(pPImCurItem, pSecCurParentSection); pSecCurParentSection.SubSections.Add(pSecSection); pSecCurParentSection = pSecSection; pBlnDropDown = false; } else { pSecCurParentSection = pSecCurParentSection.ParentSection; } } else { Section pSecSection = new Section(pPImCurItem, pSecCurParentSection); pSecCurParentSection.SubSections.Add(pSecSection); pSecCurParentSection = pSecSection; pBlnDropDown = false; } } } break; } case LineType.listitem: case LineType.listitemend: case LineType.listitemstart: { pSecCurParentSection.SubItems.Add(pPImCurItem); break; } case LineType.image: { pSecCurParentSection.SubItems.Add(pPImCurItem); break; } case LineType.blank: { pSecCurParentSection.SubItems.Add(pPImCurItem); break; } default: { throw new Exception(String.Format("Cannot parse LineType '{0}' as section.", pPImCurItem.LineType)); } } } else { throw new Exception("No section has been defined."); } } else { if (pPImCurItem.LineType == LineType.heading1) { Section pSecSection = new Section(pPImCurItem, null); pParParser.cLisSections.Add(pSecSection); pSecCurParentSection = pSecSection; } else { throw new Exception("Document must start with a heading 1 item."); } } pPImLastItem = pPImCurItem; } return(pParParser); }