Commit c33f158c authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Better algorithm for description shortening.

Always show only the first sentence, no matter how short it is.
parent 74cd5041
Loading
Loading
Loading
Loading
+5 −7
Original line number Diff line number Diff line
@@ -204,7 +204,7 @@ module JsDuck
      replace(RDiscount.new(input).to_html)
    end

    # Shortens text if needed.
    # 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.
@@ -217,15 +217,12 @@ module JsDuck
    #
    #   Blah blah blah some text.
    #
    # Ellipsis is only added when input actually gets shortened.
    def shorten(input)
      sent = first_sentence(strip_tags(input))
      if sent.length > @max_length
        sent[0..(@max_length-4)] + "..."
      elsif strip_tags(input).length > sent.length
        sent + " ..."
      else
        input
        sent + " ..."
      end
    end

@@ -235,11 +232,12 @@ module JsDuck

    # Returns true when input should get shortened.
    def too_long?(input)
      first_sentence(input).length > input.length || strip_tags(input).length > @max_length
      stripped = strip_tags(input)
      first_sentence(stripped).length < stripped.length || stripped.length > @max_length
    end

    def strip_tags(str)
      str.gsub(/<.*?>/, "")
      str.gsub(/<.*?>/, "").strip
    end
  end

+31 −10
Original line number Diff line number Diff line
@@ -255,27 +255,48 @@ describe JsDuck::DocFormatter do
      @formatter.max_length = 10
    end

    it "leaves short text unchanged" do
      @formatter.shorten("Ha ha").should == "Ha ha"
    end

    it "leaves text with max length unchanged" do
      @formatter.shorten("1234567890").should == "1234567890"
    it "appends ellipsis to short text" do
      @formatter.shorten("Ha ha").should == "Ha ha ..."
    end

    it "shortens text longer than max length" do
      @formatter.shorten("12345678901").should == "1234567..."
    end

    it "ignores HTML tags when calculating text length" do
      @formatter.shorten("<a href='some-long-link'>Foo</a>").should == "<a href='some-long-link'>Foo</a>"
    end

    it "strips HTML tags when shortening" do
      @formatter.shorten("<a href='some-long-link'>12345678901</a>").should == "1234567..."
    end

    it "takes only first centence" do
      @formatter.shorten("bla. blah").should == "bla. ..."
    end
  end

  describe "#too_long?" do

    before do
      @formatter.max_length = 10
    end

    it "is false when exactly equal to the max_length" do
      @formatter.too_long?("1234567890").should == false
    end

    it "is false when short sentence" do
      @formatter.too_long?("bla bla.").should == false
    end

    it "is true when long sentence" do
      @formatter.too_long?("bla bla bla.").should == true
    end

    it "ignores HTML tags when calculating text length" do
      @formatter.too_long?("<a href='some-long-link'>Foo</a>").should == false
    end

  end


  describe "#first_sentence" do
    it "extracts first sentence" do
      @formatter.first_sentence("Hi John. This is me.").should == "Hi John."