Sha256: 07c091fa7162da99906af374357c39d4d467eacd51b254428c27dd79f93a3e3e

Contents?: true

Size: 880 Bytes

Versions: 4

Compression:

Stored size: 880 Bytes

Contents

module TruncateHtml
  class HtmlString < String

    UNPAIRED_TAGS = %w(br hr img)

    def initialize(original_html)
      super(original_html)
    end

    def html_tokens
      scan(regex).map do
        |token| token.gsub(
          #remove newline characters
            /\n/,''
        ).gsub(
          #clean out extra consecutive whitespace
            /\s+/, ' '
        )
      end.map { |token| HtmlString.new(token) }
    end

    def html_tag?
      self =~ /<\/?[^>]+>/ ? true : false
    end

    def open_tag?
      self =~ /<(?!(?:#{UNPAIRED_TAGS.join('|')}|script|\/))[^>]+>/i ? true : false
    end

    def matching_close_tag
      gsub(/<(\w+)\s?.*>/, '</\1>').strip
    end

    private
    def regex
      /(?:<script.*>.*<\/script>)+|<\/?[^>]+>|[#{"[[:alpha:]]" if RUBY_VERSION >= '1.9'}\w\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'",\.\/?]+|\s+/
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
truncate_html-0.5.3 lib/truncate_html/html_string.rb
truncate_html-0.5.2 lib/truncate_html/html_string.rb
truncate_html-0.5.1 lib/truncate_html/html_string.rb
truncate_html-0.5.0 lib/truncate_html/html_string.rb