Sha256: 143e33631724c5305458302f02a059bc2d15e2b7d0cd58d4b1eb545416910c16

Contents?: true

Size: 915 Bytes

Versions: 3

Compression:

Stored size: 915 Bytes

Contents

module Jkl
  module Text
    class << self

      def sanitize(text, number = 5)
        remove_short_lines(strip_all_tags(remove_script_tags(text)), number)
      end
      alias :clean :sanitize

      def strip_all_tags(text)
        text.gsub(/<\/?[^>]*>/, "")
      end

      def remove_blank_lines(text)
        text.gsub(/\n\r|\r\n|\n|\r/, "")
      end

      def remove_html_comments(text)
        text.gsub(/<!--(.|\s)*?-->/, "")
      end

      def remove_script_tags(text)
        text = remove_html_comments(text)
        text.gsub(/((<[\s\/]*script\b[^>]*>)([^>]*)(<\/script>))/i, "")
      end

      def remove_short_lines(text, number = 5)
        text = text.gsub(/\s\s/, "\n")
        str = ""
        # remove short lines - ususally just navigation
        text.split("\n").each do |l|
          str << l unless l.count(" ") < number
        end
        str
      end
      
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jakal-0.1.95 lib/jkl/text_client.rb
jakal-0.1.94 lib/jkl/text_client.rb
jakal-0.1.93 lib/jkl/text_client.rb