Commit 0f22e110 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Move Shortener also to Format namespace.

parent 1915fdb1
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
require 'jsduck/type_parser'
require 'jsduck/logger'
require 'jsduck/tag_registry'
require 'jsduck/shortener'
require 'jsduck/format/shortener'
require 'jsduck/util/html'

module JsDuck
@@ -41,8 +41,8 @@ module JsDuck
      def format_member(m)
        @formatter.doc_context = m[:files][0]
        format_tags(m)
        if expandable?(m) || Shortener.too_long?(m[:doc])
          m[:shortDoc] = Shortener.shorten(m[:doc])
        if expandable?(m) || Format::Shortener.too_long?(m[:doc])
          m[:shortDoc] = Format::Shortener.shorten(m[:doc])
        end

        # We don't validate and format CSS var and mixin type definitions
+60 −0
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-
require 'jsduck/util/html'
require 'jsduck/util/singleton'

module JsDuck
  module Format

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

lib/jsduck/shortener.rb

deleted100644 → 0
+0 −58
Original line number Diff line number Diff line
# -*- 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
+7 −7
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-
require "jsduck/shortener"
require "jsduck/format/shortener"

describe JsDuck::Shortener do
describe JsDuck::Format::Shortener do

  describe "#shorten" do

    def shorten(text)
      JsDuck::Shortener.shorten(text)
      JsDuck::Format::Shortener.shorten(text)
    end

    before do
      JsDuck::Shortener.instance.max_length = 10
      JsDuck::Format::Shortener.instance.max_length = 10
    end

    it "appends ellipsis to short text" do
@@ -43,11 +43,11 @@ describe JsDuck::Shortener do
  describe "#too_long?" do

    def too_long?(text)
      JsDuck::Shortener.too_long?(text)
      JsDuck::Format::Shortener.too_long?(text)
    end

    before do
      JsDuck::Shortener.instance.max_length = 10
      JsDuck::Format::Shortener.instance.max_length = 10
    end

    it "is false when exactly equal to the max_length" do
@@ -75,7 +75,7 @@ describe JsDuck::Shortener do

  describe "#first_sentence" do
    def first_sentence(text)
      JsDuck::Shortener.first_sentence(text)
      JsDuck::Format::Shortener.first_sentence(text)
    end

    it "extracts first sentence" do