2017-11-27 13:45:24 +03:00
|
|
|
# frozen_string_literal: true
|
2022-02-10 05:31:06 +03:00
|
|
|
require_relative '../rdoc'
|
2018-03-26 08:56:26 +03:00
|
|
|
require 'erb'
|
2012-11-27 08:28:14 +04:00
|
|
|
require 'time'
|
2015-06-17 03:50:15 +03:00
|
|
|
require 'json'
|
2020-12-10 10:20:21 +03:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'webrick'
|
|
|
|
rescue LoadError
|
2020-12-10 15:04:57 +03:00
|
|
|
abort "webrick is not found. You may need to `gem install webrick` to install webrick."
|
2020-12-10 10:20:21 +03:00
|
|
|
end
|
2012-11-27 08:28:14 +04:00
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# This is a WEBrick servlet that allows you to browse ri documentation.
|
|
|
|
#
|
|
|
|
# You can show documentation through either `ri --server` or, with RubyGems
|
|
|
|
# 2.0 or newer, `gem server`. For ri, the server runs on port 8214 by
|
|
|
|
# default. For RubyGems the server runs on port 8808 by default.
|
|
|
|
#
|
|
|
|
# You can use this servlet in your own project by mounting it on a WEBrick
|
|
|
|
# server:
|
|
|
|
#
|
|
|
|
# require 'webrick'
|
|
|
|
#
|
|
|
|
# server = WEBrick::HTTPServer.new Port: 8000
|
|
|
|
#
|
|
|
|
# server.mount '/', RDoc::Servlet
|
|
|
|
#
|
|
|
|
# If you want to mount the servlet some other place than the root, provide the
|
|
|
|
# base path when mounting:
|
|
|
|
#
|
|
|
|
# server.mount '/rdoc', RDoc::Servlet, '/rdoc'
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
class RDoc::Servlet < WEBrick::HTTPServlet::AbstractServlet
|
|
|
|
|
|
|
|
@server_stores = Hash.new { |hash, server| hash[server] = {} }
|
|
|
|
@cache = Hash.new { |hash, store| hash[store] = {} }
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Maps an asset type to its path on the filesystem
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
attr_reader :asset_dirs
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# An RDoc::Options instance used for rendering options
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
attr_reader :options
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Creates an instance of this servlet that shares cached data between
|
|
|
|
# requests.
|
|
|
|
|
|
|
|
def self.get_instance server, *options # :nodoc:
|
2012-11-27 08:28:14 +04:00
|
|
|
stores = @server_stores[server]
|
|
|
|
|
|
|
|
new server, stores, @cache, *options
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Creates a new WEBrick servlet.
|
|
|
|
#
|
|
|
|
# Use +mount_path+ when mounting the servlet somewhere other than /.
|
|
|
|
#
|
2013-09-19 03:33:36 +04:00
|
|
|
# Use +extra_doc_dirs+ for additional documentation directories.
|
|
|
|
#
|
2012-12-07 09:22:50 +04:00
|
|
|
# +server+ is provided automatically by WEBrick when mounting. +stores+ and
|
|
|
|
# +cache+ are provided automatically by the servlet.
|
|
|
|
|
2013-09-19 03:33:36 +04:00
|
|
|
def initialize server, stores, cache, mount_path = nil, extra_doc_dirs = []
|
2012-11-27 08:28:14 +04:00
|
|
|
super server
|
|
|
|
|
2012-11-28 11:41:19 +04:00
|
|
|
@cache = cache
|
|
|
|
@mount_path = mount_path
|
2013-09-19 03:33:36 +04:00
|
|
|
@extra_doc_dirs = extra_doc_dirs
|
2012-11-28 11:41:19 +04:00
|
|
|
@stores = stores
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
@options = RDoc::Options.new
|
|
|
|
@options.op_dir = '.'
|
|
|
|
|
|
|
|
darkfish_dir = nil
|
|
|
|
|
|
|
|
# HACK dup
|
|
|
|
$LOAD_PATH.each do |path|
|
|
|
|
darkfish_dir = File.join path, 'rdoc/generator/template/darkfish/'
|
|
|
|
next unless File.directory? darkfish_dir
|
|
|
|
@options.template_dir = darkfish_dir
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
@asset_dirs = {
|
|
|
|
:darkfish => darkfish_dir,
|
|
|
|
:json_index =>
|
|
|
|
File.expand_path('../generator/template/json_index/', __FILE__),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Serves the asset at the path in +req+ for +generator_name+ via +res+.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def asset generator_name, req, res
|
|
|
|
asset_dir = @asset_dirs[generator_name]
|
|
|
|
|
|
|
|
asset_path = File.join asset_dir, req.path
|
|
|
|
|
|
|
|
if_modified_since req, res, asset_path
|
|
|
|
|
|
|
|
res.body = File.read asset_path
|
|
|
|
|
|
|
|
res.content_type = case req.path
|
2019-07-29 16:54:58 +03:00
|
|
|
when /\.css\z/ then 'text/css'
|
|
|
|
when /\.js\z/ then 'application/javascript'
|
|
|
|
else 'application/octet-stream'
|
2012-11-27 08:28:14 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# GET request entry point. Fills in +res+ for the path, etc. in +req+.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def do_GET req, res
|
2019-07-30 06:44:38 +03:00
|
|
|
req.path.sub!(/\A#{Regexp.escape @mount_path}/, '') if @mount_path
|
2012-11-28 11:41:19 +04:00
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
case req.path
|
|
|
|
when '/' then
|
|
|
|
root req, res
|
2016-09-05 13:35:30 +03:00
|
|
|
when '/js/darkfish.js', '/js/jquery.js', '/js/search.js',
|
|
|
|
%r%^/css/%, %r%^/images/%, %r%^/fonts/% then
|
2012-11-27 08:28:14 +04:00
|
|
|
asset :darkfish, req, res
|
|
|
|
when '/js/navigation.js', '/js/searcher.js' then
|
|
|
|
asset :json_index, req, res
|
|
|
|
when '/js/search_index.js' then
|
|
|
|
root_search req, res
|
|
|
|
else
|
|
|
|
show_documentation req, res
|
|
|
|
end
|
2013-09-19 03:33:36 +04:00
|
|
|
rescue WEBrick::HTTPStatus::NotFound => e
|
|
|
|
generator = generator_for RDoc::Store.new
|
|
|
|
|
|
|
|
not_found generator, req, res, e.message
|
2012-11-27 08:28:14 +04:00
|
|
|
rescue WEBrick::HTTPStatus::Status
|
|
|
|
raise
|
|
|
|
rescue => e
|
|
|
|
error e, req, res
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Fills in +res+ with the class, module or page for +req+ from +store+.
|
|
|
|
#
|
|
|
|
# +path+ is relative to the mount_path and is used to determine the class,
|
|
|
|
# module or page name (/RDoc/Servlet.html becomes RDoc::Servlet).
|
|
|
|
# +generator+ is used to create the page.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def documentation_page store, generator, path, req, res
|
2019-07-29 16:51:01 +03:00
|
|
|
text_name = path.chomp '.html'
|
2019-01-25 17:58:30 +03:00
|
|
|
name = text_name.gsub '/', '::'
|
2012-11-27 08:28:14 +04:00
|
|
|
|
|
|
|
if klass = store.find_class_or_module(name) then
|
|
|
|
res.body = generator.generate_class klass
|
2019-07-29 16:56:48 +03:00
|
|
|
elsif page = store.find_text_page(name.sub(/_([^_]*)\z/, '.\1')) then
|
2012-11-27 08:28:14 +04:00
|
|
|
res.body = generator.generate_page page
|
2019-07-29 16:56:48 +03:00
|
|
|
elsif page = store.find_text_page(text_name.sub(/_([^_]*)\z/, '.\1')) then
|
2019-01-25 17:58:30 +03:00
|
|
|
res.body = generator.generate_page page
|
2012-11-27 08:28:14 +04:00
|
|
|
else
|
|
|
|
not_found generator, req, res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Creates the JSON search index on +res+ for the given +store+. +generator+
|
|
|
|
# must respond to \#json_index to build. +req+ is ignored.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def documentation_search store, generator, req, res
|
|
|
|
json_index = @cache[store].fetch :json_index do
|
|
|
|
@cache[store][:json_index] =
|
|
|
|
JSON.dump generator.json_index.build_index
|
|
|
|
end
|
|
|
|
|
|
|
|
res.content_type = 'application/javascript'
|
|
|
|
res.body = "var search_data = #{json_index}"
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Returns the RDoc::Store and path relative to +mount_path+ for
|
|
|
|
# documentation at +path+.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def documentation_source path
|
|
|
|
_, source_name, path = path.split '/', 3
|
|
|
|
|
|
|
|
store = @stores[source_name]
|
|
|
|
return store, path if store
|
|
|
|
|
|
|
|
store = store_for source_name
|
|
|
|
|
|
|
|
store.load_all
|
|
|
|
|
|
|
|
@stores[source_name] = store
|
|
|
|
|
|
|
|
return store, path
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Generates an error page for the +exception+ while handling +req+ on +res+.
|
|
|
|
|
|
|
|
def error exception, req, res
|
|
|
|
backtrace = exception.backtrace.join "\n"
|
2012-11-27 08:28:14 +04:00
|
|
|
|
|
|
|
res.content_type = 'text/html'
|
|
|
|
res.status = 500
|
|
|
|
res.body = <<-BODY
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
<title>Error - #{ERB::Util.html_escape exception.class}</title>
|
2012-11-27 08:28:14 +04:00
|
|
|
|
2016-09-05 13:35:30 +03:00
|
|
|
<link type="text/css" media="screen" href="#{@mount_path}/css/rdoc.css" rel="stylesheet">
|
2012-11-27 08:28:14 +04:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Error</h1>
|
|
|
|
|
|
|
|
<p>While processing <code>#{ERB::Util.html_escape req.request_uri}</code> the
|
2012-12-07 09:22:50 +04:00
|
|
|
RDoc (#{ERB::Util.html_escape RDoc::VERSION}) server has encountered a
|
|
|
|
<code>#{ERB::Util.html_escape exception.class}</code>
|
2012-11-27 08:28:14 +04:00
|
|
|
exception:
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
<pre>#{ERB::Util.html_escape exception.message}</pre>
|
|
|
|
|
|
|
|
<p>Please report this to the
|
2017-10-10 05:01:00 +03:00
|
|
|
<a href="https://github.com/ruby/rdoc/issues">RDoc issues tracker</a>. Please
|
2012-12-07 09:22:50 +04:00
|
|
|
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.
|
2012-11-27 08:28:14 +04:00
|
|
|
|
|
|
|
<p>Backtrace:
|
|
|
|
|
|
|
|
<pre>#{ERB::Util.html_escape backtrace}</pre>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
BODY
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Instantiates a Darkfish generator for +store+
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
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
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# 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+.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
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
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def installed_docs
|
2013-09-19 03:33:36 +04:00
|
|
|
extra_counter = 0
|
2012-11-27 08:28:14 +04:00
|
|
|
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]
|
2013-09-19 03:33:36 +04:00
|
|
|
when :extra then
|
|
|
|
extra_counter += 1
|
|
|
|
store.load_cache if exists
|
|
|
|
title = store.title || "Extra Documentation"
|
|
|
|
[title, "extra-#{extra_counter}/", exists, type, path]
|
2012-11-27 08:28:14 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Returns a 404 page built by +generator+ for +req+ on +res+.
|
|
|
|
|
2013-09-19 03:33:36 +04:00
|
|
|
def not_found generator, req, res, message = nil
|
|
|
|
message ||= "The page <kbd>#{ERB::Util.h req.path}</kbd> was not found"
|
|
|
|
res.body = generator.generate_servlet_not_found message
|
2012-11-27 08:28:14 +04:00
|
|
|
res.status = 404
|
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Enumerates the ri paths. See RDoc::RI::Paths#each
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def ri_paths &block
|
2013-09-19 03:33:36 +04:00
|
|
|
RDoc::RI::Paths.each true, true, true, :all, *@extra_doc_dirs, &block #TODO: pass extra_dirs
|
2012-11-27 08:28:14 +04:00
|
|
|
end
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Generates the root page on +res+. +req+ is ignored.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
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
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Generates a search index for the root page on +res+. +req+ is ignored.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def root_search req, res
|
|
|
|
search_index = []
|
|
|
|
info = []
|
|
|
|
|
|
|
|
installed_docs.map do |name, href, exists, type, path|
|
|
|
|
next unless exists
|
|
|
|
|
|
|
|
search_index << name
|
|
|
|
|
2013-02-05 12:24:20 +04:00
|
|
|
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'
|
2013-09-19 03:33:36 +04:00
|
|
|
when :extra
|
|
|
|
comment = name
|
2013-02-05 12:24:20 +04:00
|
|
|
end
|
2012-11-27 08:28:14 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Displays documentation for +req+ on +res+, whether that be HTML or some
|
|
|
|
# asset.
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
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
|
|
|
|
|
2012-12-07 09:22:50 +04:00
|
|
|
##
|
|
|
|
# Returns an RDoc::Store for the given +source_name+ ('ruby' or a gem name).
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def store_for source_name
|
|
|
|
case source_name
|
2013-01-23 05:02:24 +04:00
|
|
|
when 'home' then
|
|
|
|
RDoc::Store.new RDoc::RI::Paths.home_dir, :home
|
2012-11-27 08:28:14 +04:00
|
|
|
when 'ruby' then
|
|
|
|
RDoc::Store.new RDoc::RI::Paths.system_dir, :system
|
2013-01-23 05:02:24 +04:00
|
|
|
when 'site' then
|
|
|
|
RDoc::Store.new RDoc::RI::Paths.site_dir, :site
|
2019-07-29 16:56:48 +03:00
|
|
|
when /\Aextra-(\d+)\z/ then
|
2013-09-19 03:33:36 +04:00
|
|
|
index = $1.to_i - 1
|
|
|
|
ri_dir = installed_docs[index][4]
|
|
|
|
RDoc::Store.new ri_dir, :extra
|
2012-11-27 08:28:14 +04:00
|
|
|
else
|
|
|
|
ri_dir, type = ri_paths.find do |dir, dir_type|
|
|
|
|
next unless dir_type == :gem
|
|
|
|
|
|
|
|
source_name == dir[%r%/([^/]*)/ri$%, 1]
|
|
|
|
end
|
|
|
|
|
2013-09-19 03:33:36 +04:00
|
|
|
raise WEBrick::HTTPStatus::NotFound,
|
2018-03-26 08:56:26 +03:00
|
|
|
"Could not find gem \"#{ERB::Util.html_escape(source_name)}\". Are you sure you installed it?" unless ri_dir
|
2013-09-19 03:33:36 +04:00
|
|
|
|
|
|
|
store = RDoc::Store.new ri_dir, type
|
|
|
|
|
|
|
|
return store if File.exist? store.cache_path
|
|
|
|
|
|
|
|
raise WEBrick::HTTPStatus::NotFound,
|
2018-03-26 08:56:26 +03:00
|
|
|
"Could not find documentation for \"#{ERB::Util.html_escape(source_name)}\". Please run `gem rdoc --ri gem_name`"
|
2012-11-27 08:28:14 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|