From 74ca8742327ea20a4c16f2cf2642ba2dc5999689 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Mon, 14 Nov 2011 14:23:05 +0200 Subject: [PATCH] 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. --- lib/jsduck/api_exporter.rb | 55 ++++++++++++++++++++++++++++++++++++++ lib/jsduck/app.rb | 11 ++++++++ lib/jsduck/options.rb | 5 ++-- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 lib/jsduck/api_exporter.rb diff --git a/lib/jsduck/api_exporter.rb b/lib/jsduck/api_exporter.rb new file mode 100644 index 00000000..7ba6a15a --- /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 ce5fbc04..1210ed0d 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 4e510de5..9d9e29cb 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 -- GitLab