Module: JekyllMiscellaneous::Filters::Truncate
- Defined in:
- lib/jekyll_miscellaneous/filters/truncate.rb
Instance Method Summary collapse
-
#truncate(input, truncate_at = 140, separator = nil, omission = '...') ⇒ Object
Truncates a string to a certain length, and adds an ellipsis if the string is truncated.
Instance Method Details
#truncate(input, truncate_at = 140, separator = nil, omission = '...') ⇒ Object
Truncates a string to a certain length, and adds an ellipsis if the string is truncated. If the string is already shorter than the specified length, it is returned unchanged. If the string is longer than the specified length, it is truncated to that length. The ellipsis is added to the end of the string. The separator is used to split the string into words.
Parameters:
- input
-
The string to truncate.
- length
-
The maximum length of the string.
- separator
-
The string to use as a separator.
- omission
-
The string to use as an omission.
Returns:
The truncated string.
Example:
truncate('Hello, World!', 10)
# => "Hello, ..."
truncate('Hello, World!', 10, ',')
# => "Hello..."
truncate('Hello, World!', 10, ',', '!')
# => "Hello!"
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jekyll_miscellaneous/filters/truncate.rb', line 33 def truncate(input, truncate_at = 140, separator = nil, omission = '...') return input.dup unless input.length > truncate_at length_with_room_for_omission = truncate_at - omission.length stop = if separator input.rindex(separator, length_with_room_for_omission) || length_with_room_for_omission else length_with_room_for_omission end "#{input[0...stop]}#{omission}" end |