如何在常规 Ruby 代码(非 Rails)中使用 strip_tags?

发布于 2024-10-05 20:38:54 字数 356 浏览 8 评论 0原文

我需要将 HTML 转换为纯文本。 ActionView 的 SanitizeHelper 中有一个很好的函数可以执行此操作,但我无法理解如何引用它并在简单的 test.rb 文件中使用它。

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

我希望能够调用 strip_tags("lol") => “哈哈”

I need to turn HTML into plain text. There's a nice function that does that in ActionView's SanitizeHelper, but I have trouble understanding how I can reference it and use it in a simple test.rb file.

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

I would like to be able to call strip_tags("<b>lol</b>") => "lol"

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

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

发布评论

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

评论(8

治碍 2024-10-12 20:38:54

这个问题很老了,但我最近也遇到了同样的问题。我找到了一个简单的解决方案:gem sanitize。它很轻,工作正常,如果您需要的话,还有其他选项。

Sanitize.clean("<b>lol</b>") #=> "lol"

The question is quite old, but I had the same problem recently. I found a simple solution: gem sanitize. It's light, works fine and has additional options if you need them.

Sanitize.clean("<b>lol</b>") #=> "lol"
寻找我们的幸福 2024-10-12 20:38:54

ActiveSupport 是唯一支持挑选单个组件的 Rails 框架。其他框架(包括 ActionView)必须全部被 require:

require 'action_view'

请注意,此 require 不一定会加载所有 ActionView。除非线程安全要求自动加载立即发生,否则它只是设置自动加载并需要公共依赖项。这意味着在 require 之后,如果您引用例如 ActionView::Helpers::SanitizeHelper,它将导致 action_view/helpers /sanitize_helper.rb 被要求。

因此,使用 ActionView 完成您想要的任务的正确且受支持的方法如下

require 'action_view'

class Test < Test::Unit::TestCase # or whatever
  include ActionView::Helpers::SanitizeHelper

  def my_test
    assert_equal "lol", strip_tags("<b>lol</b>")
  end
end

:我这个答案主要基于对此问题的讨论

ActiveSupport is the only Rails framework that supports cherry-picking individual components. The other frameworks, including ActionView, must be required en-masse:

require 'action_view'

Note that this require won't necessarily load all of ActionView. Barring situations where thread-safety requires that autoloads happen eagerly, it merely sets up autoloads and requires common dependencies. That means that following the require, if you reference, e.g. ActionView::Helpers::SanitizeHelper, it will cause action_view/helpers /sanitize_helper.rb to be required.

Therefore the correct, supported way to accomplish what you desire using ActionView is the following:

require 'action_view'

class Test < Test::Unit::TestCase # or whatever
  include ActionView::Helpers::SanitizeHelper

  def my_test
    assert_equal "lol", strip_tags("<b>lol</b>")
  end
end

This isn't well-documented; I based this answer primarily off of the discussion on this issue.

瑕疵 2024-10-12 20:38:54

我相信这应该足够了:

"<b>lol</b>".gsub(/<[^>]*>/ui,'') #=> lol

您也可以使用 Nokogiri:

require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML("<b>lol</b>")
doc.text #=> "lol"

您仍然可以通过执行以下操作来使用 Rails:

require 'rubygems'
require 'action_view'

class Foo
  include ActionView::Helpers::SanitizeHelper

  def test
    strip_tags("<b>lol</b>")
  end
end

f = Foo.new
puts f.test #=> lol

I believe this should be enough:

"<b>lol</b>".gsub(/<[^>]*>/ui,'') #=> lol

You can use Nokogiri as well:

require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML("<b>lol</b>")
doc.text #=> "lol"

You still can go with the Rails one by doing something like:

require 'rubygems'
require 'action_view'

class Foo
  include ActionView::Helpers::SanitizeHelper

  def test
    strip_tags("<b>lol</b>")
  end
end

f = Foo.new
puts f.test #=> lol
慈悲佛祖 2024-10-12 20:38:54

如果您不经常使用它,那么您可以使用:

ActionView::Base.full_sanitizer.sanitize(your_html_string)

否则您可以在 test_helper.rb 文件中定义一个方法,如下所示:

def strip_html_tags(string)
    ActionView::Base.full_sanitizer.sanitize(string)
end

然后在 test.rb 文件中,使用如下所示:

strip_html_tags(your_html_string)

If you don't use it very often, then you can use:

ActionView::Base.full_sanitizer.sanitize(your_html_string)

else you can define a method in test_helper.rb file like:

def strip_html_tags(string)
    ActionView::Base.full_sanitizer.sanitize(string)
end

And then in your test.rb file, use this like:

strip_html_tags(your_html_string)
夢归不見 2024-10-12 20:38:54

这个问题很老了,但是您可以在 test.rb 中调用它,如下所示:

ActionController::Base.helpers.strip_tags("<b>lol</b>") => "lol"

The question is quite old, but you can call it in your test.rb like this:

ActionController::Base.helpers.strip_tags("<b>lol</b>") => "lol"
内心荒芜 2024-10-12 20:38:54

通过这个例子:

"<p><i>example</i></p>"

这对我有帮助:

ActionView::Base.full_sanitizer.sanitize(Nokogiri::HTML(example).text)

输出:

example

With this example:

"<p><i>example</i></p>"

This helped me:

ActionView::Base.full_sanitizer.sanitize(Nokogiri::HTML(example).text)

Output:

example
亢潮 2024-10-12 20:38:54

理想情况下,您需要并包含 ActionView::Helpers::SanitizeHelper 但当您这样做时,有几个依赖项不会包含在内。您可以要求他们自己能够使用 strip_tags

require 'erb'
require 'active_support'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/string/encoding'
require 'action_view/helpers/capture_helper'
require 'action_view/helpers/sanitize_helper'

include ActionView::Helpers::SanitizeHelper

strip_tags("<b>lol</b>") # => "lol"

这是假设您安装了 Rails 3 gems。

Ideally you would require and include ActionView::Helpers::SanitizeHelper but there are several dependencies that don't get included when you do that. You can require them yourself to be able to use strip_tags.

require 'erb'
require 'active_support'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/string/encoding'
require 'action_view/helpers/capture_helper'
require 'action_view/helpers/sanitize_helper'

include ActionView::Helpers::SanitizeHelper

strip_tags("<b>lol</b>") # => "lol"

This is assuming you have rails 3 gems installed.

与之呼应 2024-10-12 20:38:54
HTML::FullSanitizer.new.sanitize('<b>lol</b>') # => "lol"
HTML::FullSanitizer.new.sanitize('<b>lol</b>') # => "lol"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文