class TruncatedSaxDocument
Constants
- IGNORABLE_TAGS
- SINGLE_TAGS
Attributes
filtered_attributes[R]
ignored_levels[R]
max_length[R]
max_length_reached[R]
tail[R]
truncated_string[R]
Public Class Methods
new(options)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 12 def initialize(options) @html_coder = HTMLEntities.new capture_options options init_parsing_state end
Public Instance Methods
characters(decoded_string)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 25 def characters decoded_string return if @max_length_reached || ignore_mode? 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
comment(string)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 32 def comment string if @comments return if @max_length_reached process_comment string end end
end_document()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 53 def end_document close_truncated_document if max_length_reached end
end_element(name)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 39 def end_element name if filtered_tags.include?(name) && ignore_mode? exit_ignored_level return end return if @max_length_reached || ignorable_tag?(name) || ignore_mode? unless single_tag_element? name @closing_tags.pop append_to_truncated_string closing_tag(name), overriden_tag_length end end
start_element(name, attributes)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 18 def start_element name, attributes enter_ignored_level if filtered_tags.include?(name) return if @max_length_reached || ignorable_tag?(name) || ignore_mode? @closing_tags.push name unless single_tag_element? name append_to_truncated_string opening_tag(name, attributes), overriden_tag_length end
Private Instance Methods
append_to_truncated_string(string, overriden_length=nil)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 96 def append_to_truncated_string string, overriden_length=nil @truncated_string << string increase_estimated_length(overriden_length || string.length) end
artificial_root_name?(name)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 174 def artificial_root_name? name name == Truncato::ARTIFICIAL_ROOT_NAME end
attributes_to_string(attributes)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 110 def attributes_to_string attributes return "" if attributes.empty? attributes_string = concatenate_attributes_declaration attributes attributes_string.rstrip end
capture_options(options)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 59 def capture_options(options) @max_length = options[:max_length] @count_tags = options [:count_tags] @count_tail = options.fetch(:count_tail, false) @tail = options[:tail] @filtered_attributes = options[:filtered_attributes] || [] @filtered_tags = options[:filtered_tags] || [] @tail_before_final_tag = options.fetch(:tail_before_final_tag, false) @comments = options.fetch(:comments, false) end
check_max_length_reached()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 133 def check_max_length_reached @max_length_reached = true if @estimated_length >= max_length end
close_truncated_document()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 155 def close_truncated_document append_tail_between_closing_tags if @tail_before_final_tag append_to_truncated_string tail unless @tail_appended append_closing_tags end
closing_tag(name)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 124 def closing_tag name "</#{name}>" end
comment_tag(comment)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 76 def comment_tag comment "<!--#{comment}-->" end
concatenate_attributes_declaration(attributes)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 116 def concatenate_attributes_declaration attributes attributes.inject(' ') do |string, attribute| key, value = attribute next string if @filtered_attributes.include? key string << "#{key}='#{@html_coder.encode value}' " end end
enter_ignored_level()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 182 def enter_ignored_level @ignored_levels += 1 end
exit_ignored_level()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 186 def exit_ignored_level @ignored_levels -= 1 end
ignorable_tag?(name)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 170 def ignorable_tag?(name) artificial_root_name?(name) || IGNORABLE_TAGS.include?(name.downcase) end
ignore_mode?()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 190 def ignore_mode? @ignored_levels > 0 end
increase_estimated_length(amount)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 128 def increase_estimated_length amount @estimated_length += amount check_max_length_reached end
init_parsing_state()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 80 def init_parsing_state @truncated_string = "" @closing_tags = [] @estimated_length = @count_tail ? tail_length : 0 @max_length_reached = false @ignored_levels = 0 end
opening_tag(name, attributes)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 101 def opening_tag name, attributes attributes_string = attributes_to_string attributes if single_tag_element? name "<#{name}#{attributes_string}/>" else "<#{name}#{attributes_string}>" end end
overriden_tag_length()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 165 def overriden_tag_length @count_tags ? nil : 0 end
process_comment(string)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 70 def process_comment(string) remaining_length = max_length - @estimated_length - 1 string_to_append = comment_tag(string).length > remaining_length ? truncate_comment(comment_tag(string), remaining_length) : comment_tag(string) append_to_truncated_string string_to_append end
single_tag_element?(name)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 92 def single_tag_element? name SINGLE_TAGS.include? name end
tail_length()
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 88 def tail_length tail.match(/^&\w+;$/).nil? ? tail.length : 1 end
truncate_comment(string, remaining_length)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 146 def truncate_comment string, remaining_length if @tail_before_final_tag string[0..remaining_length] else @tail_appended = true "#{string[0..remaining_length]}#{tail}-->" end end
truncate_string(string, remaining_length)
click to toggle source
# File lib/truncato/truncated_sax_document.rb, line 137 def truncate_string string, remaining_length if @tail_before_final_tag string[0..remaining_length] else @tail_appended = true "#{string[0..remaining_length]}#{tail}" end end