如何在 Rails 3.0.x 上使用可读性 gem?

发布于 2024-10-07 17:00:55 字数 3131 浏览 0 评论 0原文

我将在我的 Rails 3 应用程序上使用 Readability gem。我无法按照 github 页面上的建议使用它 http://github.com/sspinc/readability尽管它在 ruby​​ 控制台上运行良好,但在应用程序内部。

在 ruby​​ 控制台上:

> require 'open-uri'
 => true 
> require 'readability'
 => true
> doc = Nokogiri::HTML(open('http://google.com'))
 => #<Nokogiri::HTML::Document:0x81345324 name="document"
> doc.to_readable
 => #<Nokogiri::XML::Element:0x813299a8 name="html" 
# (expected result without any error)

在 Rails 控制台上:

> doc = Nokogiri::HTML(open('http://google.com'))
 => #<Nokogiri::HTML::Document:0x81c2baec name="document"
# (and so on.. expected result)
> readable = doc.to_readable
SyntaxError: /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/readability-0.1.5/lib/readability/js/readability.js:2: syntax error, unexpected '='
    if(typeof console !== 'undefined') {
                         ^
/Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/readability-0.1.5/lib/readability/js/readability.js:2: syntax error, unexpected '{', expecting kTHEN or ':' or '\n' or ';'
/Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/readability-0.1.5/lib/readability/js/readability.js:5: syntax error, unexpected '}', expecting $end
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/harmony-0.5.6/lib/harmony/page.rb:76:in `load'

我验证这不是 javascript 语法错误。从主动支持错误来看,它未能加载依赖项,我怀疑 Rails 未能加载可读性使用的所需 gem(如 Harmony、johnson 和 envjs)来处理 Ruby 中的 javascript 和 DOM。

所以我的猜测是 Gemfile 中可能有问题?这是我当前的 Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.0.3'
gem 'devise', '~> 1.1.5'
gem 'haml', '3.0.24'
gem 'simple_form'
gem 'cancan'
gem 'will_paginate', '~> 3.0.beta'
gem 'feedzirra', :require => [ 'feedzirra' ]
gem 'loofah', '1.0.0.beta.1'
gem 'compass', '~> 0.10.4'
gem 'meta_where', '~> 0.9'
gem 'populator'
gem 'simple-navigation'
gem 'readability'

我尝试在可读性中添加 :require 选项来指定 Harmony、Johnson 和 envjs;并重新运行“捆绑安装”,没有任何错误,但我仍然有相同的错误。

我还检查了 config/application.rb 以要求 Gemfile 中的所有 gem。那么

Bundler.require(:default, Rails.env) if defined?(Bundler)

,回到问题,如何在 Rails 3.0.x 上使用可读性?我错过了什么吗?如果这很重要的话,我使用的是 MacOS 10.6.5。

谢谢

I'm going to use readability gem on my rails 3 app. I can't use it as advised on the github page http://github.com/sspinc/readability inside the app although it works fine on ruby console.

On the ruby console:

> require 'open-uri'
 => true 
> require 'readability'
 => true
> doc = Nokogiri::HTML(open('http://google.com'))
 => #<Nokogiri::HTML::Document:0x81345324 name="document"
> doc.to_readable
 => #<Nokogiri::XML::Element:0x813299a8 name="html" 
# (expected result without any error)

On the rails console:

> doc = Nokogiri::HTML(open('http://google.com'))
 => #<Nokogiri::HTML::Document:0x81c2baec name="document"
# (and so on.. expected result)
> readable = doc.to_readable
SyntaxError: /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/readability-0.1.5/lib/readability/js/readability.js:2: syntax error, unexpected '='
    if(typeof console !== 'undefined') {
                         ^
/Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/readability-0.1.5/lib/readability/js/readability.js:2: syntax error, unexpected '{', expecting kTHEN or ':' or '\n' or ';'
/Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/readability-0.1.5/lib/readability/js/readability.js:5: syntax error, unexpected '}', expecting $end
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
        from /Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/harmony-0.5.6/lib/harmony/page.rb:76:in `load'

I verified that it is not a javascript syntax error. Judging by the active support error, it failed loading the dependencies, I suspect that Rails has failed to load the required gems that readability use (like harmony, johnson and envjs) to handle javascript and DOM in Ruby.

So my guess was may be somthing wrong in the Gemfile? Here's my current Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.0.3'
gem 'devise', '~> 1.1.5'
gem 'haml', '3.0.24'
gem 'simple_form'
gem 'cancan'
gem 'will_paginate', '~> 3.0.beta'
gem 'feedzirra', :require => [ 'feedzirra' ]
gem 'loofah', '1.0.0.beta.1'
gem 'compass', '~> 0.10.4'
gem 'meta_where', '~> 0.9'
gem 'populator'
gem 'simple-navigation'
gem 'readability'

I have tried adding :require option in readability to specify harmony, johnson and envjs; and reran 'bundle install' without any error, but I still have the same error.

I have also checked the config/application.rb to require all the gems in the Gemfile. with this line

Bundler.require(:default, Rails.env) if defined?(Bundler)

So, back to the question, how can I use readability on Rails 3.0.x? Did I miss something? I'm on MacOS 10.6.5 if that matters.

Thank you

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

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

发布评论

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

评论(1

淡水深流 2024-10-14 17:00:55

在我的 Mac(Rails 3.x、Ruby 1.9.2)上,我在加载 johnson gem 时遇到了麻烦,它是一个依赖项。但是,ruby-readability gem 可以工作。

On my Mac (Rails 3.x, Ruby 1.9.2 I had trouble with it loading the johnson gem which is a dependency . However, ruby-readability gem works.

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