在使用Ruby调试器几个小时后,我终于了解到,在将一些格式错误的HTML页面提供给Hpricot之前,我需要清理这些页面。到目前为止,我找到的最好的解决方案是Tidy Ruby interface。
Tidy在命令行中工作得很好,而且它的界面也很好用。但是,它需要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?网络搜索显示it wasn't available last year。
或者,有人可以提出其他方法来清理JRuby中格式错误的超文本标记语言吗?
更新
按照Markus的建议,我现在使用Tidy via 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发布于 2009-03-03 21:48:18
您可以在JRuby中的命令行中使用它,并使用%x{...}或反号。您可能还想考虑使用popen (并通过它传输数据)。
也许不是很优雅,但比起尝试处理不受支持的库,它更有可能让你以最小的麻烦开始工作。
https://stackoverflow.com/questions/608306
复制相似问题