Hi,
It is a great job what you have done here. I've rewriten the code a litlebit it may usefull
public static class TextConverter
{
public static string ConvertHtmlToText(string source)
{
string AmpStr;
MatchCollection AmpCodes;
Dictionary<string, int> SpecChars = new Dictionary<string, int>();
SpecChars.Add("Á", 193); SpecChars.Add("á", 225); SpecChars.Add("Â", 194);
SpecChars.Add("â", 226); SpecChars.Add("´", 180); SpecChars.Add("Æ", 198);
SpecChars.Add("æ", 230); SpecChars.Add("À", 192); SpecChars.Add("à", 224);
SpecChars.Add("ℵ", 8501); SpecChars.Add("Α", 913); SpecChars.Add("α", 945);
SpecChars.Add("&", 38); SpecChars.Add("∧", 8743); SpecChars.Add("∠", 8736);
SpecChars.Add("Å", 197); SpecChars.Add("å", 229); SpecChars.Add("≈", 8776);
SpecChars.Add("Ã", 195); SpecChars.Add("ã", 227); SpecChars.Add("Ä", 196);
SpecChars.Add("ä", 228); SpecChars.Add("„", 8222); SpecChars.Add("Β", 914);
SpecChars.Add("β", 946); SpecChars.Add("¦", 166); SpecChars.Add("•", 8226);
SpecChars.Add("∩", 8745); SpecChars.Add("Ç", 199); SpecChars.Add("ç", 231);
SpecChars.Add("¸", 184); SpecChars.Add("¢", 162); SpecChars.Add("Χ", 935);
SpecChars.Add("χ", 967); SpecChars.Add("ˆ", 710); SpecChars.Add("♣", 9827);
SpecChars.Add("≅", 8773); SpecChars.Add("©", 169); SpecChars.Add("↵", 8629);
SpecChars.Add("∪", 8746); SpecChars.Add("¤", 164); SpecChars.Add("†", 8224);
SpecChars.Add("‡", 8225); SpecChars.Add("↓", 8595); SpecChars.Add("⇓", 8659);
SpecChars.Add("°", 176); SpecChars.Add("Δ", 916); SpecChars.Add("δ", 948);
SpecChars.Add("♦", 9830); SpecChars.Add("÷", 247); SpecChars.Add("É", 201);
SpecChars.Add("é", 233); SpecChars.Add("Ê", 202); SpecChars.Add("ê", 234);
SpecChars.Add("È", 200); SpecChars.Add("è", 232); SpecChars.Add("&emdash;", 8212);
SpecChars.Add("∅", 8709); SpecChars.Add(" ", 8195); SpecChars.Add("&endash;", 8211);
SpecChars.Add(" ", 8194); SpecChars.Add("Ε", 917); SpecChars.Add("ε", 949);
SpecChars.Add("≡", 8801); SpecChars.Add("Η", 919); SpecChars.Add("η", 951);
SpecChars.Add("Ð", 208); SpecChars.Add("ð", 240); SpecChars.Add("Ë", 203);
SpecChars.Add("ë", 235); SpecChars.Add("€", 8364); SpecChars.Add("∃", 8707);
SpecChars.Add("ƒ", 402); SpecChars.Add("∀", 8704); SpecChars.Add("½", 189);
SpecChars.Add("¼", 188); SpecChars.Add("¾", 190); SpecChars.Add("⁄", 8260);
SpecChars.Add("Γ", 915); SpecChars.Add("γ", 947); SpecChars.Add("≥", 8805);
SpecChars.Add(">", 62); SpecChars.Add("↔", 8596); SpecChars.Add("⇔", 8660);
SpecChars.Add("♥", 9829); SpecChars.Add("…", 8230); SpecChars.Add("Í", 205);
SpecChars.Add("í", 237); SpecChars.Add("Î", 206); SpecChars.Add("î", 238);
SpecChars.Add("¡", 161); SpecChars.Add("Ì", 204); SpecChars.Add("ì", 236);
SpecChars.Add("ℑ", 8465); SpecChars.Add("∞", 8734); SpecChars.Add("∫", 8747);
SpecChars.Add("Ι", 921); SpecChars.Add("ι", 953); SpecChars.Add("¿", 191);
SpecChars.Add("∈", 8712); SpecChars.Add("Ï", 207); SpecChars.Add("ï", 239);
SpecChars.Add("Κ", 922); SpecChars.Add("κ", 954); SpecChars.Add("Λ", 923);
SpecChars.Add("λ", 955); SpecChars.Add("⟨", 9001); SpecChars.Add("«", 171);
SpecChars.Add("←", 8592); SpecChars.Add("⇐", 8656); SpecChars.Add("⌈", 8968);
SpecChars.Add("“", 8220); SpecChars.Add("≤", 8804); SpecChars.Add("⌊", 8970);
SpecChars.Add("∗", 8727); SpecChars.Add("◊", 9674); SpecChars.Add("‎", 8206);
SpecChars.Add("‹", 8249); SpecChars.Add("‘", 8216); SpecChars.Add("<", 60);
SpecChars.Add("¯", 175); SpecChars.Add("—", 8212); SpecChars.Add("µ", 181);
SpecChars.Add("·", 183); SpecChars.Add("−", 8722); SpecChars.Add("Μ", 924);
SpecChars.Add("μ", 956); SpecChars.Add("∇", 8711); SpecChars.Add(" ", 32);
SpecChars.Add("–", 8211); SpecChars.Add("≠", 8800); SpecChars.Add("∋", 8715);
SpecChars.Add("¬", 172); SpecChars.Add("∉", 8713); SpecChars.Add("⊄", 8836);
SpecChars.Add("Ñ", 209); SpecChars.Add("ñ", 241); SpecChars.Add("Ν", 925);
SpecChars.Add("ν", 957); SpecChars.Add("Ó", 211); SpecChars.Add("ó", 243);
SpecChars.Add("Ô", 212); SpecChars.Add("ô", 244); SpecChars.Add("Œ", 338);
SpecChars.Add("œ", 339); SpecChars.Add("Ò", 210); SpecChars.Add("ò", 242);
SpecChars.Add("‾", 8254); SpecChars.Add("Ω", 937); SpecChars.Add("ω", 969);
SpecChars.Add("Ο", 927); SpecChars.Add("ο", 959); SpecChars.Add("⊕", 8853);
SpecChars.Add("∨", 8744); SpecChars.Add("ª", 170); SpecChars.Add("º", 186);
SpecChars.Add("Ø", 216); SpecChars.Add("ø", 248); SpecChars.Add("Õ", 213);
SpecChars.Add("õ", 245); SpecChars.Add("⊗", 8855); SpecChars.Add("Ö", 214);
SpecChars.Add("ö", 246); SpecChars.Add("¶", 182); SpecChars.Add("∂", 8706);
SpecChars.Add("‰", 8240); SpecChars.Add("⊥", 8869); SpecChars.Add("Φ", 934);
SpecChars.Add("φ", 966); SpecChars.Add("Π", 928); SpecChars.Add("π", 960);
SpecChars.Add("ϖ", 982); SpecChars.Add("±", 177); SpecChars.Add("£", 163);
SpecChars.Add("′", 8242); SpecChars.Add("″", 8243); SpecChars.Add("∏", 8719);
SpecChars.Add("∝", 8733); SpecChars.Add("Ψ", 936); SpecChars.Add("ψ", 968);
SpecChars.Add(""", 34); SpecChars.Add("√", 8730); SpecChars.Add("⟩", 9002);
SpecChars.Add("»", 187); SpecChars.Add("→", 8594); SpecChars.Add("⇒", 8658);
SpecChars.Add("⌉", 8969); SpecChars.Add("”", 8221); SpecChars.Add("ℜ", 8476);
SpecChars.Add("®", 174); SpecChars.Add("⌋", 8971); SpecChars.Add("Ρ", 929);
SpecChars.Add("ρ", 961); SpecChars.Add("‏", 8207); SpecChars.Add("›", 8250);
SpecChars.Add("’", 8217); SpecChars.Add("‚", 8218); SpecChars.Add("Š", 352);
SpecChars.Add("š", 353); SpecChars.Add("⋅", 8901); SpecChars.Add("§", 167);
SpecChars.Add("­", 173); SpecChars.Add("Σ", 931); SpecChars.Add("σ", 963);
SpecChars.Add("ς", 962); SpecChars.Add("∼", 8764); SpecChars.Add("♠", 9824);
SpecChars.Add("⊂", 8834); SpecChars.Add("⊆", 8838); SpecChars.Add("∑", 8721);
SpecChars.Add("⊃", 8835); SpecChars.Add("¹", 185); SpecChars.Add("²", 178);
SpecChars.Add("³", 179); SpecChars.Add("⊇", 8839); SpecChars.Add("ß", 223);
SpecChars.Add("Τ", 932); SpecChars.Add("τ", 964); SpecChars.Add("∴", 8756);
SpecChars.Add("Θ", 920); SpecChars.Add("θ", 952); SpecChars.Add("ϑ", 977);
SpecChars.Add(" ", 8201); SpecChars.Add("Þ", 222); SpecChars.Add("þ", 254);
SpecChars.Add("˜", 732); SpecChars.Add("×", 215); SpecChars.Add("™", 8482);
SpecChars.Add("Ú", 218); SpecChars.Add("ú", 250); SpecChars.Add("↑", 8593);
SpecChars.Add("⇑", 8657); SpecChars.Add("Û", 219); SpecChars.Add("û", 251);
SpecChars.Add("Ù", 217); SpecChars.Add("ù", 249); SpecChars.Add("¨", 168);
SpecChars.Add("ϒ", 978); SpecChars.Add("Υ", 933); SpecChars.Add("υ", 965);
SpecChars.Add("Ü", 220); SpecChars.Add("ü", 252); SpecChars.Add("℘", 8472);
SpecChars.Add("Ξ", 926); SpecChars.Add("ξ", 958); SpecChars.Add("Ý", 221);
SpecChars.Add("ý", 253); SpecChars.Add("¥", 165); SpecChars.Add("ÿ", 255);
SpecChars.Add("Ÿ", 376); SpecChars.Add("Ζ", 918); SpecChars.Add("ζ", 950);
SpecChars.Add("‍", 8205); SpecChars.Add("‌", 8204);
HtmlToTextReplaceElement[] ReplaceStrings = new HtmlToTextReplaceElement[]
{
new HtmlToTextReplaceElement(@"\s", " ", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@" {2,}", " ", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"<! *--.*?-- *>", " ", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"< *head( *>| [^>]*>).*< */ *head *>", string.Empty, HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"< *script( *>| [^>]*>).*?< */ *script *>", string.Empty, HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"< *style( *>| [^>]*>).*?< */ *style *>", string.Empty, HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"< *td[^>]*>","\t", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"< *(br|li) */{0,1} *>", "\r", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"< *(div|tr|p)( *>| [^>]*>)", "\r\r", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"<[^>]*>", string.Empty, HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@" ", " ", HtmlToTextReplaceType.String),
new HtmlToTextReplaceElement(@"[ \t]+\r", "\r", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"\t +\t", "\t\t", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"\r +\t", "\r\t", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"\r\t{2,}", "\r\t", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"\r{3,}", "\r\r", HtmlToTextReplaceType.RegEx),
new HtmlToTextReplaceElement(@"\t{4,}", "\t\t\t\t", HtmlToTextReplaceType.RegEx)
};
string result = source;
for (int i = 0; i < ReplaceStrings.Length; i++)
{
switch (ReplaceStrings[i].Type)
{
case HtmlToTextReplaceType.String:
result = result.Replace(ReplaceStrings[i].Pattern, ReplaceStrings[i].Substitute);
break;
case HtmlToTextReplaceType.RegEx:
result = Regex.Replace(result, ReplaceStrings[i].Pattern, ReplaceStrings[i].Substitute,RegexOptions.IgnoreCase);
break;
}
}
AmpCodes = Regex.Matches(result, @"&#\d{1,5};",RegexOptions.IgnoreCase);
for (int i = AmpCodes.Count - 1; i >= 0; i--)
{
AmpStr = AmpCodes[i].Value;
result = result.Substring(0, AmpCodes[i].Index) +
Convert.ToChar(Int32.Parse(AmpStr.Substring(2, AmpStr.Length - 3))) +
result.Substring(AmpCodes[i].Index + AmpCodes[i].Length);
}
AmpCodes = Regex.Matches(result, @"&#x[0-9a-f]{1,4};",RegexOptions.IgnoreCase);
for (int i = AmpCodes.Count - 1; i >= 0; i--)
{
AmpStr = AmpCodes[i].Value;
result = result.Substring(0, AmpCodes[i].Index) +
Convert.ToChar(Int32.Parse(AmpStr.Substring(3, AmpStr.Length - 4),NumberStyles.AllowHexSpecifier)) +
result.Substring(AmpCodes[i].Index + AmpCodes[i].Length);
}
AmpCodes = Regex.Matches(result, @"&\w+;", RegexOptions.IgnoreCase);
for (int i = AmpCodes.Count - 1; i >= 0; i--)
{
if (SpecChars.ContainsKey(AmpCodes[i].Value))
{
result = result.Substring(0, AmpCodes[i].Index) +
Convert.ToChar(SpecChars[AmpCodes[i].Value]) +
result.Substring(AmpCodes[i].Index + AmpCodes[i].Length);
}
}
result = Regex.Replace(result, @"&[^;]*;", string.Empty,RegexOptions.IgnoreCase);
return result;
}
}
public enum HtmlToTextReplaceType { String, RegEx }
public class HtmlToTextReplaceElement
{
public HtmlToTextReplaceElement() { }
public HtmlToTextReplaceElement(string Pattern, string Substitute, HtmlToTextReplaceType Type)
{
this.Pattern = Pattern;
this.Substitute = Substitute;
this.Type = Type;
}
public string Pattern;
public string Substitute;
public HtmlToTextReplaceType Type;
}
in addition to the standard ones you have to use this two using in the cs file header:
using System.Text.RegularExpressions;
using System.Globalization;
[Update] Unfortunately two & nbsp ; text in the code converted back to space (due to some problems with the site's engine):
SpecChars.Add(" ", 32);
new HtmlToTextReplaceElement(@" ", " ", HtmlToTextReplaceType.String),
change them back otherwise it will not work correctly
|