From bfb3a014e7ed17ac1902ab7a291f12aee544f1dd Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Mon, 27 Jun 2011 11:21:07 +0300 Subject: [PATCH] Refactor guides parsing to separate class. --- lib/jsduck/app.rb | 19 +++--------- lib/jsduck/guides.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 lib/jsduck/guides.rb diff --git a/lib/jsduck/app.rb b/lib/jsduck/app.rb index 10b8f4ce..6a1a0619 100644 --- a/lib/jsduck/app.rb +++ b/lib/jsduck/app.rb @@ -14,6 +14,7 @@ require 'jsduck/exporter' require 'jsduck/timer' require 'jsduck/parallel_wrap' require 'jsduck/logger' +require 'jsduck/guides' require 'json' require 'fileutils' @@ -261,21 +262,9 @@ module JsDuck # Writes JsonP export file for each guide def write_guides(in_path, out_path, relations) - formatter = get_doc_formatter(relations) - FileUtils.mkdir(out_path) - Dir.glob(in_path + "/*").each do |in_dir| - if File.directory?(in_dir) - guide_name = File.basename(in_dir) - out_dir = out_path + "/" + guide_name - Logger.instance.log("Creating guide #{out_dir} ...") - FileUtils.cp_r(in_dir, out_dir) - formatter.doc_context = {:filename => out_dir + "/README.md", :linenr => 0} - guide = formatter.format(IO.read(out_dir + "/README.md")) - guide.gsub!(/ guide}) - FileUtils.rm(out_dir + "/README.md") - end - end + guides = Guides.new(get_doc_formatter(relations)) + guides.parse_dir(in_path) + guides.write(out_path) end # Creates and initializes DocFormatter diff --git a/lib/jsduck/guides.rb b/lib/jsduck/guides.rb new file mode 100644 index 00000000..050f3cb0 --- /dev/null +++ b/lib/jsduck/guides.rb @@ -0,0 +1,69 @@ +require 'jsduck/logger' +require 'fileutils' +require 'json' + +module JsDuck + + # Reads in guides and converts them to JsonP files + class Guides + def initialize(formatter) + @guides = [] + @formatter = formatter + end + + # Looks for guide in each subdir of given directory + def parse_dir(guides_dir) + Dir.glob(guides_dir + "/*").each do |dir| + if File.directory?(dir) + parse_guide(dir) + end + end + end + + def parse_guide(dir) + guide_file = dir + "/README.md" + unless File.exists?(guide_file) + return Logger.instance.warn("README.md not found in #{dir}") + end + + Logger.instance.log("Parsing guide #{guide_file} ...") + markdown = IO.read(guide_file) + name = File.basename(dir) + # Treat the first line of file as title + title = markdown.match(/^([^\n]*?)(\r?\n|$)/)[1], + + @formatter.doc_context = {:filename => guide_file, :linenr => 0} + html = @formatter.format(markdown) + html.gsub!(/ dir, + :name => name, + :title => title, + :icon => File.exists?(dir+"/icon.png") ? dir+"/icon.png" : nil, + :html => html, + } + end + + # Writes all guides to given dir in JsonP format + def write(dir) + FileUtils.mkdir(dir) + @guides.each do |guide| + 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]}) + 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 -- GitLab