Advertisement
digioz

HTML to RTF

Jun 16th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.29 KB | None | 0 0
  1. public static string ConvertHtmlToText(string source) {
  2.  
  3.             string result;
  4.  
  5.             // Remove HTML Development formatting
  6.             // Replace line breaks with space
  7.             // because browsers inserts space
  8.             result = source.Replace("\r", " ");
  9.             // Replace line breaks with space
  10.             // because browsers inserts space
  11.             result = result.Replace("\n", " ");
  12.             // Remove step-formatting
  13.             result = result.Replace("\t", string.Empty);
  14.             // Remove repeating speces becuase browsers ignore them
  15.             result = System.Text.RegularExpressions.Regex.Replace(result,
  16.                                                                   @"( )+", " ");
  17.  
  18.             // Remove the header (prepare first by clearing attributes)
  19.             result = System.Text.RegularExpressions.Regex.Replace(result,
  20.                      @"<( )*head([^>])*>", "<head>",
  21.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  22.             result = System.Text.RegularExpressions.Regex.Replace(result,
  23.                      @"(<( )*(/)( )*head( )*>)", "</head>",
  24.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  25.             result = System.Text.RegularExpressions.Regex.Replace(result,
  26.                      "(<head>).*(</head>)", string.Empty,
  27.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  28.  
  29.             // remove all scripts (prepare first by clearing attributes)
  30.             result = System.Text.RegularExpressions.Regex.Replace(result,
  31.                      @"<( )*script([^>])*>", "<script>",
  32.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  33.             result = System.Text.RegularExpressions.Regex.Replace(result,
  34.                      @"(<( )*(/)( )*script( )*>)", "</script>",
  35.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  36.             //result = System.Text.RegularExpressions.Regex.Replace(result,
  37.             //         @"(<script>)([^(<script>\.</script>)])*(</script>)",
  38.             //         string.Empty,
  39.             //         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  40.             result = System.Text.RegularExpressions.Regex.Replace(result,
  41.                      @"(<script>).*(</script>)", string.Empty,
  42.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  43.  
  44.             // remove all styles (prepare first by clearing attributes)
  45.             result = System.Text.RegularExpressions.Regex.Replace(result,
  46.                      @"<( )*style([^>])*>", "<style>",
  47.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  48.             result = System.Text.RegularExpressions.Regex.Replace(result,
  49.                      @"(<( )*(/)( )*style( )*>)", "</style>",
  50.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  51.             result = System.Text.RegularExpressions.Regex.Replace(result,
  52.                      "(<style>).*(</style>)", string.Empty,
  53.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  54.  
  55.             // insert tabs in spaces of <td> tags
  56.             result = System.Text.RegularExpressions.Regex.Replace(result,
  57.                      @"<( )*td([^>])*>", "\t",
  58.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  59.  
  60.             // insert line breaks in places of <BR> and <LI> tags
  61.             result = System.Text.RegularExpressions.Regex.Replace(result,
  62.                      @"<( )*br( )*>", "\r",
  63.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  64.             result = System.Text.RegularExpressions.Regex.Replace(result,
  65.                      @"<( )*li( )*>", "\r",
  66.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  67.  
  68.             // insert line paragraphs (double line breaks) in place
  69.             // if <P>, <DIV> and <TR> tags
  70.             result = System.Text.RegularExpressions.Regex.Replace(result,
  71.                      @"<( )*div([^>])*>", "\r\r",
  72.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  73.             result = System.Text.RegularExpressions.Regex.Replace(result,
  74.                      @"<( )*tr([^>])*>", "\r\r",
  75.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  76.             result = System.Text.RegularExpressions.Regex.Replace(result,
  77.                      @"<( )*p([^>])*>", "\r\r",
  78.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  79.  
  80.             // Remove remaining tags like <a>, links, images,
  81.             // comments etc - anything thats enclosed inside < >
  82.             result = System.Text.RegularExpressions.Regex.Replace(result,
  83.                      @"<[^>]*>", string.Empty,
  84.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  85.  
  86.             // replace special characters:
  87.             result = System.Text.RegularExpressions.Regex.Replace(result,
  88.                      @"&nbsp;", " ",
  89.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  90.  
  91.             result = System.Text.RegularExpressions.Regex.Replace(result,
  92.                      @"&bull;", " * ",
  93.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  94.             result = System.Text.RegularExpressions.Regex.Replace(result,
  95.                      @"&lsaquo;", "<",
  96.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  97.             result = System.Text.RegularExpressions.Regex.Replace(result,
  98.                      @"&rsaquo;", ">",
  99.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  100.             result = System.Text.RegularExpressions.Regex.Replace(result,
  101.                      @"&trade;", "(tm)",
  102.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  103.             result = System.Text.RegularExpressions.Regex.Replace(result,
  104.                      @"&frasl;", "/",
  105.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  106.             result = System.Text.RegularExpressions.Regex.Replace(result,
  107.                      @"<", "<",
  108.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  109.             result = System.Text.RegularExpressions.Regex.Replace(result,
  110.                      @">", ">",
  111.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  112.             result = System.Text.RegularExpressions.Regex.Replace(result,
  113.                      @"&copy;", "(c)",
  114.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  115.             result = System.Text.RegularExpressions.Regex.Replace(result,
  116.                      @"&reg;", "(r)",
  117.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  118.             // Remove all others. More can be added, see
  119.             // http://hotwired.lycos.com/webmonkey/reference/special_characters/
  120.             result = System.Text.RegularExpressions.Regex.Replace(result,
  121.                      @"&(.{2,6});", string.Empty,
  122.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  123.  
  124.  
  125.             // make line breaking consistent
  126.             result = result.Replace("\n", "\r");
  127.  
  128.             // Remove extra line breaks and tabs:
  129.             // replace over 2 breaks with 2 and over 4 tabs with 4.
  130.             // Prepare first to remove any whitespaces inbetween
  131.             // the escaped characters and remove redundant tabs inbetween linebreaks
  132.             result = System.Text.RegularExpressions.Regex.Replace(result,
  133.                      "(\r)( )+(\r)", "\r\r",
  134.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  135.             result = System.Text.RegularExpressions.Regex.Replace(result,
  136.                      "(\t)( )+(\t)", "\t\t",
  137.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  138.             result = System.Text.RegularExpressions.Regex.Replace(result,
  139.                      "(\t)( )+(\r)", "\t\r",
  140.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  141.             result = System.Text.RegularExpressions.Regex.Replace(result,
  142.                      "(\r)( )+(\t)", "\r\t",
  143.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  144.             // Remove redundant tabs
  145.             result = System.Text.RegularExpressions.Regex.Replace(result,
  146.                      "(\r)(\t)+(\r)", "\r\r",
  147.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  148.             // Remove multible tabs followind a linebreak with just one tab
  149.             result = System.Text.RegularExpressions.Regex.Replace(result,
  150.                      "(\r)(\t)+", "\r\t",
  151.                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  152.             // Initial replacement target string for linebreaks
  153.             string breaks = "\r\r\r";
  154.             // Initial replacement target string for tabs
  155.             string tabs = "\t\t\t\t\t";
  156.             for (int index = 0; index < result.Length; index++) {
  157.                 result = result.Replace(breaks, "\r\r");
  158.                 result = result.Replace(tabs, "\t\t\t\t");
  159.                 breaks = breaks + "\r";
  160.                 tabs = tabs + "\t";
  161.             }
  162.  
  163.             // Thats it.
  164.             return result;
  165.  
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement