protected List <Bold> ParseBolded(HtmlAgilityPack.HtmlNode original) { List <Bold> bolded = new List <Bold>(); HtmlAgilityPack.HtmlNode content = original.CloneNode("Votes", true); RemoveQuotes(content); // strip out quotes List <String> goodColors = new List <string>() { _voteColor }; //"darkolivegreen", "darkgreen", "yellowgreen", "seagreen", //"lime", "palegreen", "olive", "green" RemoveColors(content, goodColors); // strip out colors RemoveNewlines(content); // strip out newlines if (_voteColor == "") { // look for plain bold HtmlAgilityPack.HtmlNodeCollection bolds = content.SelectNodes("child::b"); if (bolds != null) { BoldsFromSet(bolds, bolded); } } else { // look for color,bold. foreach (var n in content.SelectNodes("descendant::font") ?? new HtmlAgilityPack.HtmlNodeCollection(content)) { HtmlAgilityPack.HtmlNodeCollection colorbolds = n.SelectNodes("child::b"); if (colorbolds != null) { BoldsFromSet(colorbolds, bolded); } } // look for bold,color. HtmlAgilityPack.HtmlNodeCollection bolds = content.SelectNodes("descendant::b"); foreach (var n in bolds ?? new HtmlAgilityPack.HtmlNodeCollection(content)) { HtmlAgilityPack.HtmlNodeCollection boldcolors = n.SelectNodes("child::font"); if (boldcolors != null) { BoldsFromSet(boldcolors, bolded); } } // look for span w/color HtmlAgilityPack.HtmlNodeCollection boldspan = content.SelectNodes("descendant::span[starts-with(@style,\"color:red;font-weight:bold;\")]"); if (boldspan != null) { BoldsFromSet(boldspan, bolded); } } return(bolded); }
/// <summary> /// Only document can be select /// </summary> /// <param name="element"></param> /// <returns></returns> private HtmlAgilityPack.HtmlNode CheckElementAndConvert(object element) { HtmlAgilityPack.HtmlNode node = element as HtmlAgilityPack.HtmlNode; if (node != null) { if (node.NodeType == HtmlAgilityPack.HtmlNodeType.Document) { return(node); } HtmlAgilityPack.HtmlDocument root = new HtmlAgilityPack.HtmlDocument(); root.DocumentNode.ChildNodes.Append(node.CloneNode(true)); return(root.DocumentNode); } throw new ArgumentException("Element is not HtmlNode"); }
/// <summary> /// Prepare Html /// </summary> /// <param name="input"></param> /// <param name="output"></param> private void PrepareHtml(HtmlAgilityPack.HtmlNode input, HtmlAgilityPack.HtmlNode output) { HtmlAgilityPack.HtmlNode parent = output; switch (input.NodeType) { case HtmlAgilityPack.HtmlNodeType.Document: break; case HtmlAgilityPack.HtmlNodeType.Element: if (!availableTags.Contains(input.OriginalName)) { return; } if (input.OriginalName.Equals("img")) { output.AppendChild(input.CloneNode(true)); return; } parent = output.AppendChild(input.CloneNode(false)); string newStyle = string.Empty; string style = input.GetAttributeValue("style", string.Empty); string href = input.GetAttributeValue("href", string.Empty); parent.Attributes.RemoveAll(); if (style != string.Empty) { foreach (string item in style.Split(';')) { if ((item.Contains("font-weight") && item.Contains("bold")) || (item.Contains("font-style") && item.Contains("italic")) || (item.Contains("text-decoration") && item.Contains("underline"))) { newStyle += string.Format("{0};", item); } } } if (newStyle != string.Empty) { parent.SetAttributeValue("style", newStyle); } if (href != string.Empty) { parent.SetAttributeValue("href", href); } break; case HtmlAgilityPack.HtmlNodeType.Text: output.AppendChild(input.CloneNode(true)); return; default: return; } foreach (HtmlAgilityPack.HtmlNode child in input.ChildNodes) { PrepareHtml(child, parent); } }