While processing #{ERB::Util.html_escape req.request_uri} the
RDoc (#{ERB::Util.html_escape RDoc::VERSION}) server has encountered a
#{ERB::Util.html_escape exception.class}
exception:
#{ERB::Util.html_escape exception.message}
Please report this to the RDoc issues tracker. Please include the RDoc version, the URI above and exception class, message and backtrace. If you're viewing a gem's documentation, include the gem name and version. If you're viewing Ruby's documentation, include the version of ruby.
Backtrace:
#{ERB::Util.html_escape backtrace}
BODY
end
##
# Instantiates a Darkfish generator for +store+
def generator_for store
generator = RDoc::Generator::Darkfish.new store, @options
generator.file_output = false
generator.asset_rel_path = '..'
rdoc = RDoc::RDoc.new
rdoc.store = store
rdoc.generator = generator
rdoc.options = @options
@options.main_page = store.main
@options.title = store.title
generator
end
##
# Handles the If-Modified-Since HTTP header on +req+ for +path+. If the
# file has not been modified a Not Modified response is returned. If the
# file has been modified a Last-Modified header is added to +res+.
def if_modified_since req, res, path = nil
last_modified = File.stat(path).mtime if path
res['last-modified'] = last_modified.httpdate
return unless ims = req['if-modified-since']
ims = Time.parse ims
unless ims < last_modified then
res.body = ''
raise WEBrick::HTTPStatus::NotModified
end
end
##
# Returns an Array of installed documentation.
#
# Each entry contains the documentation name (gem name, 'Ruby
# Documentation', etc.), the path relative to the mount point, whether the
# documentation exists, the type of documentation (See RDoc::RI::Paths#each)
# and the filesystem to the RDoc::Store for the documentation.
def installed_docs
extra_counter = 0
ri_paths.map do |path, type|
store = RDoc::Store.new path, type
exists = File.exist? store.cache_path
case type
when :gem then
gem_path = path[%r%/([^/]*)/ri$%, 1]
[gem_path, "#{gem_path}/", exists, type, path]
when :system then
['Ruby Documentation', 'ruby/', exists, type, path]
when :site then
['Site Documentation', 'site/', exists, type, path]
when :home then
['Home Documentation', 'home/', exists, type, path]
when :extra then
extra_counter += 1
store.load_cache if exists
title = store.title || "Extra Documentation"
[title, "extra-#{extra_counter}/", exists, type, path]
end
end
end
##
# Returns a 404 page built by +generator+ for +req+ on +res+.
def not_found generator, req, res, message = nil
message ||= "The page #{ERB::Util.h req.path} was not found"
res.body = generator.generate_servlet_not_found message
res.status = 404
end
##
# Enumerates the ri paths. See RDoc::RI::Paths#each
def ri_paths &block
RDoc::RI::Paths.each true, true, true, :all, *@extra_doc_dirs, &block #TODO: pass extra_dirs
end
##
# Generates the root page on +res+. +req+ is ignored.
def root req, res
generator = RDoc::Generator::Darkfish.new nil, @options
res.body = generator.generate_servlet_root installed_docs
res.content_type = 'text/html'
end
##
# Generates a search index for the root page on +res+. +req+ is ignored.
def root_search req, res
search_index = []
info = []
installed_docs.map do |name, href, exists, type, path|
next unless exists
search_index << name
case type
when :gem
gemspec = path.gsub(%r%/doc/([^/]*?)/ri$%,
'/specifications/\1.gemspec')
spec = Gem::Specification.load gemspec
path = spec.full_name
comment = spec.summary
when :system then
path = 'ruby'
comment = 'Documentation for the Ruby standard library'
when :site then
path = 'site'
comment = 'Documentation for non-gem libraries'
when :home then
path = 'home'
comment = 'Documentation from your home directory'
when :extra
comment = name
end
info << [name, '', path, '', comment]
end
index = {
:index => {
:searchIndex => search_index,
:longSearchIndex => search_index,
:info => info,
}
}
res.body = "var search_data = #{JSON.dump index};"
res.content_type = 'application/javascript'
end
##
# Displays documentation for +req+ on +res+, whether that be HTML or some
# asset.
def show_documentation req, res
store, path = documentation_source req.path
if_modified_since req, res, store.cache_path
generator = generator_for store
case path
when nil, '', 'index.html' then
res.body = generator.generate_index
when 'table_of_contents.html' then
res.body = generator.generate_table_of_contents
when 'js/search_index.js' then
documentation_search store, generator, req, res
else
documentation_page store, generator, path, req, res
end
ensure
res.content_type ||= 'text/html'
end
##
# Returns an RDoc::Store for the given +source_name+ ('ruby' or a gem name).
def store_for source_name
case source_name
when 'home' then
RDoc::Store.new RDoc::RI::Paths.home_dir, :home
when 'ruby' then
RDoc::Store.new RDoc::RI::Paths.system_dir, :system
when 'site' then
RDoc::Store.new RDoc::RI::Paths.site_dir, :site
when /\Aextra-(\d+)\z/ then
index = $1.to_i - 1
ri_dir = installed_docs[index][4]
RDoc::Store.new ri_dir, :extra
else
ri_dir, type = ri_paths.find do |dir, dir_type|
next unless dir_type == :gem
source_name == dir[%r%/([^/]*)/ri$%, 1]
end
raise WEBrick::HTTPStatus::NotFound,
"Could not find gem \"#{ERB::Util.html_escape(source_name)}\". Are you sure you installed it?" unless ri_dir
store = RDoc::Store.new ri_dir, type
return store if File.exist? store.cache_path
raise WEBrick::HTTPStatus::NotFound,
"Could not find documentation for \"#{ERB::Util.html_escape(source_name)}\". Please run `gem rdoc --ri gem_name`"
end
end
end