Ruby 1.9 中的 HTML 整理/清理

发布于 2024-08-03 07:40:28 字数 132 浏览 9 评论 0原文

我目前正在使用 RubyTidy Ruby 绑定来进行 HTML 整洁,以确保我收到的 HTML 格式良好。目前,这个库是唯一阻碍我在 Ruby 1.9 上获取 Rails 应用程序的因素。是否有其他库可以整理 Ruby 1.9 上的 HTML 块?

I'm currently using the RubyTidy Ruby bindings for HTML tidy to make sure HTML I receive is well-formed. Currently this library is the only thing holding me back from getting a Rails application on Ruby 1.9. Are there any alternative libraries out there that will tidy up chunks of HTML on Ruby 1.9?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌能量女王 2024-08-10 07:40:28

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) 上面的链接为您提供了更多用法示例。

http://github.com/libc/tidy_ffi/blob/master/README.rdoc works with ruby 1.9 (latest version)

If you are working on windows, you need to set the library_path eg

    require 'tidy_ffi'
    TidyFFI.library_path = 'lib\\tidy\\bin\\tidy.dll'
    tidy = TidyFFI::Tidy.new('test')
    puts tidy.clean

(It uses the same dll as tidy) The above links gives you more example of the usage.

¢蛋碎的人ぎ生 2024-08-10 07:40:28

我正在使用 Nokogiri 来修复无效的 html:

  Nokogiri::HTML::DocumentFragment.parse(html).to_html

I am using Nokogiri to fix invalid html:

  Nokogiri::HTML::DocumentFragment.parse(html).to_html
一抹淡然 2024-08-10 07:40:28

下面是一个很好的例子,说明如何使用 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

有关更多 tidy 选项,请查看 手册页

Here is a nice example of how to make your html look better using tidy:

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

For more tidy options, check out the man page.

迷你仙 2024-08-10 07:40:28

目前这个库是唯一的
阻碍我获得的东西
Ruby 1.9 上的 Rails 应用程序。

请注意,Ruby Tidy 绑定存在一些严重的内存泄漏问题。目前它在长时间运行的进程中不可用。 (郑重声明,我正在使用 http://github.com/ak47/tidy

)只需将其从生产 Rails 2.3 应用程序中删除,因为它的泄漏速度约为 1MB/分钟。

Currently this library is the only
thing holding me back from getting a
Rails application on Ruby 1.9.

Watch out, the Ruby Tidy bindings have some nasty memory leaks. It's currently unusable in long running processes. (for the record, I'm using http://github.com/ak47/tidy)

I just had to remove it from a production Rails 2.3 application because it was leaking about 1MB/min.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文