#!/usr/bin/env ruby
# Create an HTML index of links to generated RDocs for all locally installed gems
#
# Usage: gemdocindex > gemindex.html
#
# Copyright (c) 2008 Steve Purcell, http://www.sanityinc.com/
#
# Licensed under the same terms as Ruby itself.
#
require 'rubygems'
require 'rubygems/doc_manager'
require 'rubygems/version'
gem_name_to_gem_list_map = {}
Gem::SourceIndex.from_installed_gems.each do |full_gem_name, gem|
if Gem::DocManager.new(gem).rdoc_installed?
(gem_name_to_gem_list_map[gem.name] ||= []) << gem
end
end
# Add a gem spec for rubygems itself
rubygems_gem = Gem::Specification.new do |spec|
spec.name = 'rubygems'
spec.version = Gem::RubyGemsVersion
spec.summary = "The rubygems system"
end
def rubygems_gem.installation_path() Gem::default_path end
gem_name_to_gem_list_map['rubygems'] = [rubygems_gem]
puts <<-end_html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Gem RDoc index</title>
<style>
body { background: #fff; margin: 0px }
a { text-decoration: none }
a:hover { text-decoration: underline }
h1, #footer { margin: 0; padding: 0.2em 20px; background: #ddd; color: #000 }
h1 { border-bottom: solid 5px #ccc }
table { margin: 20px; clear: both; border-collapse: collapse }
tr { border-top: solid 1px #ccc }
td.name { width: 15em; }
td .version { padding-right: 0.5em; font-size: smaller; }
#footer { font-size: smaller }
</style>
</head>
<body>
<h1>Gem RDoc index</h1>
<table>
end_html
Gem::Specification.module_eval do
define_method(:rdoc_index_path) do
File.join(installation_path, "doc", "#{name}-#{version}", "rdoc", "index.html")
end
end
gem_name_to_gem_list_map.keys.sort_by { |n| n.downcase }.each do |gem_name|
gems_from_newest_to_oldest = gem_name_to_gem_list_map[gem_name].sort_by { |g| g.version }.reverse
latest = gems_from_newest_to_oldest.first
puts "<tr><td class=\"name\"><a href=\"file://#{latest.rdoc_index_path}\">#{gem_name}</a></td>"
puts "<td>"
for gem in gems_from_newest_to_oldest
puts "<span class=\"version\"><a href=\"file://#{gem.rdoc_index_path}\">#{gem.version}</a></span>"
end
puts "</td></tr>"
end
puts <<-end_html
</table>
<div id="footer">
Generated by <a href="http://www.sanityinc.com/">gemdocindex</a>
</div>
</body>
</html>
end_html