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

Improve meta-tags implementation.

Each meta-tag class is now responsible for rendering a block of all
meta-tags of same type, complete with title and everything.
The transform method has been replaced with to_html method that
converts array of meta-tag contents into output HTML. No more are
there @title and @hidden attributes.
parent 1a2b69c4
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -51,14 +51,19 @@ module JsDuck

    # Merges new class-doc into old one.
    def merge_classes(old, new)
      # Merge booleans
      [:extends, :singleton, :private, :protected].each do |tag|
        old[tag] = old[tag] || new[tag]
      end
      [:mixins, :alternateClassNames, :meta, :files].each do |tag|
      # Merge arrays
      [:mixins, :alternateClassNames, :files].each do |tag|
        old[tag] = old[tag] + new[tag]
      end
      new[:xtypes].each_pair do |key, xtypes|
        old[:xtypes][key] = (old[:xtypes][key] || []) + xtypes
      # Merge hashes of arrays
      [:xtypes, :meta].each do |tag|
        new[tag].each_pair do |key, contents|
          old[tag][key] = (old[tag][key] || []) + contents
        end
      end
      old[:doc] = old[:doc].length > 0 ? old[:doc] : new[:doc]
      # Additionally the doc-comment can contain configs and constructor
@@ -144,7 +149,7 @@ module JsDuck
        :alternateClassNames => [],
        :members => Class.default_members_hash,
        :statics => Class.default_members_hash,
        :meta => [],
        :meta => {},
        :files => [{:filename => "", :linenr => 0}],
      })
    end
+5 −3
Original line number Diff line number Diff line
@@ -158,9 +158,6 @@ module JsDuck
      class_formatter = ClassFormatter.new(@relations, doc_formatter)
      # Don't format types when exporting
      class_formatter.include_types = !@opts.export
      # Inject formatter to all meta-tags.
      @opts.meta_tags.each {|tag| tag.formatter = doc_formatter }
      class_formatter.meta_tags = @opts.meta_tags
      # Format all doc-objects in parallel
      formatted_classes = @parallel.map(@relations.classes) do |cls|
        {
@@ -191,6 +188,11 @@ module JsDuck
    def write_classes
      exporter = Exporter.new(@relations)
      renderer = Renderer.new
      # Inject formatter to all meta-tags.
      doc_formatter = get_doc_formatter
      @opts.meta_tags.each {|tag| tag.formatter = doc_formatter }
      renderer.meta_tags = @opts.meta_tags

      dir = @opts.output_dir + (@opts.export ? "" : "/output")
      @parallel.each(@relations.classes) do |cls|
        filename = dir + "/" + cls[:name] + (@opts.export ? ".json" : ".js")
+0 −12
Original line number Diff line number Diff line
@@ -9,8 +9,6 @@ module JsDuck
  class ClassFormatter
    # Set to false to disable HTML-formatting of type definitions.
    attr_accessor :include_types
    # List of meta-tag implementations
    attr_accessor :meta_tags

    def initialize(relations, formatter)
      @relations = relations
@@ -32,19 +30,9 @@ module JsDuck
      cls[:statics].each_pair do |type, members|
        cls[:statics][type] = members.reject {|m| m[:private] }.map {|m| format_member(m) }
      end
      cls[:meta] = cls[:meta].map {|m| format_meta(m) }.compact
      cls
    end

    def format_meta(meta)
      tag = @meta_tags.find {|tag| tag.name == meta[:name] }
      if tag.hidden
        nil
      else
        {:name => tag.name, :title => tag.title, :doc => tag.transform(meta[:doc])}
      end
    end

    def format_member(m)
      @formatter.doc_context = m[:files][0]
      m[:doc] = @formatter.format(m[:doc]) if m[:doc]
+6 −1
Original line number Diff line number Diff line
@@ -358,7 +358,12 @@ module JsDuck
    end

    def detect_meta(doc_map)
      doc_map[:meta] ? doc_map[:meta].map {|tag| {:name => tag[:name], :doc => tag[:doc]} } : []
      meta = {}
      (doc_map[:meta] || []).map do |tag|
        meta[tag[:name]] = [] unless meta[tag[:name]]
        meta[tag[:name]] << tag[:doc]
      end
      meta
    end

    def detect_deprecated(doc_map)
+12 −11
Original line number Diff line number Diff line
@@ -3,24 +3,25 @@ module JsDuck
  # Abstract base class for all meta tag implementations.
  #
  # Child classes must define value for @name attribute.  They can
  # also provide @title, @hidden, and override #render method.
  # also provide @multiline, and override #to_html method.
  class MetaTag
    # Name of the tag (required)
    attr_reader :name

    # Title to use when rendering the meta-tag info
    attr_reader :title

    # True to include all lines up to next @tag as part of this meta-tag
    attr_reader :multiline

    # True to not render the meta tag at all
    attr_reader :hidden

    # Override this to transform the content of meta-tag in whichever
    # way desired.
    def transform(text)
      markdown(text)
    # Override this to transform the content of meta-tag to HTML to be
    # included into documentation.
    #
    # It gets passed an array of contents gathered from all meta-tags
    # of given type. It should return an HTML string to inject into
    # document.  For help in that it can use the #markdown method to
    # easily support markdown inside the meta-tag.
    #
    # By default the method returns nil, which means the tag will not
    # be rendered at all.
    def to_html(contents)
    end

    # This is used to inject the formatter object for #markdown method
Loading