Commit 2b9ba16b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Include shortDesc to JSON export.

Moved shortening functionality to doc_formatter class.
parent 517b4eab
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -18,10 +18,14 @@ module JsDuck
    # Context#blah is meant.
    attr_accessor :context

    # Maximum length for text that doesn't get shortened, defaults to 120
    attr_accessor :max_length

    def initialize
      @context = ""
      @cssClass = nil
      @urlTemplate = "%cls%"
      @max_length = 120
    end

    # Replaces {@link Class#member link text} in given string with
@@ -81,6 +85,36 @@ module JsDuck
      replace(RDiscount.new(input).to_html)
    end

    # Shortens text if needed.
    #
    # 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.
    #
    # Ellipsis is only added when input actually gets shortened.
    def shorten(input)
      if too_long?(input)
        strip_tags(input)[0..(@max_length-4)] + "..."
      else
        input
      end
    end

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

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

end
+3 −0
Original line number Diff line number Diff line
@@ -48,6 +48,9 @@ module JsDuck
    def format_member(m)
      m = m.clone
      m[:doc] = @formatter.format(m[:doc]) if m[:doc]
      if m[:params] || @formatter.too_long?(m[:doc])
        m[:shortDoc] = @formatter.shorten(m[:doc])
      end
      m[:params] = format_params(m[:params]) if m[:params]
      m[:return] = format_return(m[:return]) if m[:return]
      m
+4 −21
Original line number Diff line number Diff line
@@ -91,24 +91,11 @@ module JsDuck
      return "<a id='#{id}'></a><b><a href='#{src}'>#{item[:name]}</a></b>" + signature_suffix(item)
    end

    # 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.
    #
    # Creates either expandable or normal doc-entry
    def expandable_desc(p_doc, e_doc)
      if expandable?(p_doc, e_doc)
        # Only show ellipsis when primary_doc is shortened.
        tagless = strip_tags(p_doc)
        short_doc = tagless[0..116]
        ellipsis = tagless.length > short_doc.length ? "..." : ""
        "<div class='short'>#{short_doc}#{ellipsis}</div>" +
        short_doc = @formatter.shorten(p_doc)
        "<div class='short'>#{short_doc}</div>" +
          "<div class='long'>#{p_doc}#{e_doc}</div>"
      else
        p_doc
@@ -125,11 +112,7 @@ module JsDuck
    end

    def expandable?(p_doc, e_doc)
      strip_tags(p_doc).length > 120 || e_doc.length > 0
    end

    def strip_tags(str)
      str.gsub(/<.*?>/, "")
      @formatter.too_long?(p_doc) || e_doc.length > 0
    end

    def inherited?(item)
+29 −1
Original line number Diff line number Diff line
@@ -3,7 +3,8 @@ require "jsduck/doc_formatter"
describe JsDuck::DocFormatter do

  before do
    @formatter = JsDuck::DocFormatter.new("Context")
    @formatter = JsDuck::DocFormatter.new
    @formatter.context = "Context"
  end

  describe "#replace" do
@@ -98,4 +99,31 @@ describe JsDuck::DocFormatter do

  end

  describe "#shorten" do

    before 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"
    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
  end

end