Module: Truncato

Defined in:
lib/truncato/version.rb,
lib/truncato/truncato.rb

Constant Summary collapse

VERSION =
'0.7.10'
DEFAULT_OPTIONS =
{
    max_length: 30,
    count_tags: true,
    tail: "...",
    filtered_attributes: []
}
ARTIFICIAL_ROOT_NAME =
'__truncato_root__'

Class Method Summary collapse

Class Method Details

.truncate(source, user_options = {}) ⇒ String

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 (…).

Options Hash (user_options):

  • :max_length (Integer)

    Maximum length

  • :tail (String)

    text to append when the truncation happens

  • :count_tags (Boolean)

    `true` for counting tags for truncation, `false` for not counting them

  • :filtered_attributes (Array<String>)

    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.


21
22
23
24
# 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