Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"

like image 631
sanitizeme Avatar asked Dec 04 '10 14:12

sanitizeme


4 Answers

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"
like image 132
santuxus Avatar answered Nov 15 '22 06:11

santuxus


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.

like image 40
John Avatar answered Nov 15 '22 08:11

John


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
like image 37
khelll Avatar answered Nov 15 '22 08:11

khelll


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)
like image 39
Malik Shahzad Avatar answered Nov 15 '22 07:11

Malik Shahzad