Class: JsDuck::Categories
- Inherits:
-
Object
- Object
- JsDuck::Categories
- Defined in:
- lib/jsduck/categories.rb
Overview
Reads in categories and outputs them as HTML div
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(categories, doc_formatter, relations = {}) ⇒ Categories
constructor
A new instance of Categories.
- #max_sum(cols) ⇒ Object
- #render_columns(groups) ⇒ Object
- #render_groups(groups) ⇒ Object
-
#split(items, n) ⇒ Object
Splits the array of items into n chunks so that the sum of largest chunk is as small as possible.
-
#sum(arr) ⇒ Object
Finds the total size of items in array.
-
#to_html ⇒ Object
Returns HTML listing of classes divided into categories.
Constructor Details
#initialize(categories, doc_formatter, relations = {}) ⇒ Categories
Returns a new instance of Categories.
19 20 21 22 23 |
# File 'lib/jsduck/categories.rb', line 19 def initialize(categories, doc_formatter, relations={}) @categories = categories @doc_formatter = doc_formatter @relations = relations end |
Class Method Details
.create(filename, doc_formatter, relations) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/jsduck/categories.rb', line 10 def self.create(filename, doc_formatter, relations) if filename categories = FileCategories.new(filename, relations) else categories = AutoCategories.new(relations) end Categories.new(categories.generate, doc_formatter, relations) end |
Instance Method Details
#max_sum(cols) ⇒ Object
98 99 100 |
# File 'lib/jsduck/categories.rb', line 98 def max_sum(cols) cols.map {|col| sum(col) }.max end |
#render_columns(groups) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jsduck/categories.rb', line 44 def render_columns(groups) align = ["left-column", "middle-column", "right-column"] i = -1 return split(groups, 3).map do |col| i += 1 [ "<div class='#{align[i]}'>", render_groups(col), "</div>", ] end end |
#render_groups(groups) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jsduck/categories.rb', line 57 def render_groups(groups) return groups.map do |g| [ "<h3>#{g['name']}</h3>", "<div class='links'>", g["classes"].map {|cls| @relations[cls] ? @doc_formatter.link(cls, nil, cls) : cls }, "</div>", ] end end |
#split(items, n) ⇒ Object
Splits the array of items into n chunks so that the sum of largest chunk is as small as possible.
This is a brute-force implementation - we just try all the combinations and choose the best one.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/jsduck/categories.rb', line 73 def split(items, n) if n == 1 [items] elsif items.length <= n Array.new(n) {|i| items[i] ? [items[i]] : [] } else min_max = nil min_arr = nil i = 0 while i <= items.length-n i += 1 # Try placing 1, 2, 3, ... items to first chunk. # Calculate the remaining chunks recursively. cols = [items[0,i]] + split(items[i, items.length], n-1) max = max_sum(cols) # Is this the optimal solution so far? Remember it. if !min_max || max < min_max min_max = max min_arr = cols end end min_arr end end |
#sum(arr) ⇒ Object
Finds the total size of items in array
The size of one item is it’s number of classes + the space for header
105 106 107 108 |
# File 'lib/jsduck/categories.rb', line 105 def sum(arr) header_size = 3 arr.reduce(0) {|sum, item| sum + item["classes"].length + header_size } end |
#to_html ⇒ Object
Returns HTML listing of classes divided into categories
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jsduck/categories.rb', line 26 def to_html html = @categories.map do |category| [ "<div class='section'>", "<h1>#{category['name']}</h1>", render_columns(category['groups']), "<div style='clear:both'></div>", "</div>", ] end.flatten.join("\n") return <<-EOHTML <div id='categories-content' style='display:none'> #{html} </div> EOHTML end |