OS 固有ではない (各ホストでコンパイルする必要がある) HTML tidy (http://tidy.sourceforge.net/) に似たライブラリは存在しますか? 基本的に、ユーザーから送信された HTML を検証/クリーンアップしたいだけです。
<p>hello</p></p><br>
なるべき
<p>hello</p>
<br/>
JavaScriptまたはRubyの何かが私にとってはうまくいくでしょう。ありがとう!
OS 固有ではない (各ホストでコンパイルする必要がある) HTML tidy (http://tidy.sourceforge.net/) に似たライブラリは存在しますか? 基本的に、ユーザーから送信された HTML を検証/クリーンアップしたいだけです。
<p>hello</p></p><br>
なるべき
<p>hello</p>
<br/>
JavaScriptまたはRubyの何かが私にとってはうまくいくでしょう。ありがとう!
前にこれをチェックしましたか?http://tidy.rubyforge.org/
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>
html-tidy は javascript にコンパイルされています (emscripten を使用)。
勇気があれば、必要なオプションを使用して、自分で JavaScript にコンパイルできます。https://github.com/lovasoa/tidy-html5を参照してください
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.
Would the W3 Validator work for you?
Or are you wanting something to fix the errors?
美化だけが必要な場合は、Pretty Diff を使用してください。