Sha256: f7f0c191b89956e6f5a97e887b227b70d259a812283e4bb7bd2dcce39d43ac9d

Contents?: true

Size: 943 Bytes

Versions: 1

Compression:

Stored size: 943 Bytes

Contents

module Jkl
  module Text
    class << self

      def sanitize(text, words_on_line = 5)
        remove_short_lines(strip_all_tags(remove_script_tags(text)), words_on_line)
      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, words_on_line = 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(" ") < words_on_line
        end
        str
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jakal-0.1.96 lib/jkl/text_client.rb