Tidy.NETでHTMLを清書する
下記のページによれば、汚いHTMLをXHTMLに清書できる(はず)。
- http://bytes.com/groups/net-c/576661-tidy-net
- http://www.developmentnow.com/g/36_2006_12_0_0_864791/Tidy-Net.htm
- http://refactormycode.com/codes/333-sanitize-html
- http://blog.levo.us/index.php/2008/04/18/quickly-fixing-crappy-invalid-html-using-c/
こんな感じ↓
string cleanupHtml(string text) { Tidy doc = new Tidy(); TidyMessageCollection messages = new TidyMessageCollection(); MemoryStream input = new MemoryStream(); MemoryStream output = new MemoryStream(); byte[] array = Encoding.UTF8.GetBytes(text); input.Write(array, 0, array.Length); input.Position = 0; doc.Options.DocType = DocType.Strict; doc.Options.Xhtml = true; doc.Options.LogicalEmphasis = true; doc.Options.MakeClean = false; doc.Options.SmartIndent = false; doc.Options.IndentContent = false; doc.Options.TidyMark = false; doc.Options.DropFontTags = false; doc.Options.QuoteAmpersand = true; doc.Options.DropEmptyParas = true; doc.Options.CharEncoding = CharEncoding.UTF8; doc.Parse(input, output, messages); string result = Encoding.UTF8.GetString(output.ToArray()); return result; }
日本語通るかやってみたが、ダメだった。orz
ソースコードをちらっと見てみたけど大丈夫そうなんだけどなー。
↑ウソ:これが原因。
しょうがないので、こっちでやります。