From e5ffa377e31063bf3b4b3022b8e04de4212c57da Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sat, 5 Mar 2011 02:35:10 +0200 Subject: [PATCH] Simplified Members class. Completely removed the unused sorting functionality. Removed the @root variable. Only local variable used inside create(). Generalized the logic in add_class and moved it entirely into create(). create() now returns array. The surrounding hash is added inside App. Renamed expandable_desc() to short_desc() - nothing expandable in it. Few more small variable renames. Replaced comments with more accurate ones. --- lib/jsduck/app.rb | 2 +- lib/jsduck/members.rb | 78 ++++++++++++------------------------------- 2 files changed, 22 insertions(+), 58 deletions(-) diff --git a/lib/jsduck/app.rb b/lib/jsduck/app.rb index a3a3ce03..9abbb38c 100644 --- a/lib/jsduck/app.rb +++ b/lib/jsduck/app.rb @@ -101,7 +101,7 @@ module JsDuck # in JSON form into a file. def write_members(filename, docs) members = Members.new.create(docs) - js = "Docs.membersData = " + JSON.generate( members ) + ";" + js = "Docs.membersData = " + JSON.generate( {:data => members} ) + ";" File.open(filename, 'w') {|f| f.write(js) } end diff --git a/lib/jsduck/members.rb b/lib/jsduck/members.rb index e98003b5..7ededaa3 100644 --- a/lib/jsduck/members.rb +++ b/lib/jsduck/members.rb @@ -1,75 +1,39 @@ module JsDuck - # Creator of package-tree in the format expected by the - # documentation browser UI. - # - # See unit test for example output. - # + # Creates list of all members in all classes that is used by the + # searching feature in UI. class Members - def initialize - @root = { - :data => [] - } - end - - # Given list of class documentation objects returns a - # tree-structure that can be turned into JSON that's needed by - # documentation browser interface. + # Given list of class documentation objects returns an array of + # hashes describing all the members. def create(docs) - docs.each {|cls| add_class(cls) } - #sort_tree(@root) - @root - end - - # Sorts all child nodes, and recursively all child packages. - def sort_tree(node) - node[:data].sort! {|a,b| compare(a, b) } - end - - # Comparson method that sorts package nodes before class nodes. - def compare(a, b) - if a[:cls] == b[:cls] - a[:member] <=> b[:member] - else - a[:cls] <=> b[:cls] - end - end - - # When package for the class exists, add class node to that - # package; otherwise create the package first. - def add_class(cls) - cls.members(:cfg).each do |m| - @root[:data] << member_node(m, cls) - end - cls.members(:property).each do |m| - @root[:data] << member_node(m, cls) - end - cls.members(:method).each do |m| - @root[:data] << member_node(m, cls) - end - cls.members(:event).each do |m| - @root[:data] << member_node(m, cls) + list = [] + docs.each do |cls| + [:cfg, :property, :method, :event].each do |type| + cls.members(type).each do |m| + list << member_node(m, cls) + end + end end + list end - # Given full doc object for class creates class node - def member_node(m, cls) - #puts "DBG: #{m}" + # Creates structure representing one member + def member_node(member, cls) return { - :cls => "#{cls.full_name}", - :member => m[:name], - :type => m[:tagname], - :doc => expandable_desc(m[:doc]) + :cls => cls.full_name, + :member => member[:name], + :type => member[:tagname], + :doc => short_desc(member[:doc]) } end - def expandable_desc(p_doc) - tagless = first_sentence(strip_tags(strip_links(p_doc))) + def short_desc(str) + tagless = first_sentence(strip_tags(strip_links(str))) if tagless.length > 120 short_doc = tagless[0..116] ellipsis = tagless.length > short_doc.length ? "..." : "" - "#{short_doc}#{ellipsis}" + tagless[0..116] + ellipsis else tagless end -- GitLab