我目前正在为 HTML tidy 使用 RubyTidy Ruby 绑定,以确保我收到的 HTML 格式正确。目前,这个库是唯一阻碍我在 Ruby 1.9 上获得 Rails 应用程序的东西。是否有任何替代库可以整理 Ruby 1.9 上的 HTML 块?
问问题
6997 次
4 回答
7
http://github.com/libc/tidy_ffi/blob/master/README.rdoc适用于 ruby 1.9(最新版本)
如果您在 Windows 上工作,则需要设置 library_path 例如
require 'tidy_ffi'
TidyFFI.library_path = 'lib\\tidy\\bin\\tidy.dll'
tidy = TidyFFI::Tidy.new('test')
puts tidy.clean
(它使用与 tidy 相同的 dll) 上面的链接为您提供了更多使用示例。
于 2010-04-21T18:36:03.320 回答
7
我正在使用Nokogiri修复无效的 html:
Nokogiri::HTML::DocumentFragment.parse(html).to_html
于 2010-11-29T08:42:30.410 回答
3
这是一个很好的例子,说明如何使用 tidy 让你的 html 看起来更好:
require 'tidy'
Tidy.path = '/opt/local/lib/libtidy.dylib' # or where ever your tidylib resides
nice_html = ""
Tidy.open(:show_warnings=>true) do |tidy|
tidy.options.output_xhtml = true
tidy.options.wrap = 0
tidy.options.indent = 'auto'
tidy.options.indent_attributes = false
tidy.options.indent_spaces = 4
tidy.options.vertical_space = false
tidy.options.char_encoding = 'utf8'
nice_html = tidy.clean(my_nasty_html_string)
end
# remove excess newlines
nice_html = nice_html.strip.gsub(/\n+/, "\n")
puts nice_html
有关更整洁的选项,请查看手册页。
于 2010-04-16T08:08:25.703 回答
1
目前,这个库是唯一阻碍我在 Ruby 1.9 上获得 Rails 应用程序的东西。
注意,Ruby Tidy 绑定有一些严重的内存泄漏。它目前无法在长时间运行的进程中使用。(为了记录,我正在使用http://github.com/ak47/tidy)
我只需要从生产 Rails 2.3 应用程序中删除它,因为它的泄漏速度约为 1MB/分钟。
于 2010-03-11T09:49:46.777 回答