Sha256: bad25478d878cb73950b99086a67c0dce3ab49d7033d6e9560344933b073e4d0

Contents?: true

Size: 1.48 KB

Versions: 22

Compression:

Stored size: 1.48 KB

Contents

# -*- coding: utf-8 -*-
require 'jsduck/util/html'
require 'jsduck/util/singleton'

module JsDuck

  # Little helper for shortening text
  class Shortener
    include Util::Singleton

    # Maximum length for text that doesn't get shortened.
    # The accessor is used for testing purposes only.
    attr_accessor :max_length

    def initialize
      @max_length = 120
    end

    # 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.
    #
    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

    # Returns the first sentence inside a string.
    def first_sentence(str)
      str.sub(/\A(.+?(\.|。))\s.*\Z/mu, "\\1")
    end

    # Returns true when input should get shortened.
    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

  end

end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
jsduck-4.10.4 lib/jsduck/shortener.rb
jsduck-4.10.3 lib/jsduck/shortener.rb
jsduck-4.10.2 lib/jsduck/shortener.rb
jsduck-4.10.1 lib/jsduck/shortener.rb
jsduck-4.10.0 lib/jsduck/shortener.rb
jsduck-4.9.0 lib/jsduck/shortener.rb
jsduck-4.8.0 lib/jsduck/shortener.rb
jsduck-4.7.1 lib/jsduck/shortener.rb
jsduck-4.7.0 lib/jsduck/shortener.rb
jsduck-4.6.2 lib/jsduck/shortener.rb
jsduck-4.6.1 lib/jsduck/shortener.rb
jsduck-4.6.0 lib/jsduck/shortener.rb
jsduck-4.5.1 lib/jsduck/shortener.rb
jsduck-4.5.0 lib/jsduck/shortener.rb
jsduck-4.4.1 lib/jsduck/shortener.rb
jsduck-4.4.0 lib/jsduck/shortener.rb
jsduck-4.3.2 lib/jsduck/shortener.rb
jsduck-4.3.1 lib/jsduck/shortener.rb
jsduck-4.3.0 lib/jsduck/shortener.rb
jsduck-4.2.1 lib/jsduck/shortener.rb