/Source/Pronto/HtmlHelper.cs
C# | 292 lines | 286 code | 4 blank | 2 comment | 11 complexity | ea72bd64852c61fd7c4f28ae3b1e951b MD5 | raw file
Possible License(s): LGPL-2.1
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
-
- namespace Pronto
- {
- public static class HtmlHelper
- {
- public static IEnumerable<XObject> ParseXObjects(this string content)
- {
- return XElement.Parse("<body>" + ConvertHtmlEntitiesToNumbers(content) + "</body>").Nodes().Cast<XObject>();
- }
-
- static string ConvertHtmlEntitiesToNumbers(string input)
- {
- var sb = new StringBuilder(input);
- for (int i = 0; i < sb.Length; i++)
- {
- if (sb[i] == '&')
- {
- i = ReplaceEntity(sb, i);
- }
- }
- return sb.ToString();
- }
-
- static int ReplaceEntity(StringBuilder sb, int i)
- {
- int start = i;
- while (++i < sb.Length && sb[i] != ';') ;
- if (i == sb.Length) throw new ArgumentException("Could not find closing ';' for '&' at position " + start + ".");
- int length = i - start + 1;
- char[] chars = new char[length];
- sb.CopyTo(start, chars, 0, length);
- if (chars[1] != '#') // ignore numeric entities
- {
- var entity = new string(chars);
- string numericCode;
- if (entityMap.TryGetValue(entity, out numericCode))
- {
- sb.Remove(start, length);
- sb.Insert(start, numericCode);
- }
- }
- return i;
- }
-
- // XDocument won't like HTML entities.
- // This map converts to numeric character codes.
- static readonly Dictionary<string, string> entityMap = new Dictionary<string, string>
- {
- { " ", " "},
- { "¡", "¡"},
- { "¢", "¢"},
- { "£", "£"},
- { "¤", "¤"},
- { "¥", "¥"},
- { "¦", "¦"},
- { "§", "§"},
- { "¨", "¨"},
- { "©", "©"},
- { "ª", "ª"},
- { "«", "«"},
- { "¬", "¬"},
- { "­", "­"},
- { "®", "®"},
- { "¯", "¯"},
- { "°", "°"},
- { "±", "±"},
- { "²", "²"},
- { "³", "³"},
- { "´", "´"},
- { "µ", "µ"},
- { "¶", "¶"},
- { "·", "·"},
- { "¸", "¸"},
- { "¹", "¹"},
- { "º", "º"},
- { "»", "»"},
- { "¼", "¼"},
- { "½", "½"},
- { "¾", "¾"},
- { "¿", "¿"},
- { "×", "×"},
- { "÷", "÷"},
- { "À", "À"},
- { "Á", "Á"},
- { "Â", "Â"},
- { "Ã", "Ã"},
- { "Ä", "Ä"},
- { "Å", "Å"},
- { "Æ", "Æ"},
- { "Ç", "Ç"},
- { "È", "È"},
- { "É", "É"},
- { "Ê", "Ê"},
- { "Ë", "Ë"},
- { "Ì", "Ì"},
- { "Í", "Í"},
- { "Î", "Î"},
- { "Ï", "Ï"},
- { "Ð", "Ð"},
- { "Ñ", "Ñ"},
- { "Ò", "Ò"},
- { "Ó", "Ó"},
- { "Ô", "Ô"},
- { "Õ", "Õ"},
- { "Ö", "Ö"},
- { "Ø", "Ø"},
- { "Ù", "Ù"},
- { "Ú", "Ú"},
- { "Û", "Û"},
- { "Ü", "Ü"},
- { "Ý", "Ý"},
- { "Þ", "Þ"},
- { "ß", "ß"},
- { "à", "à"},
- { "á", "á"},
- { "â", "â"},
- { "ã", "ã"},
- { "ä", "ä"},
- { "å", "å"},
- { "æ", "æ"},
- { "ç", "ç"},
- { "è", "è"},
- { "é", "é"},
- { "ê", "ê"},
- { "ë", "ë"},
- { "ì", "ì"},
- { "í", "í"},
- { "î", "î"},
- { "ï", "ï"},
- { "ð", "ð"},
- { "ñ", "ñ"},
- { "ò", "ò"},
- { "ó", "ó"},
- { "ô", "ô"},
- { "õ", "õ"},
- { "ö", "ö"},
- { "ø", "ø"},
- { "ù", "ù"},
- { "ú", "ú"},
- { "û", "û"},
- { "ü", "ü"},
- { "ý", "ý"},
- { "þ", "þ"},
- { "ÿ", "ÿ"},
- { "Œ", "Œ" },
- { "œ", "œ" },
- { "Š", "Š" },
- { "š", "š" },
- { "Ÿ", "Ÿ" },
- { "ƒ", "ƒ" },
- { "ˆ", "ˆ" },
- { "˜", "˜" },
- { " ", " " },
- { " ", " " },
- { " ", " " },
- { "‌", "‌" },
- { "‍", "‍" },
- { "‎", "‎" },
- { "‏", "‏" },
- { "–", "–" },
- { "—", "—" },
- { "‘", "‘" },
- { "’", "’" },
- { "‚", "‚" },
- { "“", "“" },
- { "”", "”" },
- { "„", "„" },
- { "†", "†" },
- { "‡", "‡" },
- { "•", "•" },
- { "…", "…" },
- { "‰", "‰" },
- { "′", "′" },
- { "″", "″" },
- { "‹", "‹" },
- { "›", "›" },
- { "‾", "‾" },
- { "€", "€" },
- { "™", "™" },
- { "←", "←" },
- { "↑", "↑" },
- { "→", "→" },
- { "↓", "↓" },
- { "↔", "↔" },
- { "↵", "↵" },
- { "⌈", "⌈" },
- { "⌉", "⌉" },
- { "⌊", "⌊" },
- { "⌋", "⌋" },
- { "◊", "◊" },
- { "♠", "♠" },
- { "♣", "♣" },
- { "♥", "♥" },
- { "♦", "♦" },
- { "Α", "Α" },
- { "Β", "Β" },
- { "Γ", "Γ" },
- { "Δ", "Δ" },
- { "Ε", "Ε" },
- { "Ζ", "Ζ" },
- { "Η", "Η" },
- { "Θ", "Θ" },
- { "Ι", "Ι" },
- { "Κ", "Κ" },
- { "Λ", "Λ" },
- { "Μ", "Μ" },
- { "Ν", "Ν" },
- { "Ξ", "Ξ" },
- { "Ο", "Ο" },
- { "Π", "Π" },
- { "Ρ", "Ρ" },
- { "Σ", "Σ" },
- { "Τ", "Τ" },
- { "Υ", "Υ" },
- { "Φ", "Φ" },
- { "Χ", "Χ" },
- { "Ψ", "Ψ" },
- { "Ω", "Ω" },
- { "α", "α" },
- { "β", "β" },
- { "γ", "γ" },
- { "δ", "δ" },
- { "ε", "ε" },
- { "ζ", "ζ" },
- { "η", "η" },
- { "θ", "θ" },
- { "ι", "ι" },
- { "κ", "κ" },
- { "λ", "λ" },
- { "μ", "μ" },
- { "ν", "ν" },
- { "ξ", "ξ" },
- { "ο", "ο" },
- { "π", "π" },
- { "ρ", "ρ" },
- { "ς", "ς" },
- { "σ", "σ" },
- { "τ", "τ" },
- { "υ", "υ" },
- { "φ", "φ" },
- { "χ", "χ" },
- { "ψ", "ψ" },
- { "ω", "ω" },
- { "ϑ", "ϑ" },
- { "ϒ", "ϒ" },
- { "ϖ", "ϖ" },
- { "∀", "∀" },
- { "∂", "∂" },
- { "&exists;", "∃" },
- { "∅", "∅" },
- { "∇", "∇" },
- { "∈", "∈" },
- { "∉", "∉" },
- { "∋", "∋" },
- { "∏", "∏" },
- { "∑", "∑" },
- { "−", "−" },
- { "∗", "∗" },
- { "√", "√" },
- { "∝", "∝" },
- { "∞", "∞" },
- { "∠", "∠" },
- { "∧", "∧" },
- { "∨", "∨" },
- { "∩", "∩" },
- { "∪", "∪" },
- { "∫", "∫" },
- { "∴", "∴" },
- { "∼", "∼" },
- { "≅", "≅" },
- { "≈", "≈" },
- { "≠", "≠" },
- { "≡", "≡" },
- { "≤", "≤" },
- { "≥", "≥" },
- { "⊂", "⊂" },
- { "⊃", "⊃" },
- { "⊄", "⊄" },
- { "⊆", "⊆" },
- { "⊇", "⊇" },
- { "⊕", "⊕" },
- { "⊗", "⊗" },
- { "⊥", "⊥" },
- { "⋅", "⋅" }
- };
- }
- }