问题
Does there exist a library similar to HTML tidy (http://tidy.sourceforge.net/) that is not OS specific (needs to be compiled on each host). Basically i just want to validate/clean the HTML sent to me by the user.
<p>hello</p></p><br>
should become
<p>hello</p>
<br/>
Something in javascript or ruby would work for me. Thanks!
回答1:
In Ruby you can parse the HTML in Nokogiri, which will let you check for errors, then have it output the HTML, which will clean up missing closing tags and such. Notice in the following HTML that the title and p tags are not closed correctly, but Nokogiri adds the ending tags.
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>
Alternately you can open a connection to /usr/bin/tidy
and tell it to do the dirty work:
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>
回答2:
Had you checked this before? http://tidy.rubyforge.org/
回答3:
html-tidy has been compiled to javascript (using emscripten).
See the demo and download tidy.js.
If you are brave enough, you can compile it to javascript yourself, with the options you want. See https://github.com/lovasoa/tidy-html5
回答4:
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.
回答5:
Would the W3 Validator work for you?
Or are you wanting something to fix the errors?
回答6:
If you just want a beautifier use Pretty Diff.
http://prettydiff.com/?m=beautify&html
来源:https://stackoverflow.com/questions/4240107/are-there-javascript-or-ruby-versions-of-html-tidy