Commit 1bcc1af8 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

JSON export functionality.

Integrated JSON export initially implemented by Nick Poulden.

By using the --json option jsduck exports docs for each class in
JSON format to the output directory.  Each exported class will
also contain all the inherited members.
parent 5d17bde3
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -34,6 +34,10 @@ opts = OptionParser.new do | opts |
    app.template_dir = path
  end

  opts.on('--json', "Produces JSON export instead of HTML documentation.") do |path|
    app.export = :json
  end

  opts.on('-v', '--verbose', "This will fill up your console.") do
    app.verbose = true
  end
+39 −8
Original line number Diff line number Diff line
@@ -22,25 +22,37 @@ module JsDuck
    attr_accessor :template_dir
    attr_accessor :input_files
    attr_accessor :verbose
    attr_accessor :export

    def initialize
      @output_dir = nil
      @template_dir = nil
      @input_files = []
      @verbose = false
      @export = nil
      @timer = Timer.new
    end

    # Call this after input parameters set
    def run
      clear_dir(@output_dir)
      if @export
        FileUtils.mkdir(@output_dir)
      else
        copy_template(@template_dir, @output_dir)
      end

      parsed_files = @timer.time(:parsing) { parallel_parse(@input_files) }
      result = @timer.time(:aggregating) { aggregate(parsed_files) }
      classes = @timer.time(:aggregating) { filter_classes(result) }

      if @export == :json
        @timer.time(:generating) { write_json(@output_dir, classes) }
      else
        @timer.time(:generating) { write_tree(@output_dir+"/output/tree.js", classes) }
        @timer.time(:generating) { write_members(@output_dir+"/output/members.js", classes) }
        @timer.time(:generating) { write_pages(@output_dir+"/output", classes) }
      end

      @timer.report if @verbose
    end
@@ -53,7 +65,7 @@ module JsDuck
        code = IO.read(fname)
        {
          :filename => fname,
          :html_filename => File.basename(src.write(code, fname)),
          :html_filename => @export ? "" : File.basename(src.write(code, fname)),
          :data => Parser.new(code).parse,
        }
      end
@@ -118,15 +130,34 @@ module JsDuck
      end
    end

    # Writes JSON export file for each class
    def write_json(path, docs)
      subclasses = Subclasses.new(docs)
      cache = {}
      Parallel.each(docs) do |cls|
        filename = path + "/" + cls[:name] + ".json"
        puts "Writing to #{filename} ..." if @verbose
        hash = cls.to_hash
        if hash[:doc]
          hash[:doc] = DocFormatter.new(hash[:name]).format(hash[:doc])
        end
        json = JSON.pretty_generate(hash)
        File.open(filename, 'w') {|f| f.write(json) }
      end
    end

    def copy_template(template_dir, dir)
      puts "Copying template files to #{dir}..." if @verbose
      if File.exists?(dir)
        FileUtils.rm_r(dir)
      end
      FileUtils.cp_r(template_dir, dir)
      FileUtils.mkdir(dir + "/output")
      FileUtils.mkdir(dir + "/source")
    end

    def clear_dir(dir)
      if File.exists?(dir)
        FileUtils.rm_r(dir)
      end
    end
  end

end
+17 −0
Original line number Diff line number Diff line
@@ -34,6 +34,23 @@ 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.
    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
    end

    # Returns true when this class inherits from the specified class.
    # Also returns true when the class itself is the one we are asking about.
    def inherits_from?(class_name)