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.124 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_text/package/ree_text/functions/strip_tags.rb