Advertisement
uwekeim

Convert HTML to plain text

May 10th, 2013
1,954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.82 KB | None | 0 0
  1. namespace ZetaUploader.ServerRuntime.Sys
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text.RegularExpressions;
  6.     using ZetaLongPaths;
  7.  
  8.     /// <summary>
  9.     /// Function to convert HTML source into readable plain text, intended to use in e-mail messages.
  10.     /// </summary>
  11.     /// <remarks>
  12.     /// Changelog:
  13.     ///
  14.     /// - 2018-09-23, Uwe Keim: Added ability to keep links visible and not remove them.
  15.     /// - 2018-09-23, Uwe Keim: Added ability to keep HRs as "-------...".
  16.     /// - 2013-05-10, Uwe Keim: Initial release.
  17.     ///
  18.     /// Use it for whatever you want. Some of our tools that use this function include:
  19.     ///
  20.     /// - https://www.zeta-producer.com
  21.     /// - https://www.zeta-uploader.com
  22.     /// - https://www.zeta-test.com
  23.     /// </remarks>
  24.     public static class HtmlToText2
  25.     {
  26.         /// <summary>
  27.         /// Convert a given HTML source code to readable plain text, intented to use in e-mail message.
  28.         /// </summary>
  29.         public static string ConvertHtmlToPlainText(string html)
  30.         {
  31.             if (string.IsNullOrWhiteSpace(html)) return string.Empty;
  32.  
  33.             // http://pastebin.com/NswerNkQ
  34.             // http://stackoverflow.com/questions/8419517/convert-html-to-plain-text-while-preserving-p-br-ul-ol
  35.             // http://www.codeproject.com/KB/HTML/HTML_to_Plain_Text.aspx
  36.             // https://github.com/soundasleep/html2text/blob/master/src/Html2Text.php for keeping links usable.
  37.  
  38.             // Remove HTML Development formatting
  39.             // Replace line breaks with space
  40.             // because browsers inserts space
  41.             // ReSharper disable LocalizableElement
  42.             var result = html.Replace("\r", @" ");
  43.             // Replace line breaks with space
  44.             // because browsers inserts space
  45.             result = result.Replace("\n", @" ");
  46.             // Remove step-formatting
  47.             result = result.Replace("\t", string.Empty);
  48.             // ReSharper restore LocalizableElement
  49.             // Remove repeating spaces because browsers ignore them
  50.             result = Regex.Replace(result, @"(\s)+", " ", RegexOptions.Singleline);
  51.  
  52.             // Remove the header (prepare first by clearing attributes)
  53.             result = Regex.Replace(result,
  54.                         @"<( )*head([^>])*>", @"<head>",
  55.                         RegexOptions.IgnoreCase);
  56.             result = Regex.Replace(result,
  57.                         @"(<( )*(/)( )*head( )*>)", @"</head>",
  58.                         RegexOptions.IgnoreCase);
  59.             result = Regex.Replace(result,
  60.                         @"(<head>).*(</head>)", string.Empty,
  61.                         RegexOptions.IgnoreCase);
  62.  
  63.             // remove all scripts (prepare first by clearing attributes)
  64.             result = Regex.Replace(result,
  65.                         @"<( )*script([^>])*>", @"<script>",
  66.                         RegexOptions.IgnoreCase);
  67.             result = Regex.Replace(result,
  68.                         @"(<( )*(/)( )*script( )*>)", @"</script>",
  69.                         RegexOptions.IgnoreCase);
  70.             //result = Regex.Replace(result,
  71.             //         @"(<script>)([^(<script>\.</script>)])*(</script>)",
  72.             //         string.Empty,
  73.             //         RegexOptions.IgnoreCase);
  74.             result = Regex.Replace(result,
  75.                         @"(<script>).*(</script>)", string.Empty,
  76.                         RegexOptions.IgnoreCase);
  77.  
  78.             // remove all styles (prepare first by clearing attributes)
  79.             result = Regex.Replace(result,
  80.                         @"<( )*style([^>])*>", @"<style>",
  81.                         RegexOptions.IgnoreCase);
  82.             result = Regex.Replace(result,
  83.                         @"(<( )*(/)( )*style( )*>)", @"</style>",
  84.                         RegexOptions.IgnoreCase);
  85.             result = Regex.Replace(result,
  86.                         @"(<style>).*(</style>)", string.Empty,
  87.                         RegexOptions.IgnoreCase);
  88.  
  89.             // insert tabs in spaces of <td> tags
  90.             result = Regex.Replace(result,
  91.                         @"<( )*td([^>])*>", "\t",
  92.                         RegexOptions.IgnoreCase);
  93.  
  94.             // insert line breaks in places of <BR> and <LI> tags
  95.             result = Regex.Replace(result,
  96.                         @"<( )*br( )*>", "\r",
  97.                         RegexOptions.IgnoreCase);
  98.             result = Regex.Replace(result,
  99.                         @"<( )*li( )*>", "\r- ",
  100.                         RegexOptions.IgnoreCase);
  101.  
  102.             // --
  103.             // Keep HRs.
  104.  
  105.             var hrs = Regex.Matches(
  106.                     result, @"<hr.*?>",
  107.                     RegexOptions.IgnoreCase)
  108.                 .Cast<Match>()
  109.                 .Where(m => m.Success);
  110.  
  111.             foreach (var hr in hrs)
  112.             {
  113.                 // Insert inside "<p>" tags to let the following code add
  114.                 // the correct number of empty lines.
  115.                 result = result.Replace(hr.Value, "<p>---------------------------------------------------------------</p>");
  116.             }
  117.  
  118.             // --
  119.  
  120.             // insert line paragraphs (double line breaks) in place
  121.             // if <P>, <DIV> and <TR> tags
  122.             result = Regex.Replace(result,
  123.                         @"<( )*div([^>])*>", "\r\r",
  124.                         RegexOptions.IgnoreCase);
  125.             result = Regex.Replace(result,
  126.                         @"<( )*tr([^>])*>", "\r\r",
  127.                         RegexOptions.IgnoreCase);
  128.             result = Regex.Replace(result,
  129.                         @"<( )*p([^>])*>", "\r\r",
  130.                         RegexOptions.IgnoreCase);
  131.             result = Regex.Replace(result,
  132.                         @"<( )*ol([^>])*>", "\r\r",
  133.                         RegexOptions.IgnoreCase);
  134.             result = Regex.Replace(result,
  135.                         @"<( )*ul([^>])*>", "\r\r",
  136.                         RegexOptions.IgnoreCase);
  137.  
  138.             // --
  139.             // Extract from Anchors.
  140.  
  141.             var anchors = Regex.Matches(
  142.                     result,
  143.                     @"<a.*?href\s*=\s*(?:['""])(.+?)(?:['""])>(.*?)</a>",
  144.                     RegexOptions.IgnoreCase | RegexOptions.Singleline)
  145.                 .Cast<Match>()
  146.                 .Where(m => m.Success);
  147.  
  148.             foreach (var anchor in anchors)
  149.             {
  150.                 var link = anchor.Groups[1].Value;
  151.                 var text = anchor.Groups[2].Value;
  152.  
  153.                 var coreLink = link;
  154.                 var coreText = text;
  155.  
  156.                 var isSame =
  157.                     coreLink.EqualsNoCase(coreText) ||
  158.                     coreLink.EqualsNoCase($@"mailto:{coreText}") ||
  159.                     coreLink.EqualsNoCase($@"http://{coreText}") ||
  160.                     coreLink.EqualsNoCase($@"https://{coreText}");
  161.  
  162.                 var replacement = isSame || string.IsNullOrEmpty(coreText) ? coreLink : $@"[{coreText}]({coreLink})";
  163.  
  164.                 result = result.Replace(anchor.Value, replacement);
  165.             }
  166.  
  167.             // --
  168.  
  169.             // Remove remaining tags like <a>, links, images,
  170.             // comments etc - anything that's enclosed inside < >
  171.             result = Regex.Replace(result,
  172.                     @"<[^>]*>", string.Empty,
  173.                     RegexOptions.IgnoreCase);
  174.  
  175.             // replace special characters:
  176.             result = Regex.Replace(result,
  177.                         @" ", " ",
  178.                         RegexOptions.IgnoreCase);
  179.  
  180.             result = Regex.Replace(result,
  181.                         @"&bull;", " * ",
  182.                         RegexOptions.IgnoreCase);
  183.             result = Regex.Replace(result,
  184.                         @"&lsaquo;", "<",
  185.                         RegexOptions.IgnoreCase);
  186.             result = Regex.Replace(result,
  187.                         @"&rsaquo;", ">",
  188.                         RegexOptions.IgnoreCase);
  189.             result = Regex.Replace(result,
  190.                         @"&trade;", "(tm)",
  191.                         RegexOptions.IgnoreCase);
  192.             result = Regex.Replace(result,
  193.                         @"&frasl;", "/",
  194.                         RegexOptions.IgnoreCase);
  195.             result = Regex.Replace(result,
  196.                         @"&lt;", "<",
  197.                         RegexOptions.IgnoreCase);
  198.             result = Regex.Replace(result,
  199.                         @"&gt;", ">",
  200.                         RegexOptions.IgnoreCase);
  201.             result = Regex.Replace(result,
  202.                         @"&copy;", "(c)",
  203.                         RegexOptions.IgnoreCase);
  204.             result = Regex.Replace(result,
  205.                         @"&reg;", "(r)",
  206.                         RegexOptions.IgnoreCase);
  207.             // Remove all others. More can be added, see
  208.             // http://hotwired.lycos.com/webmonkey/reference/special_characters/
  209.             result = Regex.Replace(result,
  210.                         @"&(.{2,6});", string.Empty,
  211.                         RegexOptions.IgnoreCase);
  212.  
  213.             // for testing
  214.             //Regex.Replace(result,
  215.             //       this.txtRegex.Text,string.Empty,
  216.             //       RegexOptions.IgnoreCase);
  217.  
  218.             // make line breaking consistent
  219.             // ReSharper disable LocalizableElement
  220.             result = result.Replace("\n", "\r");
  221.             // ReSharper restore LocalizableElement
  222.  
  223.             // Remove extra line breaks and tabs:
  224.             // replace over 2 breaks with 2 and over 4 tabs with 4.
  225.             // Prepare first to remove any whitespaces in between
  226.             // the escaped characters and remove redundant tabs in between line breaks
  227.             result = Regex.Replace(result,
  228.                         "(\r)( )+(\r)", "\r\r",
  229.                         RegexOptions.IgnoreCase);
  230.             result = Regex.Replace(result,
  231.                         "(\t)( )+(\t)", "\t\t",
  232.                         RegexOptions.IgnoreCase);
  233.             result = Regex.Replace(result,
  234.                         "(\t)( )+(\r)", "\t\r",
  235.                         RegexOptions.IgnoreCase);
  236.             result = Regex.Replace(result,
  237.                         "(\r)( )+(\t)", "\r\t",
  238.                         RegexOptions.IgnoreCase);
  239.             // Remove redundant tabs
  240.             result = Regex.Replace(result,
  241.                         "(\r)(\t)+(\r)", "\r\r",
  242.                         RegexOptions.IgnoreCase);
  243.             // Remove multiple tabs following a line break with just one tab
  244.             result = Regex.Replace(result,
  245.                         "(\r)(\t)+", "\r\t",
  246.                         RegexOptions.IgnoreCase);
  247.             // Initial replacement target string for line breaks
  248.             // ReSharper disable LocalizableElement
  249.             var breaks = "\r\r\r";
  250.             // ReSharper restore LocalizableElement
  251.             // Initial replacement target string for tabs
  252.             // ReSharper disable LocalizableElement
  253.             var tabs = "\t\t\t\t\t";
  254.             // ReSharper restore LocalizableElement
  255.             for (var index = 0; index < result.Length; index++)
  256.             {
  257.                 // ReSharper disable LocalizableElement
  258.                 result = result.Replace(breaks, "\r\r");
  259.                 result = result.Replace(tabs, "\t\t\t\t");
  260.                 breaks = breaks + "\r";
  261.                 tabs = tabs + "\t";
  262.                 // ReSharper restore LocalizableElement
  263.             }
  264.  
  265.             // UK: Space at the beginning.
  266.             // ReSharper disable LocalizableElement
  267.             result = result.Replace("\r ", "\r");
  268.             // ReSharper restore LocalizableElement
  269.  
  270.             // UK: Normalize.
  271.             // ReSharper disable LocalizableElement
  272.             result = result.Replace("\r", Environment.NewLine);
  273.             // ReSharper restore LocalizableElement
  274.  
  275.             // That's it.
  276.             return result.Trim();
  277.         }
  278.     }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement