Namespace RDoc::DOT. Clean up formatting of RDoc::Diagrams.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14928 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
drbrain 2008-01-07 07:06:59 +00:00
parent 4b93d732e4
commit 9ad5f854e7
3 changed files with 83 additions and 74 deletions

View File

@ -1,3 +1,9 @@
Mon Jan 7 16:06:09 2008
* lib/rdoc/dot.rb: Namespace under RDoc.
* lib/rdoc/diagram.rb: Clean up formatting.
Mon Jan 7 15:51:35 2008 Eric Hodel <drbrain@segment7.net> Mon Jan 7 15:51:35 2008 Eric Hodel <drbrain@segment7.net>
* lib/rdoc/options.rb: Convert to OptionParser, clean up -h output, * lib/rdoc/options.rb: Convert to OptionParser, clean up -h output,

View File

@ -30,8 +30,9 @@ module RDoc
DOT_PATH = "dot" DOT_PATH = "dot"
# Pass in the set of top level objects. The method also creates ##
# the subdirectory to hold the images # Pass in the set of top level objects. The method also creates the
# subdirectory to hold the images
def initialize(info, options) def initialize(info, options)
@info = info @info = info
@ -41,9 +42,10 @@ module RDoc
@diagram_cache = {} @diagram_cache = {}
end end
# Draw the diagrams. We traverse the files, drawing a diagram for ##
# each. We also traverse each top-level class and module in that # Draw the diagrams. We traverse the files, drawing a diagram for each. We
# file drawing a diagram for these too. # also traverse each top-level class and module in that file drawing a
# diagram for these too.
def draw def draw
unless @options.quiet unless @options.quiet
@ -56,25 +58,25 @@ module RDoc
@local_names = find_names(i) @local_names = find_names(i)
@global_names = [] @global_names = []
@global_graph = graph = DOT::Digraph.new('name' => 'TopLevel', @global_graph = graph = DOT::Digraph.new('name' => 'TopLevel',
'fontname' => FONT, 'fontname' => FONT,
'fontsize' => '8', 'fontsize' => '8',
'bgcolor' => 'lightcyan1', 'bgcolor' => 'lightcyan1',
'compound' => 'true') 'compound' => 'true')
# it's a little hack %) i'm too lazy to create a separate class # it's a little hack %) i'm too lazy to create a separate class
# for default node # for default node
graph << DOT::Node.new('name' => 'node', graph << DOT::Node.new('name' => 'node',
'fontname' => FONT, 'fontname' => FONT,
'color' => 'black', 'color' => 'black',
'fontsize' => 8) 'fontsize' => 8)
i.modules.each do |mod| i.modules.each do |mod|
draw_module(mod, graph, true, i.file_relative_name) draw_module(mod, graph, true, i.file_relative_name)
end end
add_classes(i, graph, i.file_relative_name) add_classes(i, graph, i.file_relative_name)
i.diagram = convert_to_png("f_#{file_count}", graph) i.diagram = convert_to_png("f_#{file_count}", graph)
# now go through and document each top level class and # now go through and document each top level class and
# module independently # module independently
i.modules.each_with_index do |mod, count| i.modules.each_with_index do |mod, count|
@ -83,26 +85,24 @@ module RDoc
@global_names = [] @global_names = []
@global_graph = graph = DOT::Digraph.new('name' => 'TopLevel', @global_graph = graph = DOT::Digraph.new('name' => 'TopLevel',
'fontname' => FONT, 'fontname' => FONT,
'fontsize' => '8', 'fontsize' => '8',
'bgcolor' => 'lightcyan1', 'bgcolor' => 'lightcyan1',
'compound' => 'true') 'compound' => 'true')
graph << DOT::Node.new('name' => 'node', graph << DOT::Node.new('name' => 'node',
'fontname' => FONT, 'fontname' => FONT,
'color' => 'black', 'color' => 'black',
'fontsize' => 8) 'fontsize' => 8)
draw_module(mod, graph, true) draw_module(mod, graph, true)
mod.diagram = convert_to_png("m_#{file_count}_#{count}", mod.diagram = convert_to_png("m_#{file_count}_#{count}",
graph) graph)
end end
end end
$stderr.puts unless @options.quiet $stderr.puts unless @options.quiet
end end
#######
private private
#######
def find_names(mod) def find_names(mod)
return [mod.full_name] + mod.classes.collect{|cl| cl.full_name} + return [mod.full_name] + mod.classes.collect{|cl| cl.full_name} +
@ -128,13 +128,13 @@ module RDoc
@counter += 1 @counter += 1
url = mod.http_url("classes") url = mod.http_url("classes")
m = DOT::Subgraph.new('name' => "cluster_#{mod.full_name.gsub( /:/,'_' )}", m = DOT::Subgraph.new('name' => "cluster_#{mod.full_name.gsub( /:/,'_' )}",
'label' => mod.name, 'label' => mod.name,
'fontname' => FONT, 'fontname' => FONT,
'color' => 'blue', 'color' => 'blue',
'style' => 'filled', 'style' => 'filled',
'URL' => %{"#{url}"}, 'URL' => %{"#{url}"},
'fillcolor' => toplevel ? 'palegreen1' : 'palegreen3') 'fillcolor' => toplevel ? 'palegreen1' : 'palegreen3')
@done_modules[mod.full_name] = m @done_modules[mod.full_name] = m
add_classes(mod, m, file) add_classes(mod, m, file)
graph << m graph << m
@ -144,22 +144,22 @@ module RDoc
m_full_name = find_full_name(inc.name, mod) m_full_name = find_full_name(inc.name, mod)
if @local_names.include?(m_full_name) if @local_names.include?(m_full_name)
@global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}", @global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}",
'to' => "#{mod.full_name.gsub( /:/,'_' )}", 'to' => "#{mod.full_name.gsub( /:/,'_' )}",
'ltail' => "cluster_#{m_full_name.gsub( /:/,'_' )}", 'ltail' => "cluster_#{m_full_name.gsub( /:/,'_' )}",
'lhead' => "cluster_#{mod.full_name.gsub( /:/,'_' )}") 'lhead' => "cluster_#{mod.full_name.gsub( /:/,'_' )}")
else else
unless @global_names.include?(m_full_name) unless @global_names.include?(m_full_name)
path = m_full_name.split("::") path = m_full_name.split("::")
url = File.join('classes', *path) + ".html" url = File.join('classes', *path) + ".html"
@global_graph << DOT::Node.new('name' => "#{m_full_name.gsub( /:/,'_' )}", @global_graph << DOT::Node.new('name' => "#{m_full_name.gsub( /:/,'_' )}",
'shape' => 'box', 'shape' => 'box',
'label' => "#{m_full_name}", 'label' => "#{m_full_name}",
'URL' => %{"#{url}"}) 'URL' => %{"#{url}"})
@global_names << m_full_name @global_names << m_full_name
end end
@global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}", @global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}",
'to' => "#{mod.full_name.gsub( /:/,'_' )}", 'to' => "#{mod.full_name.gsub( /:/,'_' )}",
'lhead' => "cluster_#{mod.full_name.gsub( /:/,'_' )}") 'lhead' => "cluster_#{mod.full_name.gsub( /:/,'_' )}")
end end
end end
end end
@ -174,13 +174,14 @@ module RDoc
# create dummy node (needed if empty and for module includes) # create dummy node (needed if empty and for module includes)
if container.full_name if container.full_name
graph << DOT::Node.new('name' => "#{container.full_name.gsub( /:/,'_' )}", graph << DOT::Node.new('name' => "#{container.full_name.gsub( /:/,'_' )}",
'label' => "", 'label' => "",
'width' => (container.classes.empty? and 'width' => (container.classes.empty? and
container.modules.empty?) ? container.modules.empty?) ?
'0.75' : '0.01', '0.75' : '0.01',
'height' => '0.01', 'height' => '0.01',
'shape' => 'plaintext') 'shape' => 'plaintext')
end end
container.classes.each_with_index do |cl, cl_index| container.classes.each_with_index do |cl, cl_index|
last_file = cl.in_files[-1].file_relative_name last_file = cl.in_files[-1].file_relative_name
@ -197,16 +198,16 @@ module RDoc
next if cl.name == 'Object' || cl.name[0,2] == "<<" next if cl.name == 'Object' || cl.name[0,2] == "<<"
url = cl.http_url("classes") url = cl.http_url("classes")
label = cl.name.dup label = cl.name.dup
if use_fileboxes && cl.in_files.length > 1 if use_fileboxes && cl.in_files.length > 1
label << '\n[' + label << '\n[' +
cl.in_files.collect {|i| cl.in_files.collect {|i|
i.file_relative_name i.file_relative_name
}.sort.join( '\n' ) + }.sort.join( '\n' ) +
']' ']'
end end
attrs = { attrs = {
'name' => "#{cl.full_name.gsub( /:/, '_' )}", 'name' => "#{cl.full_name.gsub( /:/, '_' )}",
'fontcolor' => 'black', 'fontcolor' => 'black',
@ -218,40 +219,40 @@ module RDoc
} }
c = DOT::Node.new(attrs) c = DOT::Node.new(attrs)
if use_fileboxes if use_fileboxes
files[last_file].push c files[last_file].push c
else else
graph << c graph << c
end end
end end
if use_fileboxes if use_fileboxes
files.each_value do |val| files.each_value do |val|
graph << val graph << val
end end
end end
unless container.classes.empty? unless container.classes.empty?
container.classes.each_with_index do |cl, cl_index| container.classes.each_with_index do |cl, cl_index|
cl.includes.each do |m| cl.includes.each do |m|
m_full_name = find_full_name(m.name, cl) m_full_name = find_full_name(m.name, cl)
if @local_names.include?(m_full_name) if @local_names.include?(m_full_name)
@global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}", @global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}",
'to' => "#{cl.full_name.gsub( /:/,'_' )}", 'to' => "#{cl.full_name.gsub( /:/,'_' )}",
'ltail' => "cluster_#{m_full_name.gsub( /:/,'_' )}") 'ltail' => "cluster_#{m_full_name.gsub( /:/,'_' )}")
else else
unless @global_names.include?(m_full_name) unless @global_names.include?(m_full_name)
path = m_full_name.split("::") path = m_full_name.split("::")
url = File.join('classes', *path) + ".html" url = File.join('classes', *path) + ".html"
@global_graph << DOT::Node.new('name' => "#{m_full_name.gsub( /:/,'_' )}", @global_graph << DOT::Node.new('name' => "#{m_full_name.gsub( /:/,'_' )}",
'shape' => 'box', 'shape' => 'box',
'label' => "#{m_full_name}", 'label' => "#{m_full_name}",
'URL' => %{"#{url}"}) 'URL' => %{"#{url}"})
@global_names << m_full_name @global_names << m_full_name
end end
@global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}", @global_graph << DOT::Edge.new('from' => "#{m_full_name.gsub( /:/,'_' )}",
'to' => "#{cl.full_name.gsub( /:/, '_')}") 'to' => "#{cl.full_name.gsub( /:/, '_')}")
end end
end end
@ -261,21 +262,20 @@ module RDoc
unless @local_names.include?(sclass_full_name) or @global_names.include?(sclass_full_name) unless @local_names.include?(sclass_full_name) or @global_names.include?(sclass_full_name)
path = sclass_full_name.split("::") path = sclass_full_name.split("::")
url = File.join('classes', *path) + ".html" url = File.join('classes', *path) + ".html"
@global_graph << DOT::Node.new( @global_graph << DOT::Node.new('name' => "#{sclass_full_name.gsub( /:/, '_' )}",
'name' => "#{sclass_full_name.gsub( /:/, '_' )}", 'label' => sclass_full_name,
'label' => sclass_full_name, 'URL' => %{"#{url}"})
'URL' => %{"#{url}"})
@global_names << sclass_full_name @global_names << sclass_full_name
end end
@global_graph << DOT::Edge.new('from' => "#{sclass_full_name.gsub( /:/,'_' )}", @global_graph << DOT::Edge.new('from' => "#{sclass_full_name.gsub( /:/,'_' )}",
'to' => "#{cl.full_name.gsub( /:/, '_')}") 'to' => "#{cl.full_name.gsub( /:/, '_')}")
end end
end end
container.modules.each do |submod| container.modules.each do |submod|
draw_module(submod, graph) draw_module(submod, graph)
end end
end end
def convert_to_png(file_base, graph) def convert_to_png(file_base, graph)
@ -294,7 +294,7 @@ module RDoc
File.open(src, 'w+' ) do |f| File.open(src, 'w+' ) do |f|
f << str << "\n" f << str << "\n"
end end
system "dot", "-T#{op_type}", src, "-o", dot system "dot", "-T#{op_type}", src, "-o", dot
# Now construct the imagemap wrapper around # Now construct the imagemap wrapper around
@ -305,10 +305,10 @@ module RDoc
return ret return ret
end end
# Extract the client-side image map from dot, and use it ##
# to generate the imagemap proper. Return the whole # Extract the client-side image map from dot, and use it to generate the
# <map>..<img> combination, suitable for inclusion on # imagemap proper. Return the whole <map>..<img> combination, suitable for
# the page # inclusion on the page
def wrap_in_image_map(src, dot) def wrap_in_image_map(src, dot)
res = %{<map id="map" name="map">\n} res = %{<map id="map" name="map">\n}
@ -331,6 +331,7 @@ module RDoc
res << %{<img src="#{dot}" usemap="#map" border="0" alt="#{dot}">} res << %{<img src="#{dot}" usemap="#map" border="0" alt="#{dot}">}
return res return res
end end
end end
end end

View File

@ -1,4 +1,6 @@
module DOT module RDoc; end
module RDoc::DOT
TAB = ' ' TAB = ' '
TAB2 = TAB * 2 TAB2 = TAB * 2