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

时间:2021-06-09 23:26:37

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.

我需要将HTML转换为纯文本。在ActionView的SanitizeHelper中有一个很好的功能,但是我无法理解如何引用它并在一个简单的test.rb文件中使用它。

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

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

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

我想能够调用strip_tags(“ lol ”)=>“lol”

8 个解决方案

#1


29  

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"

#2


25  

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

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

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.

请注意,此要求不一定会加载所有ActionView。除非线程安全要求自动加载发生急切,否则它只会设置自动加载并需要常见的依赖关系。这意味着遵循要求,如果您参考,例如ActionView :: Helpers :: SanitizeHelper,它将导致需要action_view / helpers /sanitize_helper.rb。

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

因此,使用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

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

这没有详细记录;我的回答主要基于对这个问题的讨论。

#3


14  

I believe this should be enough:

我相信这应该足够了:

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

You can use Nokogiri as well:

您也可以使用Nokogiri:

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:

您仍然可以通过执行以下操作来使用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

#4


8  

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:

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

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

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

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

strip_html_tags(your_html_string)

#5


3  

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

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

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

#6


0  

With this example:

通过这个例子:

"&lt;p&gt;<i>example</i>&lt;/p&gt;"

This helped me:

这对我有所帮助:

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

Output:

输出:

example

#7


-1  

HTML::FullSanitizer.new.sanitize('<b>lol</b>') # => "lol"

#8


-2  

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.

理想情况下,您需要并包含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"

This is assuming you have rails 3 gems installed.

这假设您已安装rails 3 gem。

#1


29  

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"

#2


25  

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

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

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.

请注意,此要求不一定会加载所有ActionView。除非线程安全要求自动加载发生急切,否则它只会设置自动加载并需要常见的依赖关系。这意味着遵循要求,如果您参考,例如ActionView :: Helpers :: SanitizeHelper,它将导致需要action_view / helpers /sanitize_helper.rb。

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

因此,使用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

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

这没有详细记录;我的回答主要基于对这个问题的讨论。

#3


14  

I believe this should be enough:

我相信这应该足够了:

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

You can use Nokogiri as well:

您也可以使用Nokogiri:

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:

您仍然可以通过执行以下操作来使用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

#4


8  

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:

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

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

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

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

strip_html_tags(your_html_string)

#5


3  

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

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

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

#6


0  

With this example:

通过这个例子:

"&lt;p&gt;<i>example</i>&lt;/p&gt;"

This helped me:

这对我有所帮助:

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

Output:

输出:

example

#7


-1  

HTML::FullSanitizer.new.sanitize('<b>lol</b>') # => "lol"

#8


-2  

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.

理想情况下,您需要并包含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"

This is assuming you have rails 3 gems installed.

这假设您已安装rails 3 gem。