Commit 517b4eab authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Exporter class that converts @links and Markdown.

Moved the former Class#to_hash functionality entirely there.
parent 5ee7be8c
Loading
Loading
Loading
Loading
+3 −8
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ require 'jsduck/tree_icons'
require 'jsduck/members'
require 'jsduck/relations'
require 'jsduck/page'
require 'jsduck/exporter'
require 'jsduck/timer'
require 'json'
require 'fileutils'
@@ -132,17 +133,11 @@ module JsDuck

    # Writes JSON export file for each class
    def write_json(path, relations)
      cache = {}
      formatter = DocFormatter.new
      formatter.cssClass = 'docClass'
      exporter = Exporter.new(relations)
      Parallel.each(relations.classes) do |cls|
        filename = path + "/" + cls[:name] + ".json"
        puts "Writing to #{filename} ..." if @verbose
        hash = cls.to_hash
        if hash[:doc]
          formatter.context = cls[:name]
          hash[:doc] = formatter.format(hash[:doc])
        end
        hash = exporter.export(cls)
        json = JSON.pretty_generate(hash)
        File.open(filename, 'w') {|f| f.write(json) }
      end
+2 −18
Original line number Diff line number Diff line
@@ -46,25 +46,9 @@ module JsDuck
      end
    end

    # Returns all data in Class object as hash.  This is basically the
    # same as just accessing @doc, except instead of :cfg field there
    # is :cfgs which also contains the inherited ones.  Same for
    # :properties, :methods, and :events.
    # Returns copy of @doc hash
    def to_hash
      doc = @doc.clone
      doc[:cfgs] = members(:cfg)
      doc[:properties] = members(:property)
      doc[:methods] = members(:method)
      doc[:events] = members(:event)
      doc.delete(:cfg)
      doc.delete(:property)
      doc.delete(:method)
      doc.delete(:event)
      doc[:component] = inherits_from?("Ext.Component")
      doc[:superclasses] = superclasses.collect {|cls| cls.full_name }
      doc[:subclasses] = @relations.subclasses(self).collect {|cls| cls.full_name }
      doc[:mixedInto] = @relations.mixed_into(self).collect {|cls| cls.full_name }
      doc
      @doc.clone
    end

    # Returns true when this class inherits from the specified class.

lib/jsduck/exporter.rb

0 → 100644
+72 −0
Original line number Diff line number Diff line
require 'jsduck/doc_formatter'

module JsDuck

  # Export class data as hash with :cfg being replace with :cfgs and
  # including all the inherited members too.  Same for :properties,
  # :methods, and :events.
  #
  # Also all the :doc elements will be formatted - converted from
  # markdown to HTML and @links resolved.
  class Exporter
    attr_accessor :relations

    def initialize(relations)
      @relations = relations
      @formatter = DocFormatter.new
      @formatter.cssClass = 'docClass'
    end

    # Returns all data in Class object as hash.
    def export(cls)
      h = cls.to_hash
      h[:cfgs] = cls.members(:cfg)
      h[:properties] = cls.members(:property)
      h[:methods] = cls.members(:method)
      h[:events] = cls.members(:event)
      h.delete(:cfg)
      h.delete(:property)
      h.delete(:method)
      h.delete(:event)
      h[:component] = cls.inherits_from?("Ext.Component")
      h[:superclasses] = cls.superclasses.collect {|c| c.full_name }
      h[:subclasses] = @relations.subclasses(cls).collect {|c| c.full_name }
      h[:mixedInto] = @relations.mixed_into(cls).collect {|c| c.full_name }
      format_class(h)
    end

    # converts :doc properties from markdown to html and resolve @links
    def format_class(c)
      @formatter.context = c[:name]
      c[:doc] = @formatter.format(c[:doc]) if c[:doc]
      [:cfgs, :properties, :methods, :events].each do |type|
        c[type] = c[type].map {|m| format_member(m) }
      end
      c
    end

    def format_member(m)
      m = m.clone
      m[:doc] = @formatter.format(m[:doc]) if m[:doc]
      m[:params] = format_params(m[:params]) if m[:params]
      m[:return] = format_return(m[:return]) if m[:return]
      m
    end

    def format_params(params)
      params.map do |p|
        p = p.clone
        p[:doc] = @formatter.format(p[:doc]) if p[:doc]
        p
      end
    end

    def format_return(r)
      r = r.clone
      r[:doc] = @formatter.format(r[:doc]) if r[:doc]
      r
    end

  end

end