1

OS 固有ではない (各ホストでコンパイルする必要がある) HTML tidy (http://tidy.sourceforge.net/) に似たライブラリは存在しますか? 基本的に、ユーザーから送信された HTML を検証/クリーンアップしたいだけです。

<p>hello</p></p><br>

なるべき

<p>hello</p>
<br/>

JavaScriptまたはRubyの何かが私にとってはうまくいくでしょう。ありがとう!

4

6 に答える 6

1

前にこれをチェックしましたか?http://tidy.rubyforge.org/

于 2010-11-22T00:06:30.087 に答える
1

Ruby では、Nokogiri で HTML を解析して、エラーをチェックし、HTML を出力して、欠落している終了タグなどをクリーンアップできます。次の HTML では、title タグと p タグが正しく閉じられていませんが、Nokogiri によって終了タグが追加されていることに注意してください。

require 'nokogiri'

html = '<html><head><title>the title</head><body><p>a paragraph</body></html>'
doc = Nokogiri::HTML(html)
puts "Errors found" if (doc.errors.any?)
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >> <head>
# >> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
# >> <title>the title</title>
# >> </head>
# >> <body><p>a paragraph</p></body>
# >> </html>

別の方法として、接続を開いて/usr/bin/tidy、汚い仕事をするように指示することもできます:

require 'open3'

html = '<html><head><title>the title</head><body><p>a paragraph</body></html>'

stdin, stdout, stderr = Open3.popen3('/usr/bin/tidy -qi')
stdin.puts html
stdin.close
puts stdout.read
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
# >> 
# >> <html>
# >> <head>
# >>   <meta name="generator" content=
# >>   "HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 15.3.6), see www.w3.org">
# >> 
# >>   <title>the title</title>
# >> </head>
# >> 
# >> <body>
# >>   <p>a paragraph</p>
# >> </body>
# >> </html>
于 2010-11-22T00:08:11.090 に答える
1

html-tidy は javascript にコンパイルされています (emscripten を使用)。

デモを見て、 tidy.jsをダウンロードしてください。

勇気があれば、必要なオプションを使用して、自分で JavaScript にコンパイルできます。https://github.com/lovasoa/tidy-html5を参照してください

于 2014-12-08T21:46:21.407 に答える
0

There is a java port JTidy but no other ports that I know of, there may be some way you call HTML tidy from Ruby that works for you, prahaps call the html tidy app on the command line from your ruby webapp.

于 2010-11-21T20:25:32.960 に答える
0

Would the W3 Validator work for you?

Or are you wanting something to fix the errors?

于 2010-11-21T20:25:41.763 に答える
0

美化だけが必要な場合は、Pretty Diff を使用してください。

http://prettydiff.com/?m=beautify&html

于 2011-12-10T13:05:13.150 に答える