class JsDuck::Format::Shortener
Little helper for shortening text
Public Class Methods
new(max_length = 120)
click to toggle source
Takes as parameter the maximum length for text that doesn't get shortened. Used for testing purposes.
# File lib/jsduck/format/shortener.rb, line 10 def initialize(max_length = 120) @max_length = max_length end
Public Instance Methods
first_sentence(str)
click to toggle source
Returns the first sentence inside a string.
# File lib/jsduck/format/shortener.rb, line 39 def first_sentence(str) str.sub(/\A(.+?(\.|。))\s.*\z/mu, "\\1") end
shorten(input)
click to toggle source
Shortens text
116 chars is also where ext-doc makes its cut, but unlike ext-doc we only make the cut when there's more than 120 chars.
This way we don't get stupid expansions like:
Blah blah blah some text...
expanding to:
Blah blah blah some text.
# File lib/jsduck/format/shortener.rb, line 27 def shorten(input) sent = first_sentence(Util::HTML.strip_tags(input).strip) # Use u-modifier to correctly count multi-byte characters chars = sent.scan(/./mu) if chars.length > @max_length chars[0..(@max_length-4)].join + "..." else sent + " ..." end end
too_long?(input)
click to toggle source
Returns true when input should get shortened.
# File lib/jsduck/format/shortener.rb, line 44 def too_long?(input) stripped = Util::HTML.strip_tags(input).strip # for sentence v/s full - compare byte length # for full v/s max - compare char length first_sentence(stripped).length < stripped.length || stripped.scan(/./mu).length > @max_length end