Sha256: a10365ee5caabc97efe881524700f11d99d4f1bcd15bca91844f56b65735bc97

Contents?: true

Size: 1.52 KB

Versions: 30

Compression:

Stored size: 1.52 KB

Contents

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

module JsDuck
  module Format

    # Little helper for shortening text
    class Shortener
      # Takes as parameter the maximum length for text that doesn't
      # get shortened.  Used for testing purposes.
      def initialize(max_length = 120)
        @max_length = max_length
      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
end

Version data entries

30 entries across 30 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/format/shortener.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/format/shortener.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/format/shortener.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/format/shortener.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/format/shortener.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/format/shortener.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/format/shortener.rb
jsduck-troopjs-0.0.10 lib/jsduck/format/shortener.rb
jsduck-troopjs-0.0.9 lib/jsduck/format/shortener.rb
jsduck-troopjs-0.0.8 lib/jsduck/format/shortener.rb
jsduck-troopjs-0.0.7 lib/jsduck/format/shortener.rb
jsduck-troopjs-0.0.5 lib/jsduck/format/shortener.rb
jsduck-troopjs-0.0.4 lib/jsduck/format/shortener.rb
jsduck-troopjs-0.0.3 lib/jsduck/format/shortener.rb
jsduck-troopjs-0.0.1 lib/jsduck/format/shortener.rb
jsduck-6.0.0beta lib/jsduck/format/shortener.rb
jsduck-5.3.4 lib/jsduck/format/shortener.rb
jsduck-5.3.3 lib/jsduck/format/shortener.rb
jsduck-5.3.2 lib/jsduck/format/shortener.rb
jsduck-5.3.1 lib/jsduck/format/shortener.rb