Sha256: ffb7d38453146c51f0bf5b980d090ec6fae557aea6da25963c567da3df8d06cf

Contents?: true

Size: 841 Bytes

Versions: 3

Compression:

Stored size: 841 Bytes

Contents

# encoding: utf-8
module TruncateHtml
  class HtmlString < String

    UNPAIRED_TAGS = %w(br hr img).freeze

    def initialize(original_html)
      super(original_html)
    end

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

    def html_tag?
      /<\/?[^>]+>/ === self
    end

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

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

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

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
truncate_html-0.9.1 lib/truncate_html/html_string.rb
truncate_html-0.9 lib/truncate_html/html_string.rb
truncate_html_sentence-0.5.6 lib/truncate_html/html_string.rb