Class: NokogiriTruncateHtml::TruncateDocument
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- NokogiriTruncateHtml::TruncateDocument
- Defined in:
- lib/nokogiri_truncate_html/truncate_document.rb
Constant Summary collapse
- HTML_TAG =
'html'.freeze
- BODY_TAG =
'body'.freeze
- BR_TAG =
'br'.freeze
- EMBED_TAG =
'embed'.freeze
- HR_TAG =
'hr'.freeze
- IMG_TAG =
'img'.freeze
- INPUT_TAG =
'input'.freeze
- PARAM_TAG =
'param'.freeze
Instance Attribute Summary collapse
-
#length ⇒ Object
writeonly
Sets the attribute length.
-
#omission ⇒ Object
writeonly
Sets the attribute omission.
Instance Method Summary collapse
- #characters(text) ⇒ Object
- #end_element(name) ⇒ Object
-
#initialize ⇒ TruncateDocument
constructor
A new instance of TruncateDocument.
- #output ⇒ Object
- #start_document ⇒ Object
- #start_element(name, attrs = []) ⇒ Object
Constructor Details
#initialize ⇒ TruncateDocument
Returns a new instance of TruncateDocument.
8 9 10 |
# File 'lib/nokogiri_truncate_html/truncate_document.rb', line 8 def initialize @discard_first_element = false end |
Instance Attribute Details
#length=(value) ⇒ Object (writeonly)
Sets the attribute length
12 13 14 |
# File 'lib/nokogiri_truncate_html/truncate_document.rb', line 12 def length=(value) @length = value end |
#omission=(value) ⇒ Object (writeonly)
Sets the attribute omission
12 13 14 |
# File 'lib/nokogiri_truncate_html/truncate_document.rb', line 12 def omission=(value) @omission = value end |
Instance Method Details
#characters(text) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/nokogiri_truncate_html/truncate_document.rb', line 26 def characters(text) @output << CGI.escapeHTML(text[0, @chars_remaining]) @chars_remaining -= text.length if @chars_remaining < 0 @output << @omission throw :truncate_finished end end |
#end_element(name) ⇒ Object
66 67 68 |
# File 'lib/nokogiri_truncate_html/truncate_document.rb', line 66 def end_element(name) @output << "</#{@tags.pop}>" if @tags.last == name end |
#output ⇒ Object
14 15 16 17 18 19 |
# File 'lib/nokogiri_truncate_html/truncate_document.rb', line 14 def output while @tags.size > 0 @output << "</#{@tags.pop}>" end @output end |
#start_document ⇒ Object
21 22 23 24 |
# File 'lib/nokogiri_truncate_html/truncate_document.rb', line 21 def start_document @output, @chars_remaining, @tags = String.new, @length, [] @discard_first_element = false end |
#start_element(name, attrs = []) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/nokogiri_truncate_html/truncate_document.rb', line 44 def start_element(name, attrs = []) unless @discard_first_element return if name == HTML_TAG || name == BODY_TAG return @discard_first_element = true end @output << "<#{name}" unless attrs.empty? attrs.each do |attr, val| @output << " #{attr}=\"#{val}\"" end end if name == BR_TAG || name == EMBED_TAG || name == HR_TAG || name == IMG_TAG || name == INPUT_TAG || name == PARAM_TAG @output << ' />' else @output << '>' @tags.push name end end |