public virtual void BuildTableOfContentsItem(StringBuilder builder, HierarchyItem <Heading> heading) { if (builder == null) { throw new ArgumentNullException("builder"); } if (heading == null) { throw new ArgumentNullException("heading"); } StartHierarchyItem(builder); builder.Append("<a href=\"#"); builder.Append(heading.Item.AnchorName); builder.Append("\">"); builder.Append(heading.Item.Title); builder.Append("</a>"); BuildTableOfContentsLayer(builder, heading.Children); EndHierarchyItem(builder); }
public HierarchyCollection <Heading> GetHeadingHierarchy(string html) { var hierarchy = new HierarchyCollection <Heading>(); var headingsStack = new Stack <HierarchyItem <Heading> >(); foreach (var heading in GetHeadings(html)) { var hiearchyItem = new HierarchyItem <Heading>(heading); var addedItem = false; while (headingsStack.Count > 0) { // Keep looking up the heading hierarchy until we find the // first item that is a higher heading type than the current // heading var parentHeading = headingsStack.Peek(); if (parentHeading.Item.HeadingType < heading.HeadingType) { headingsStack.Push(hiearchyItem); parentHeading.Children.Add(hiearchyItem); addedItem = true; break; } headingsStack.Pop(); } if (!addedItem) { headingsStack.Push(hiearchyItem); hierarchy.Add(hiearchyItem); } } return(hierarchy); }