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

Auto-create @key field for meta-tags when missing.

This avoids code like: tag.key || tag.name

Also the :meta hash now always contains symbol indexes, not mix of
strings and symbols.
parent 7bc2dd6a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -163,7 +163,7 @@ module JsDuck
      prev_tag = @current_tag

      add_tag(:meta)
      @current_tag[:name] = tag.key || tag.name
      @current_tag[:name] = tag.key
      match(/\w+/)
      skip_horiz_white

+3 −3
Original line number Diff line number Diff line
@@ -8,9 +8,9 @@ module JsDuck
    # Name of the tag (required)
    attr_reader :name

    # The key under which to store this tag. Should be a symbol.
    # By default the string :name is used as key.
    attr_reader :key
    # The key under which to store this tag. Must be a symbol.  When
    # not provided then :name is converted to symbol and used as key.
    attr_accessor :key

    # The text to display in member signature.  Must be a hash
    # defining the short and long versions of the signature text:
+10 −2
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ module JsDuck
    # instatiates builtin meta tags
    def initialize
      @classes = MetaTag.descendants
      @meta_tags = @classes.map {|cls| cls.new }
      @meta_tags = @classes.map {|cls| create_tag(cls) }
    end

    # Loads user-defined meta-tags from given paths.
@@ -46,12 +46,20 @@ module JsDuck
      MetaTag.descendants.each do |cls|
        if !@classes.include?(cls)
          @classes << cls
          newtag = cls.new
          newtag = create_tag(cls)
          @meta_tags = @meta_tags.find_all {|t| t.name != newtag.name }
          @meta_tags << newtag
        end
      end
    end

    # Instanciates tag class.
    # When .key is missing, creates it from .name
    def create_tag(cls)
      tag = cls.new
      tag.key = tag.name.to_sym unless tag.key
      tag
    end
  end

end
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ module JsDuck
      return if meta_data.size == 0

      MetaTagRegistry.instance.tags.map do |tag|
        contents = meta_data[tag.key || tag.name]
        contents = meta_data[tag.key]
        if contents
          tag.to_html(contents)
        else
+10 −7
Original line number Diff line number Diff line
@@ -8,18 +8,21 @@ describe JsDuck::Aggregator do
  class AuthorTag < JsDuck::MetaTag
    def initialize
      @name = "author"
      @key = :author
    end
  end

  class EmailTag < JsDuck::MetaTag
    def initialize
      @name = "email"
      @key = :email
    end
  end

  class LongTag < JsDuck::MetaTag
    def initialize
      @name = "long"
      @key = :long
      @multiline = true
    end
  end
@@ -46,8 +49,8 @@ describe JsDuck::Aggregator do

    it "detects content of the defined tags" do
      @doc[:meta].should == {
        "author" => ["John Doe", "Steve Jobs"],
        "email" => ["Kill Bill"],
        :author => ["John Doe", "Steve Jobs"],
        :email => ["Kill Bill"],
      }
    end
  end
@@ -68,8 +71,8 @@ describe JsDuck::Aggregator do

    it "detects tag content until next @tag" do
      @doc[:meta].should == {
        "long" => ["Some text\non multiple\nlines."],
        "author" => ["Steve Jobs"],
        :long => ["Some text\non multiple\nlines."],
        :author => ["Steve Jobs"],
      }
    end
  end
@@ -87,8 +90,8 @@ describe JsDuck::Aggregator do

    it "includes {@link} as part of tag content" do
      @doc[:meta].should == {
        "long" => ["Me {@link foo bar}"],
        "author" => ["Me {@link foo bar}"],
        :long => ["Me {@link foo bar}"],
        :author => ["Me {@link foo bar}"],
      }
    end
  end
@@ -106,7 +109,7 @@ describe JsDuck::Aggregator do

    it "detects the meta tag" do
      @doc[:meta].should == {
        "author" => ["John Doe"],
        :author => ["John Doe"],
      }
    end