Commit 03fa8124 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract class for creating JsonP files.

parent bfb3a014
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ require 'jsduck/timer'
require 'jsduck/parallel_wrap'
require 'jsduck/logger'
require 'jsduck/guides'
require 'jsduck/jsonp'
require 'json'
require 'fileutils'

@@ -244,7 +245,7 @@ module JsDuck
      @parallel.each(relations.classes) do |cls|
        filename = path + "/" + cls[:name] + ".js"
        Logger.instance.log("Writing to #{filename} ...")
        write_jsonp_file(filename, cls[:name].gsub(/\./, "_"), exporter.export(cls))
        JsonP.write(filename, cls[:name].gsub(/\./, "_"), exporter.export(cls))
      end
    end

@@ -276,13 +277,6 @@ module JsDuck
      formatter
    end

    # Turns hash into JSON and writes inside JavaScript that calls the
    # given callback name
    def write_jsonp_file(filename, callback_name, data)
      jsonp = "Ext.data.JsonP." + callback_name + "(" + JSON.pretty_generate(data) + ");"
      File.open(filename, 'w') {|f| f.write(jsonp) }
    end

    def copy_template(template_dir, dir)
      Logger.instance.log("Copying template files to #{dir}...")
      FileUtils.cp_r(template_dir, dir)
+2 −9
Original line number Diff line number Diff line
require 'jsduck/jsonp'
require 'jsduck/logger'
require 'fileutils'
require 'json'

module JsDuck

@@ -52,18 +52,11 @@ module JsDuck
        out_dir = dir+"/"+guide[:name]
        FileUtils.cp_r(guide[:dir], out_dir)
        # Write the JsonP file and remove the original Markdown file
        write_jsonp_file(out_dir+"/README.js", guide[:name], {:guide => guide[:html]})
        JsonP.write(out_dir+"/README.js", guide[:name], {:guide => guide[:html]})
        FileUtils.rm(out_dir + "/README.md")
      end
    end

    # Turns hash into JSON and writes inside JavaScript that calls the
    # given callback name
    def write_jsonp_file(filename, callback_name, data)
      jsonp = "Ext.data.JsonP." + callback_name + "(" + JSON.pretty_generate(data) + ");"
      File.open(filename, 'w') {|f| f.write(jsonp) }
    end

  end

end

lib/jsduck/jsonp.rb

0 → 100644
+17 −0
Original line number Diff line number Diff line
require 'json'

module JsDuck

  # Utility class for writing JsonP files
  class JsonP

    # Turns hash into JSON and writes inside JavaScript that calls the
    # given callback name
    def self.write(filename, callback_name, data)
      jsonp = "Ext.data.JsonP." + callback_name + "(" + JSON.pretty_generate(data) + ");"
      File.open(filename, 'w') {|f| f.write(jsonp) }
    end

  end

end