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

前端 未结 8 1645
广开言路
广开言路 2020-12-31 00:09

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

相关标签:
8条回答
  • 2020-12-31 00:17

    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"
    
    0 讨论(0)
  • 2020-12-31 00:18

    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)
    
    0 讨论(0)
  • 2020-12-31 00:19

    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.

    0 讨论(0)
  • 2020-12-31 00:19
    HTML::FullSanitizer.new.sanitize('<b>lol</b>') # => "lol"
    
    0 讨论(0)
  • 2020-12-31 00:21

    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
    
    0 讨论(0)
  • 2020-12-31 00:28

    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.

    0 讨论(0)
提交回复
热议问题