diff --git a/lib/jsduck/app.rb b/lib/jsduck/app.rb
index 10b8f4ce1b6343daaf4545cf0fe565ad99e5f586..6a1a0619fd9f3e0853800f8dfd99d32b3076b261 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 0000000000000000000000000000000000000000..050f3cb00dd763d953a5990457ff4e9621731ea5
--- /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