Commit 74ca8742 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add new export format: --export=api.

This is similar to json export format, with the difference that
only class and method names are included to exported JSON.
parent 23fdd261
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
require 'jsduck/json_duck'
require 'jsduck/class'

module JsDuck

  # Exporter for simple JSON format listing only class name and names
  # of all of its members.
  #
  # It produces the following structure:
  #
  # {
  #   :name => "Panel",
  #   :members => {
  #     :cfg => ["width", "height", "title"],
  #     :method => ["getWidth", "setWidth"],
  #     ...
  #   },
  #   :statics => { ... }
  # }
  #
  class ApiExporter
    def initialize(relations, opts)
      # All params ignored, they're present to be compatible with
      # other exporters.
    end

    # Extension for filename
    def extension
      ".json"
    end

    # Writes full class data in JSON format to file
    def write(filename, cls)
      JsonDuck.write_json(filename, export(cls))
    end

    def export(cls)
      {
        :name => cls[:name],
        :members => export_members(cls, :members),
        :statics => export_members(cls, :statics),
      }
    end

    def export_members(cls, context)
      h = {}
      Class.default_members_hash.each_key do |type|
        h[type] = cls.members(type, context).map {|m| m[:name] }
      end
      h
    end

  end

end
+11 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ require 'jsduck/class_writer'
require 'jsduck/source_writer'
require 'jsduck/app_data'
require 'jsduck/index_html'
require 'jsduck/api_exporter'
require 'jsduck/json_exporter'
require 'jsduck/json_p_exporter'
require 'fileutils'
@@ -63,6 +64,16 @@ module JsDuck
          cw = ClassWriter.new(JsonExporter, @relations, @opts)
          cw.write(@opts.output_dir)
        end
      elsif @opts.export == :api
        format_classes
        if @opts.output_dir == :stdout
          exporter = ApiExporter.new({}, {})
          puts JsonDuck.generate(@relations.classes.map {|cls| exporter.export(cls) })
        else
          FileUtils.rm_rf(@opts.output_dir)
          cw = ClassWriter.new(ApiExporter, @relations, @opts)
          cw.write(@opts.output_dir)
        end
      else
        FileUtils.rm_rf(@opts.output_dir)
        TemplateDir.new(@opts).write
+3 −2
Original line number Diff line number Diff line
@@ -256,7 +256,8 @@ module JsDuck

        opts.on('--export=FORMAT',
          "Instead of HTML docs, exports docs in FORMAT:",
          "* json - JSON export of all docs.", " ") do |format|
          "* json - JSON export of all docs.",
          "* api  - JSON of only class- and member names.", " ") do |format|
          @export = format.to_sym
        end

@@ -408,7 +409,7 @@ module JsDuck
      elsif @output_dir == :stdout && !@export
        puts "Output to STDOUT only works when using --export option."
        exit(1)
      elsif ![nil, :json].include?(@export)
      elsif ![nil, :json, :api].include?(@export)
        puts "Unknown export format: #{@export}"
        exit(1)
      elsif @output_dir != :stdout