Ejemplo n.º 1
0
        private void ParseHTagsAndAddAnchors(HtmlNode parentNode)
        {
            foreach (HtmlNode node in parentNode.ChildNodes)
            {
                string tagName = node.Name;
                string title   = node.InnerText;

                if (tagName.StartsWith("h"))
                {
                    // Use the H number (e.g. 2 for H2) as the current level in the tree
                    int level = 0;
                    int.TryParse(tagName.ToLower().Replace("h", ""), out level);

                    // Sanity check for bad markup
                    if (level > 1)
                    {
                        Item item = _tree.AddItemAtLevel(level, title);

                        // Insert an achor tag after the header as a reference
                        HtmlNode anchor = HtmlNode.CreateNode(string.Format(@"<a name=""{0}""></a>", item.Id));
                        node.PrependChild(anchor);
                    }
                }
                else if (node.HasChildNodes)
                {
                    ParseHTagsAndAddAnchors(node);
                }
            }
        }
Ejemplo n.º 2
0
        private void ParseHTagsAndAddAnchors(IHtmlDocument document, IElement parentElement)
        {
            foreach (IElement element in parentElement.Children)
            {
                string tagName = element.NodeName.ToLower();
                string title   = element.TextContent;

                if (tagName.StartsWith("h") && tagName.Length == 2)
                {
                    // Use the H number (e.g. 2 for H2) as the current level in the tree
                    int level = 0;
                    int.TryParse(tagName.ToLower().Replace("h", ""), out level);

                    // Level sanity check for bad markup
                    if (level > 1)
                    {
                        Item item = _tree.AddItemAtLevel(level, title);

                        // Insert an achor tag after the header as a reference
                        IElement anchor = document.CreateElement("a");
                        anchor.SetAttribute("name", item.Id);

                        element.InnerHtml = anchor.OuterHtml + element.InnerHtml;
                    }
                }
                else if (element.HasChildNodes)
                {
                    ParseHTagsAndAddAnchors(document, element);
                }
            }
        }