module Truncato

Constants

ARTIFICIAL_ROOT_NAME
DEFAULT_OPTIONS
VERSION

Public Class Methods

truncate(source, user_options={}) click to toggle source

Truncates the source XML string and returns the truncated XML. It will keep a valid XML structure and insert a tail text indicating the position where content were removed (…).

@param [String] source the XML source to truncate @param [Hash] user_options truncation options @option user_options [Integer] :max_length Maximum length @option user_options [String] :tail text to append when the truncation happens @option user_options [Boolean] :count_tags `true` for counting tags for truncation, `false` for not counting them @option user_options [Array<String>] :filtered_attributes Array of names of attributes that should be excluded in the resulting truncated string. This allows you to make the truncated string shorter by excluding the content of attributes you can discard in some given context, e.g HTML `style` attribute. @return [String] the truncated string

# File lib/truncato/truncato.rb, line 21
def self.truncate source, user_options={}
  options = DEFAULT_OPTIONS.merge(user_options)
  self.truncate_html(source, options) || self.truncate_no_html(source, options)
end

Private Class Methods

do_truncate_html(source, options) click to toggle source
# File lib/truncato/truncato.rb, line 32
def self.do_truncate_html source, options
  truncated_sax_document = TruncatedSaxDocument.new(options)
  parser = Nokogiri::HTML::SAX::Parser.new(truncated_sax_document)
  parser.parse(source) { |context| context.replace_entities = false }
  truncated_string = truncated_sax_document.truncated_string
  truncated_string.empty? ? nil : truncated_string
end
truncate_html(source, options) click to toggle source
# File lib/truncato/truncato.rb, line 28
def self.truncate_html source, options
  self.do_truncate_html(source, options) ? self.do_truncate_html(with_artificial_root(source), options) : nil
end
truncate_no_html(source, options) click to toggle source
# File lib/truncato/truncato.rb, line 44
def self.truncate_no_html source, options
  max_length = options[:max_length]
  tail = source.length > max_length ? options[:tail] : ''
  "#{source[0..max_length-1]}#{tail}"
end
with_artificial_root(source) click to toggle source
# File lib/truncato/truncato.rb, line 40
def self.with_artificial_root(source)
  "<#{ARTIFICIAL_ROOT_NAME}>#{source}</#{ARTIFICIAL_ROOT_NAME}>"
end