Class: TruncateHtml::HtmlString
- Inherits:
-
String
- Object
- String
- TruncateHtml::HtmlString
- Defined in:
- lib/truncate_html/html_string.rb
Constant Summary collapse
- UNPAIRED_TAGS =
%w(br hr img).freeze
Instance Method Summary collapse
- #html_comment? ⇒ Boolean
- #html_tag? ⇒ Boolean
- #html_tokens ⇒ Object
-
#initialize(original_html) ⇒ HtmlString
constructor
A new instance of HtmlString.
- #matching_close_tag ⇒ Object
- #open_tag? ⇒ Boolean
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
31 32 33 |
# File 'lib/truncate_html/html_string.rb', line 31 def html_comment? /<\s?!--.*-->/ === self end |
#html_tag? ⇒ Boolean
23 24 25 |
# File 'lib/truncate_html/html_string.rb', line 23 def html_tag? /<\/?[^>]+>/ === self && !html_comment? end |
#html_tokens ⇒ Object
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_tag ⇒ Object
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
27 28 29 |
# File 'lib/truncate_html/html_string.rb', line 27 def open_tag? /<(?!(?:#{UNPAIRED_TAGS.join('|')}|script|\/))[^>]+>/i === self end |