-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
257 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module JsDuck | ||
|
||
# Splits array of items with subitems into roughly equal size groups. | ||
class Columns | ||
# Initialized with the name of subitems field. | ||
def initialize(subitems_field) | ||
@header_size = 3 | ||
@subitems_field = subitems_field | ||
end | ||
|
||
# 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. | ||
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 | ||
|
||
private | ||
|
||
def max_sum(cols) | ||
cols.map {|col| sum(col) }.max | ||
end | ||
|
||
# Finds the total size of items in array | ||
# | ||
# The size of one item is it's number of classes + the space for header | ||
def sum(arr) | ||
arr.reduce(0) {|sum, item| sum + item[@subitems_field].length + @header_size } | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
require 'jsduck/util/null_object' | ||
require 'jsduck/columns' | ||
|
||
module JsDuck | ||
|
||
class News | ||
# Creates News object from relations data when --import option | ||
# specified. | ||
def self.create(relations, doc_formatter, opts) | ||
if opts[:imports].length > 0 | ||
News.new(relations, doc_formatter) | ||
else | ||
Util::NullObject.new(:to_html => "") | ||
end | ||
end | ||
|
||
# Generates list of new classes & members in this version. | ||
def initialize(relations, doc_formatter) | ||
@doc_formatter = doc_formatter | ||
@columns = Columns.new(:members) | ||
@new_items = filter_new_items(relations) | ||
end | ||
|
||
# Returns the HTML | ||
def to_html(style="") | ||
return [ | ||
"<div id='news-content' style='#{style}'>", | ||
"<div class='section'>", | ||
"<h1>New in this version</h1>", | ||
render_columns(@new_items), | ||
"<div style='clear:both'></div>", | ||
"</div>", | ||
"</div>", | ||
].flatten.join("\n") | ||
end | ||
|
||
private | ||
|
||
def filter_new_items(relations) | ||
classes = [] | ||
new_items = [] | ||
relations.each do |cls| | ||
if !cls[:private] | ||
if cls[:new] | ||
classes << cls | ||
else | ||
group = {:name => cls[:name], :members => [], :new => cls[:new]} | ||
cls.all_local_members.each do |m| | ||
group[:members] << m if m[:new] && !m[:private] && !m[:hide] | ||
end | ||
group[:members] = discard_accessors(group[:members]) | ||
new_items << group if group[:members].length > 0 | ||
end | ||
end | ||
end | ||
|
||
new_items.sort! {|a, b| a[:name] <=> b[:name] } | ||
|
||
# Place the new classes section at the beginning | ||
if classes.length > 0 | ||
new_items.unshift({:name => "New classes", :members => classes}) | ||
end | ||
|
||
new_items | ||
end | ||
|
||
def discard_accessors(members) | ||
accessors = {} | ||
members.find_all {|m| m[:accessor] }.each do |cfg| | ||
accessors["set" + upcase_first(cfg[:name])] = true | ||
accessors["get" + upcase_first(cfg[:name])] = true | ||
accessors[cfg[:name].downcase + "change"] = true if cfg[:evented] | ||
end | ||
|
||
members.reject {|m| accessors[m[:name]] } | ||
end | ||
|
||
def upcase_first(str) | ||
str[0,1].upcase + str[1..-1] | ||
end | ||
|
||
def render_columns(new_items) | ||
align = ["left-column", "middle-column", "right-column"] | ||
i = -1 | ||
return @columns.split(new_items, 3).map do |col| | ||
i += 1 | ||
[ | ||
"<div class='#{align[i]}'>", | ||
render_col(col), | ||
"</div>", | ||
] | ||
end | ||
end | ||
|
||
def render_col(col) | ||
return col.map do |item| | ||
[ | ||
"<h3>#{item[:name]}</h3>", | ||
"<ul class='links'>", | ||
item[:members].map {|m| "<li>" + link(m) + "</li>" }, | ||
"</ul>", | ||
] | ||
end | ||
end | ||
|
||
def link(m) | ||
if m[:tagname] == :class | ||
@doc_formatter.link(m[:name], nil, m[:name]) | ||
else | ||
@doc_formatter.link(m[:owner], m[:name], m[:name], m[:tagname], m[:static]) | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
Oops, something went wrong.