require 'jsduck/logger' require 'jsduck/json_duck' require 'jsduck/null_object' require 'fileutils' module JsDuck # Reads in guides and converts them to JsonP files class Guides # Creates Guides object from filename and formatter def self.create(filename, formatter) if filename Guides.new(filename, formatter) else NullObject.new(:to_array => [], :to_html => "") end end # Parses guides config file def initialize(filename, formatter) @path = File.dirname(filename) @guides = JsonDuck.read(filename) @formatter = formatter end # Writes all guides to given dir in JsonP format def write(dir) FileUtils.mkdir(dir) unless File.exists?(dir) @guides.each {|group| group["items"].each {|g| write_guide(g, dir) } } # Write the JSON to output dir, so it's available in released # version of docs and people can use it with JSDuck by themselves. JsonDuck.write_json(dir+"/guides.json", @guides) end def write_guide(guide, dir) guide_dir = @path + "/guides/" + guide["name"] tutorial_dir = @path + "/tutorials/" + guide["name"] out_dir = dir + "/" + guide["name"] if File.exists?(guide_dir) in_dir = guide_dir elsif File.exists?(tutorial_dir) in_dir = tutorial_dir else return Logger.instance.warn(:guide, "Guide #{guide_dir} / #{tutorial_dir} not found") end guide_file = in_dir + "/README.md" return Logger.instance.warn(:guide, "README.md not found in #{in_dir}") unless File.exists?(guide_file) Logger.instance.log("Writing guide", out_dir) # Copy the whole guide dir over FileUtils.cp_r(in_dir, out_dir) @formatter.doc_context = {:filename => guide_file, :linenr => 0} name = File.basename(in_dir) @formatter.img_path = "guides/#{name}" html = @formatter.format(IO.read(guide_file)) JsonDuck.write_jsonp(out_dir+"/README.js", name, {:guide => html, :title => guide["title"]}) end # Returns all guides as array def to_array @guides end # Returns HTML listing of guides def to_html html = @guides.map do |group| [ "

#{group['title']}

", "", ] end.flatten.join("\n") return <<-EOHTML EOHTML end end end