Sha256: 4d1fc3b68e7f405db199b17b776a384547766230d9bf688f16fca9e4ad316b13

Contents?: true

Size: 1.08 KB

Versions: 124

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

class ReeText::StripTags
  include Ree::FnDSL

  fn :strip_tags do
    link :is_blank, from: :ree_object
    link 'ree_text/scrubbers/text_only_scrubber', -> { TextOnlyScrubber }
  end

  doc(<<~DOC)
    Strips all HTML tags from +html+, including comments and special characters.
    
      strip_tags("Strip <i>these</i> tags!")
      # => Strip these tags!
    
      strip_tags("<b>Bold</b> no more!  <a href='more.html'>See more here</a>...")
      # => Bold no more!  See more here...
    
      strip_tags("<div id='top-bar'>Welcome to my website!</div>")
      # => Welcome to my website!
    
      strip_tags("> A quote from Smith & Wesson")
      # => &gt; A quote from Smith &amp; Wesson
  DOC
  
  contract(String => String)
  def call(html)
    return html if is_blank(html)

    loofah_fragment = Loofah.fragment(html)
    loofah_fragment.scrub!(TextOnlyScrubber.new)
    properly_encode(loofah_fragment, encoding: 'UTF-8')
  end

  private

  def properly_encode(fragment, options)
    fragment.xml? ? fragment.to_xml(options) : fragment.to_html(options)
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.23 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.22 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.21 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.20 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.19 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.18 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.17 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.16 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.15 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.14 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.13 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.12 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.11 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.10 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.9 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.8 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.7 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.6 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.5 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.4 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb