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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4354060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:35:26  来源:igfitidea点击:

How can I use strip_tags in regular Ruby code (non-rails)?

htmlrubytagsstripactionview

提问by sanitizeme

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("<b>lol</b>") => "lol"

回答by santuxus

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.

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

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

回答by John

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.rbto be required.

请注意,此 require 不一定会加载所有 ActionView。除非线程安全要求自动加载急切地发生,否则它仅设置自动加载并需要通用依赖项。这意味着遵循 require ,如果您引用,例如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.

这没有很好的记录;我的这个答案主要基于对这个问题的讨论

回答by khelll

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 one:

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

回答by Malik Shahzad

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)

回答by Aleks

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

这个问题很老了,但你可以这样称呼它test.rb

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

回答by shilovk

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

回答by Samuel

Ideally you would require and include ActionView::Helpers::SanitizeHelperbut 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 gems。

回答by Alexandr Yakubenko

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