diff --git a/lib/jsduck/api_exporter.rb b/lib/jsduck/api_exporter.rb new file mode 100644 index 0000000000000000000000000000000000000000..7ba6a15af1029090e812ee404735fd44c910dc39 --- /dev/null +++ b/lib/jsduck/api_exporter.rb @@ -0,0 +1,55 @@ +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 diff --git a/lib/jsduck/app.rb b/lib/jsduck/app.rb index ce5fbc049c9b9fafcdc1ac4ed1653a8fadb33262..1210ed0dc231dca455c9cf27344e02667652ce0b 100644 --- a/lib/jsduck/app.rb +++ b/lib/jsduck/app.rb @@ -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 diff --git a/lib/jsduck/options.rb b/lib/jsduck/options.rb index 4e510de5c560806dc729a93f4c5842692d8ea963..9d9e29cbee810100227ad4e5e5103cb48131d05b 100644 --- a/lib/jsduck/options.rb +++ b/lib/jsduck/options.rb @@ -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