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

Add member type info to class member links.

For example {@link Ext#get} will be converted to something like:

    <a href="Ext-method-get">Ext.get</a>

To look up member types, implemented JsDuck::Class#member_type method.
parent b683a183
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -105,13 +105,28 @@ module JsDuck
        all_members.merge!(mix.members_hash(type))
      end

      @doc[type].each do |m|
      (@doc[type] || []).each do |m|
        all_members[m[:name]] = m if !m[:private]
      end

      all_members
    end

    # Looks up member type by member name
    #
    # Returns type of nil if member not found
    def member_type(name)
      # build hash of all members
      unless @type_map
        @type_map = {}
        [:cfg, :property, :method, :event, :css_var, :css_mixin].each do |type|
          @type_map.merge!(members_hash(type))
        end
      end

      @type_map[name] && @type_map[name][:tagname]
    end

    # A way to access full class name with similar syntax to
    # package_name and short_name
    def full_name
+7 −0
Original line number Diff line number Diff line
@@ -142,6 +142,9 @@ module JsDuck

    # applies the link template
    def link(cls, member, anchor_text)
      # prepend type name to member name
      member = member && (get_member_type(cls, member).to_s + "-" + member)

      @link_tpl.gsub(/(%\w)/) do
        case $1
        when '%c'
@@ -158,6 +161,10 @@ module JsDuck
      end
    end

    def get_member_type(cls, member)
      @relations[cls] && @relations[cls].member_type(member)
    end

    # Formats doc-comment for placement into HTML.
    # Renders it with Markdown-formatter and replaces @link-s.
    def format(input)
+24 −10
Original line number Diff line number Diff line
@@ -5,6 +5,16 @@ describe JsDuck::DocFormatter do
  before do
    @formatter = JsDuck::DocFormatter.new
    @formatter.context = "Context"
    @formatter.relations = {
      'Context' => JsDuck::Class.new({
        :method => [{:name => "bar", :tagname => :method}]
      }),
      'Ext.Msg' => JsDuck::Class.new({
      }),
      'Foo' => JsDuck::Class.new({
        :cfg => [{:name => "bar", :tagname => :cfg}]
      }),
    }
  end

  describe "#replace" do
@@ -18,12 +28,12 @@ describe JsDuck::DocFormatter do

    it "replaces {@link Foo#bar} with link to class member" do
      @formatter.replace("Look at {@link Foo#bar}").should ==
        'Look at <a href="Foo#bar">Foo.bar</a>'
        'Look at <a href="Foo#cfg-bar">Foo.bar</a>'
    end

    it "uses context to replace {@link #bar} with link to class member" do
      @formatter.replace("Look at {@link #bar}").should ==
        'Look at <a href="Context#bar">bar</a>'
        'Look at <a href="Context#method-bar">bar</a>'
    end

    it "allows use of custom link text" do
@@ -77,12 +87,16 @@ describe JsDuck::DocFormatter do
    describe "auto-detect" do
      before do
        @formatter.relations = {
          'FooBar' => true,
          'FooBar.Blah' => true,
          'Ext.form.Field' => true,
          'Ext.XTemplate' => true,
          'MyClass' => true,
          'Ext' => true,
          'FooBar' => JsDuck::Class.new({}),
          'FooBar.Blah' => JsDuck::Class.new({}),
          'Ext.form.Field' => JsDuck::Class.new({
            :method => [{:name => "getValues", :tagname => :method}]
          }),
          'Ext.XTemplate' => JsDuck::Class.new({}),
          'MyClass' => JsDuck::Class.new({}),
          'Ext' => JsDuck::Class.new({
            :method => [{:name => "encode", :tagname => :method}]
          }),
        }
      end

@@ -128,12 +142,12 @@ describe JsDuck::DocFormatter do

      it "converts Ext#encode to method link" do
        @formatter.replace("Look at Ext#encode").should ==
          'Look at <a href="Ext#encode">Ext.encode</a>'
          'Look at <a href="Ext#method-encode">Ext.encode</a>'
      end

      it "converts Ext.form.Field#getValues to method link" do
        @formatter.replace("Look at Ext.form.Field#getValues").should ==
          'Look at <a href="Ext.form.Field#getValues">Ext.form.Field.getValues</a>'
          'Look at <a href="Ext.form.Field#method-getValues">Ext.form.Field.getValues</a>'
      end

      it "doesn't create links inside {@link} tag" do