在使用Ruby Debugger花了几个小时之后,我终于了解到我需要清理一些格式错误的HTML页面才能将它们提供给Hpricot。到目前为止,我找到的最佳解决方案是 Tidy Ruby界面

Tidy 在命令行中运行良好,并且Ruby界面也可以正常工作。但是,它需要 dl / import ,无法在JRuby中加载:

$ jirb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'tidy'
LoadError: no such file to load -- dl/import

此库可用于JRuby吗?网络搜索显示去年无法使用

或者,有人可以提出其他方法来清理JRuby中格式错误的HTML吗?

更新

根据Markus的建议,我现在通过popen而不是libtidy使用Tidy。我发布了通过整理来管理文档数据的代码,以备将来参考。希望这是强大和便携的。

def clean(data)
    cleaned = nil
    tidy = IO.popen('tidy -f "log/tidy.log" --force-output yes -wrap 0 -utf8', 'w+')
    begin
        tidy.write(data)
        tidy.close_write
        cleaned = tidy.read
        tidy.close_read
    rescue Errno::EPIPE
        $stderr.print "Running 'tidy' failed: " + $!
        tidy.close
    end        
    return cleaned if cleaned and cleaned != ""
    return data
end
有帮助吗?

解决方案

您可以在 JRuby 中的命令行中使用%x {.. 。} 或反引号。您可能还需要考虑 popen (并通过它管道)。

也许并不优雅,但更有可能让你以最小的麻烦而不是试图搞乱不受支持的库。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top