Are there JavaScript or Ruby versions of “HTML tidy”? [closed]

无人久伴 提交于 2019-12-02 03:06:16

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>

Had you checked this before? http://tidy.rubyforge.org/

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

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?

If you just want a beautifier use Pretty Diff.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!