Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. public class Parser {
  2.  
  3. public void parse(string html, DocumentBuilder builder) {
  4.             HtmlDocument docHtml = new HtmlDocument();
  5.             docHtml.LoadHtml(html);
  6.            
  7.  
  8.             HtmlNode currentNode = docHtml.DocumentNode;
  9.             while (currentNode.HasChildNodes || currentNode.NextSibling != null || currentNode.ParentNode.NextSibling != null)
  10.             {
  11.                 if (currentNode.HasChildNodes)
  12.                 {
  13.                     currentNode = currentNode.FirstChild;
  14.                 } else if (currentNode.NextSibling != null)
  15.                 {
  16.                     sortirNode(currentNode, builder);
  17.                     currentNode = currentNode.NextSibling;
  18.                 } else
  19.                 {
  20.                     while (currentNode != docHtml.DocumentNode)
  21.                     {
  22.                         if (currentNode.ParentNode.NextSibling != null)
  23.                         {
  24.                             sortirNode(currentNode, builder);
  25.                             sortirNode(currentNode.ParentNode, builder);
  26.                             currentNode = currentNode.ParentNode.NextSibling;
  27.                             break;
  28.                         } else
  29.                         {
  30.                             sortirNode(currentNode, builder);
  31.                             currentNode = currentNode.ParentNode;
  32.                         }
  33.                     }
  34.                 }
  35.                
  36.  
  37.                 if (currentNode.Name == "ul")
  38.                 {
  39.                     builder.Writeln();
  40.                     builder.InsertBreak(BreakType.ParagraphBreak);
  41.                     builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.ListBullet;
  42.                 }
  43.  
  44.                 if (currentNode.Name == "li" && ((currentNode.PreviousSibling != null) && (currentNode.PreviousSibling.Name == "li")))
  45.                 {
  46.                     builder.Writeln();
  47.                     builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.ListBullet;
  48.                 }
  49.  
  50.                 if (currentNode.Name == "#text")
  51.                 {
  52.  
  53.                     if (currentNode.Ancestors("b").Count() > 0 || currentNode.Ancestors("strong").Count() > 0)
  54.                     {
  55.                         builder.Font.Bold = true;
  56.                     }
  57.                     else
  58.                     {
  59.                         builder.Font.Bold = false;
  60.                     }
  61.  
  62.                     if (currentNode.Ancestors("a").Count() > 0 || currentNode.Ancestors("u").Count() > 0)
  63.                     {
  64.                         builder.Font.Underline = Underline.Single;
  65.                     }
  66.                     else
  67.                     {
  68.                         builder.Font.Underline = Underline.None;
  69.                     }
  70.  
  71.                     if (currentNode.Ancestors("em").Count() > 0 )
  72.                     {
  73.                         builder.Font.Italic = true;
  74.                     }
  75.                     else
  76.                     {
  77.                         builder.Font.Italic = false;
  78.                     }
  79.  
  80.                     if (currentNode.Ancestors("h2").Count() > 0)
  81.                     {
  82.                         builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
  83.                     }
  84.  
  85.  
  86.                     builder.Write(currentNode.InnerText);
  87.                 }
  88.                 if (currentNode.Name == "p")
  89.                 {
  90.                     builder.InsertBreak(BreakType.ParagraphBreak);
  91.                     builder.InsertParagraph();
  92.                 }
  93.  
  94.             }
  95. }
  96.  
  97. public void sortirNode(HtmlNode node, DocumentBuilder builder)
  98.         {
  99.             if (node.Name == "h2")
  100.             {
  101.                 builder.InsertBreak(BreakType.ParagraphBreak);
  102.                 builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
  103.             }
  104.         }
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement