Class: JsDuck::Guides
- Inherits:
-
Object
- Object
- JsDuck::Guides
- Defined in:
- lib/jsduck/guides.rb
Overview
Reads in guides and converts them to JsonP files
Class Method Summary collapse
-
.create(filename, formatter) ⇒ Object
Creates Guides object from filename and formatter.
Instance Method Summary collapse
-
#add_toc(guide, html) ⇒ Object
Creates table of contents at the top of guide by looking for <h2> elements in HTML.
-
#initialize(filename, formatter) ⇒ Guides
constructor
Parses guides config file.
-
#to_array ⇒ Object
Returns all guides as array.
-
#to_html ⇒ Object
Returns HTML listing of guides.
-
#write(dir) ⇒ Object
Writes all guides to given dir in JsonP format.
- #write_guide(guide, dir) ⇒ Object
Constructor Details
Class Method Details
.create(filename, formatter) ⇒ Object
Creates Guides object from filename and formatter
11 12 13 14 15 16 17 |
# File 'lib/jsduck/guides.rb', line 11 def self.create(filename, formatter) if filename Guides.new(filename, formatter) else NullObject.new(:to_array => [], :to_html => "") end end |
Instance Method Details
#add_toc(guide, html) ⇒ Object
Creates table of contents at the top of guide by looking for <h2> elements in HTML.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/jsduck/guides.rb', line 64 def add_toc(guide, html) toc = [ "<p><strong>Contents</strong></p>\n", "<ul class='toc'>\n", ] new_html = [] i = 0 html.each_line do |line| if line =~ /^<h2>(.*)<\/h2>$/ i += 1 toc << "<li><a href='#!/guide/#{guide['name']}-section-#{i}'>#{$1}</a></li>\n" new_html << "<h2 id='#{guide['name']}-section-#{i}'>#{$1}</h2>\n" else new_html << line end end toc << "</ul>\n" # Inject TOC at below first heading new_html.insert(1, toc) new_html.flatten.join end |
#to_array ⇒ Object
Returns all guides as array
87 88 89 |
# File 'lib/jsduck/guides.rb', line 87 def to_array @guides end |
#to_html ⇒ Object
Returns HTML listing of guides
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/jsduck/guides.rb', line 92 def to_html html = @guides.map do |group| [ "<h3>#{group['title']}</h3>", "<ul>", group["items"].map {|g| "<li><a href='#!/guide/#{g['name']}'>#{g['title']}</a></li>" }, "</ul>", ] end.flatten.join("\n") return <<-EOHTML <div id='guides-content' style='display:none'> #{html} </div> EOHTML end |
#write(dir) ⇒ Object
Writes all guides to given dir in JsonP format
27 28 29 30 31 32 33 |
# File 'lib/jsduck/guides.rb', line 27 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 |
#write_guide(guide, dir) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jsduck/guides.rb', line 35 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 = add_toc(guide, @formatter.format(IO.read(guide_file))) JsonDuck.write_jsonp(out_dir+"/README.js", name, {:guide => html, :title => guide["title"]}) end |