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

Move use of Shortener inside Tag::Doc.

Turn Shortener from singleton into normal class - simplifies testing,
and in the context of Tag::Doc it only gets instantiated once anyway.

Also rename :shortDoc to :short_doc - a more Rubysh name.
parent 0f22e110
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
require 'jsduck/type_parser'
require 'jsduck/logger'
require 'jsduck/tag_registry'
require 'jsduck/format/shortener'
require 'jsduck/util/html'

module JsDuck
@@ -41,9 +40,6 @@ module JsDuck
      def format_member(m)
        @formatter.doc_context = m[:files][0]
        format_tags(m)
        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
        is_css_tag = m[:tagname] == :css_var || m[:tagname] == :css_mixin
@@ -56,10 +52,6 @@ module JsDuck
        m
      end

      def expandable?(m)
        m[:params] || (m[:properties] && m[:properties].length > 0) || m[:default] || m[:deprecated] || m[:template]
      end

      def format_item(it, is_css_tag)
        it[:doc] = @formatter.format(it[:doc]) if it[:doc]
        it[:html_type] = (@include_types && !is_css_tag) ? format_type(it[:type]) : it[:type] if it[:type]
+4 −9
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
      # 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
+1 −1
Original line number Diff line number Diff line
@@ -126,7 +126,7 @@ module JsDuck
            # short and long descriptions
            "<div class='description'>",
              "<div class='short'>",
                m[:shortDoc] ? m[:shortDoc] : m[:doc],
                m[:short_doc] ? m[:short_doc] : m[:doc],
              "</div>",
              "<div class='long'>",
                render_tags(m),
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ module JsDuck
      end

      def render_expandable
        @m[:shortDoc] ? "expandable" : "not-expandable"
        @m[:short_doc] ? "expandable" : "not-expandable"
      end

      def render_name
+12 −0
Original line number Diff line number Diff line
require "jsduck/tag/tag"
require 'jsduck/format/shortener'

module JsDuck::Tag
  # A special class for rendering the documentation field inside
@@ -7,14 +8,25 @@ module JsDuck::Tag
    def initialize
      @key = :doc
      @html_position = POS_DOC
      @shortener = JsDuck::Format::Shortener.new
    end

    def format(m, formatter)
      m[:doc] = formatter.format(m[:doc])

      if expandable?(m) || @shortener.too_long?(m[:doc])
        m[:short_doc] = @shortener.shorten(m[:doc])
      end
    end

    def to_html(m)
      m[:doc]
    end

    private

    def expandable?(m)
      m[:params] || (m[:properties] && m[:properties].length > 0) || m[:default] || m[:deprecated] || m[:template]
    end
  end
end
Loading