Sha256: 8c27d25e0945096ef3a4e776a50903420531e19499725935a8a5981c7f396602

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module PrawnRails
  module Extension
    ##
    # Removes all html tags from the string
    # @param html[String] string to remove the tags
    # @return [String] the string with out the tags
    ##
    def html_strip html
      return html if html.nil?
      html
      text = html.
        gsub(/( |\n|\s)+/im, ' ').squeeze(' ').strip.
        gsub(/<([^\s]+)[^>]*(src|href)=\s*(.?)([^>\s]*)\3[^>]*>\4<\/\1>/i,
             '\4')

      links = []
      linkregex = /<[^>]*(src|href)=\s*(.?)([^>\s]*)\2[^>]*>\s*/i
      while linkregex.match(text)
        links << $~[3]
        text.sub!(linkregex, "[#{links.size}]")
      end

      text = CGI.unescapeHTML(
                              text.
                              gsub(/<(script|style)[^>]*>.*<\/\1>/im, '').
                              gsub(/<!--.*-->/m, '').
                              gsub(/<hr(| [^>]*)>/i, "___\n").
                              gsub(/<li(| [^>]*)>/i, "\n* ").
                              gsub(/<blockquote(| [^>]*)>/i, '> ').
                              gsub(/<(br)(| [^>]*)>/i, "\n").
                              gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n").
                              gsub(/<[^>]*>/, '')
                              ).lstrip.gsub(/\n[ ]+/, "\n") + "\n"

      for i in (0...links.size).to_a
        text = text + "\n  [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless
          links[i].nil?
      end
      links = nil
      text
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prawn-rails-0.1.1 lib/prawn-rails/extension.rb