Class: TruncateHtml::HtmlString

Inherits:
String
  • Object
show all
Defined in:
lib/truncate_html/html_string.rb

Constant Summary collapse

UNPAIRED_TAGS =
%w(br hr img).freeze

Instance Method Summary collapse

Constructor Details

#initialize(original_html) ⇒ HtmlString

Returns a new instance of HtmlString.



7
8
9
# File 'lib/truncate_html/html_string.rb', line 7

def initialize(original_html)
  super(original_html)
end

Instance Method Details

#html_comment?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/truncate_html/html_string.rb', line 31

def html_comment?
  /<\s?!--.*-->/ === self
end

#html_tag?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/truncate_html/html_string.rb', line 23

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

#html_tokensObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/truncate_html/html_string.rb', line 11

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

#matching_close_tagObject



35
36
37
# File 'lib/truncate_html/html_string.rb', line 35

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

#open_tag?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/truncate_html/html_string.rb', line 27

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