Class: TruncatedSaxDocument

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/truncato/truncated_sax_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  init_from_options(options)
  @html_coder = HTMLEntities.new
  @truncated_string = ""
  @closing_tags = []
  @estimated_length = 0
  @max_length_reached = false
end

Instance Attribute Details

#count_tagsObject (readonly)

Returns the value of attribute count_tags.



2
3
4
# File 'lib/truncato/truncated_sax_document.rb', line 2

def count_tags
  @count_tags
end

#max_lengthObject (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_reachedObject (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

#tailObject (readonly)

Returns the value of attribute tail.



2
3
4
# File 'lib/truncato/truncated_sax_document.rb', line 2

def tail
  @tail
end

#truncated_stringObject (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_documentObject



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