Class: TruncatedSaxDocument
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- TruncatedSaxDocument
- Defined in:
- lib/truncato/truncated_sax_document.rb
Instance Attribute Summary collapse
-
#count_tags ⇒ Object
readonly
Returns the value of attribute count_tags.
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
-
#max_length_reached ⇒ Object
readonly
Returns the value of attribute max_length_reached.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
-
#truncated_string ⇒ Object
readonly
Returns the value of attribute truncated_string.
Instance Method Summary collapse
- #characters(decoded_string) ⇒ Object
- #end_document ⇒ Object
- #end_element(name) ⇒ Object
-
#initialize(options) ⇒ TruncatedSaxDocument
constructor
A new instance of TruncatedSaxDocument.
- #start_element(name, attributes) ⇒ Object
Constructor Details
#initialize(options) ⇒ TruncatedSaxDocument
Returns a new instance of TruncatedSaxDocument.
4 5 6 7 8 9 10 11 |
# File 'lib/truncato/truncated_sax_document.rb', line 4 def initialize() () @html_coder = HTMLEntities.new @truncated_string = "" @closing_tags = [] @estimated_length = 0 @max_length_reached = false end |
Instance Attribute Details
#count_tags ⇒ Object (readonly)
Returns the value of attribute count_tags.
2 3 4 |
# File 'lib/truncato/truncated_sax_document.rb', line 2 def @count_tags end |
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
2 3 4 |
# File 'lib/truncato/truncated_sax_document.rb', line 2 def max_length @max_length end |
#max_length_reached ⇒ Object (readonly)
Returns the value of attribute max_length_reached.
2 3 4 |
# File 'lib/truncato/truncated_sax_document.rb', line 2 def max_length_reached @max_length_reached end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
2 3 4 |
# File 'lib/truncato/truncated_sax_document.rb', line 2 def tail @tail end |
#truncated_string ⇒ Object (readonly)
Returns the value of attribute truncated_string.
2 3 4 |
# File 'lib/truncato/truncated_sax_document.rb', line 2 def truncated_string @truncated_string end |
Instance Method Details
#characters(decoded_string) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/truncato/truncated_sax_document.rb', line 19 def characters decoded_string return if @max_length_reached remaining_length = max_length - @estimated_length - 1 string_to_append = decoded_string.length > remaining_length ? truncate_string(decoded_string, remaining_length) : decoded_string append_to_truncated_string @html_coder.encode(string_to_append), string_to_append.length end |
#end_document ⇒ Object
32 33 34 |
# File 'lib/truncato/truncated_sax_document.rb', line 32 def end_document close_truncated_document if max_length_reached end |
#end_element(name) ⇒ Object
26 27 28 29 30 |
# File 'lib/truncato/truncated_sax_document.rb', line 26 def end_element name return if @max_length_reached @closing_tags.pop append_to_truncated_string closing_tag(name), overriden_tag_length end |
#start_element(name, attributes) ⇒ Object
13 14 15 16 17 |
# File 'lib/truncato/truncated_sax_document.rb', line 13 def start_element name, attributes return if @max_length_reached @closing_tags.push name append_to_truncated_string opening_tag(name), overriden_tag_length end |