diff --git a/lib/jsduck.rb b/lib/jsduck.rb index 4be6643b65e13ff264c19089473b27e00f2b370e..0558b75f4f7e08a942760eadd1b75fada11d8adf 100755 --- a/lib/jsduck.rb +++ b/lib/jsduck.rb @@ -3,6 +3,7 @@ $:.unshift File.dirname(__FILE__) # For running the actual JsDuck app require 'jsduck/aggregator' require 'jsduck/class' require 'jsduck/tree' +require 'jsduck/tree_icons' require 'jsduck/page' require 'json' @@ -58,8 +59,10 @@ module JsDuck # Given array of doc-objects, generates namespace tree and writes in # in JSON form into a file. def self.write_tree(filename, docs) - js = "Docs.classData = " + JSON.generate( Tree.new.create(docs) ) + ";" - js += "Docs.icons = {};" + tree = Tree.new.create(docs) + icons = TreeIcons.new.extract_icons(tree) + js = "Docs.classData = " + JSON.generate( tree ) + ";" + js += "Docs.icons = " + JSON.generate( icons ) + ";" File.open(filename, 'w') {|f| f.write(js) } end diff --git a/lib/jsduck/tree_icons.rb b/lib/jsduck/tree_icons.rb new file mode 100644 index 0000000000000000000000000000000000000000..062304d62526bc83a6f159c907339ca90a511544 --- /dev/null +++ b/lib/jsduck/tree_icons.rb @@ -0,0 +1,19 @@ +module JsDuck + + # Takes the structure produced by JsDuck::Tree.create and creates + # hashmap of classname-icon pairs. + class TreeIcons + def extract_icons(node) + icons = {} + if node[:children] + node[:children].each do |child| + icons.merge!(extract_icons(child)) + end + else + icons[node[:id]] = node[:iconCls] + end + icons + end + end + +end diff --git a/spec/tree_icons_spec.rb b/spec/tree_icons_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..0526438ef536e8a5d71ce68bb9e9da110d8f093f --- /dev/null +++ b/spec/tree_icons_spec.rb @@ -0,0 +1,75 @@ +require "jsduck/tree_icons" + +describe JsDuck::TreeIcons do + + before do + @icons = JsDuck::TreeIcons.new.extract_icons({ + :id => "apidocs", + :iconCls => "icon-docs", + :text => "API Documentation", + :singleClickExpand => true, + :children => [ + { + :id => "pkg-SamplePackage", + :text => "SamplePackage", + :iconCls => "icon-pkg", + :cls => "package", + :singleClickExpand => true, + :children => [ + { + :href => "output/SamplePackage.Component.html", + :text => "Component", + :id => "SamplePackage.Component", + :isClass => true, + :iconCls => "icon-cmp", + :cls => "cls", + :leaf => true + }, + { + :href => "output/SamplePackage.Singleton.html", + :text => "Singleton", + :id => "SamplePackage.Singleton", + :isClass => true, + :iconCls => "icon-static", + :cls => "cls", + :leaf => true + }, + { + :id => "pkg-SamplePackage", + :text => "sub", + :iconCls => "icon-pkg", + :cls => "package", + :singleClickExpand => true, + :children => [ + { + :href => "output/SamplePackage.sub.Foo.html", + :text => "Foo", + :id => "SamplePackage.sub.Foo", + :isClass => true, + :iconCls => "icon-cls", + :cls => "cls", + :leaf => true + }, + ] + } + ] + } + ] + }) + end + + it "extracts as many icons as there are classes in tree" do + @icons.length.should == 3 + end + + it "extracts icons inside a package" do + @icons["SamplePackage.Component"].should == "icon-cmp" + @icons["SamplePackage.Singleton"].should == "icon-static" + end + + it "extracts icons inside all subpackages too" do + @icons["SamplePackage.sub.Foo"].should == "icon-cls" + end + +end +