|
| 1 | +# frozen_string_literal: true |
| 2 | +require 'erb' |
| 3 | +require 'time' |
| 4 | +require 'json' |
| 5 | + |
| 6 | +class RDoc::Server < WEBrick::HTTPServlet::AbstractServlet |
| 7 | + ## |
| 8 | + # Creates an instance of this servlet that shares cached data between |
| 9 | + # requests. |
| 10 | + |
| 11 | + def self.get_instance server, rdoc # :nodoc: |
| 12 | + new server, rdoc |
| 13 | + end |
| 14 | + |
| 15 | + ## |
| 16 | + # Creates a new WEBrick servlet. |
| 17 | + # |
| 18 | + # +server+ is provided automatically by WEBrick when mounting. |
| 19 | + # +rdoc+ is the RDoc::RDoc instance to display documentation from. |
| 20 | + |
| 21 | + def initialize server, rdoc |
| 22 | + super server |
| 23 | + |
| 24 | + @rdoc = rdoc |
| 25 | + @generator = rdoc.generator |
| 26 | + @generator.file_output = false |
| 27 | + |
| 28 | + darkfish_dir = File.join(__dir__, 'generator/template/darkfish/') |
| 29 | + json_index_dir = File.join(__dir__, 'generator/template/json_index/') |
| 30 | + |
| 31 | + @asset_dirs = { |
| 32 | + :darkfish => darkfish_dir, |
| 33 | + :json_index => json_index_dir, |
| 34 | + } |
| 35 | + end |
| 36 | + |
| 37 | + ## |
| 38 | + # GET request entry point. Fills in +res+ for the path, etc. in +req+. |
| 39 | + |
| 40 | + def do_GET req, res |
| 41 | + req.path.sub!(/\A\//, '') |
| 42 | + |
| 43 | + case req.path |
| 44 | + when '/' |
| 45 | + res.body = @generator.generate_servlet_root installed_docs |
| 46 | + res.content_type = 'text/html' |
| 47 | + when 'js/darkfish.js', 'js/jquery.js', 'js/search.js', %r{^css/}, %r{^images/}, %r{^fonts/} |
| 48 | + asset :darkfish, req, res |
| 49 | + when 'js/navigation.js', 'js/searcher.js' |
| 50 | + asset :json_index, req, res |
| 51 | + when 'js/search_index.js' |
| 52 | + res.body = "var search_data = #{JSON.dump @generator.json_index.build_index}" |
| 53 | + res.content_type = 'application/javascript' |
| 54 | + else |
| 55 | + show_documentation req, res |
| 56 | + end |
| 57 | + rescue WEBrick::HTTPStatus::NotFound => e |
| 58 | + not_found @generator, req, res, e.message |
| 59 | + rescue WEBrick::HTTPStatus::Status |
| 60 | + raise |
| 61 | + rescue => e |
| 62 | + $stderr.puts e.full_message |
| 63 | + error e, req, res |
| 64 | + end |
| 65 | + |
| 66 | + private |
| 67 | + |
| 68 | + def asset generator_name, req, res |
| 69 | + asset_dir = @asset_dirs[generator_name] |
| 70 | + |
| 71 | + asset_path = File.join asset_dir, req.path |
| 72 | + |
| 73 | + res.body = File.read asset_path |
| 74 | + |
| 75 | + res.content_type = case req.path |
| 76 | + when /\.css\z/ then 'text/css' |
| 77 | + when /\.js\z/ then 'application/javascript' |
| 78 | + else 'application/octet-stream' |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + PAGE_NAME_SUB_REGEXP = /_([^_]*)\z/ |
| 83 | + |
| 84 | + def documentation_page store, generator, path, req, res |
| 85 | + text_name = path.chomp '.html' |
| 86 | + name = text_name.gsub '/', '::' |
| 87 | + |
| 88 | + content = if klass = store.find_class_or_module(name) |
| 89 | + generator.generate_class klass |
| 90 | + elsif page = store.find_text_page(name.sub(PAGE_NAME_SUB_REGEXP, '.\1')) |
| 91 | + generator.generate_page page |
| 92 | + elsif page = store.find_text_page(text_name.sub(PAGE_NAME_SUB_REGEXP, '.\1')) |
| 93 | + generator.generate_page page |
| 94 | + elsif page = store.find_file_named(text_name.sub(PAGE_NAME_SUB_REGEXP, '.\1')) |
| 95 | + generator.generate_page page |
| 96 | + end |
| 97 | + |
| 98 | + if content |
| 99 | + res.body = content |
| 100 | + else |
| 101 | + not_found generator, req, res |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + def error exception, req, res |
| 106 | + backtrace = exception.backtrace.join "\n" |
| 107 | + |
| 108 | + res.content_type = 'text/html' |
| 109 | + res.status = 500 |
| 110 | + res.body = <<-BODY |
| 111 | +<!DOCTYPE html> |
| 112 | +<html> |
| 113 | +<head> |
| 114 | +<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> |
| 115 | +
|
| 116 | +<title>Error - #{ERB::Util.html_escape exception.class}</title> |
| 117 | +
|
| 118 | +<link type="text/css" media="screen" href="/css/rdoc.css" rel="stylesheet"> |
| 119 | +</head> |
| 120 | +<body> |
| 121 | +<h1>Error</h1> |
| 122 | +
|
| 123 | +<p>While processing <code>#{ERB::Util.html_escape req.request_uri}</code> the |
| 124 | +RDoc (#{ERB::Util.html_escape RDoc::VERSION}) server has encountered a |
| 125 | +<code>#{ERB::Util.html_escape exception.class}</code> |
| 126 | +exception: |
| 127 | +
|
| 128 | +<pre>#{ERB::Util.html_escape exception.message}</pre> |
| 129 | +
|
| 130 | +<p>Please report this to the |
| 131 | +<a href="https://github.com/ruby/rdoc/issues">RDoc issues tracker</a>. Please |
| 132 | +include the RDoc version, the URI above and exception class, message and |
| 133 | +backtrace. If you're viewing a gem's documentation, include the gem name and |
| 134 | +version. If you're viewing Ruby's documentation, include the version of ruby. |
| 135 | +
|
| 136 | +<p>Backtrace: |
| 137 | +
|
| 138 | +<pre>#{ERB::Util.html_escape backtrace}</pre> |
| 139 | +
|
| 140 | +</body> |
| 141 | +</html> |
| 142 | + BODY |
| 143 | + end |
| 144 | + |
| 145 | + def not_found generator, req, res, message = nil |
| 146 | + message ||= "The page <kbd>#{ERB::Util.h req.path}</kbd> was not found" |
| 147 | + res.body = generator.generate_servlet_not_found message |
| 148 | + res.status = 404 |
| 149 | + end |
| 150 | + |
| 151 | + def show_documentation req, res |
| 152 | + # Clear all the previous data |
| 153 | + @rdoc.store.classes_hash.clear |
| 154 | + @rdoc.store.modules_hash.clear |
| 155 | + @rdoc.store.files_hash.clear |
| 156 | + |
| 157 | + # RDoc instance use last_modified list to avoid reparsing files |
| 158 | + # We need to clear it to force reparsing |
| 159 | + @rdoc.last_modified.clear |
| 160 | + |
| 161 | + # Reparse the files |
| 162 | + @rdoc.parse_files(@rdoc.options.files) |
| 163 | + |
| 164 | + # Regenerate the documentation and asserts |
| 165 | + @generator.generate_for_server |
| 166 | + |
| 167 | + case req.path |
| 168 | + when nil, '', 'index.html' |
| 169 | + res.body = @generator.generate_index |
| 170 | + when 'table_of_contents.html' |
| 171 | + res.body = @generator.generate_table_of_contents |
| 172 | + else |
| 173 | + documentation_page @rdoc.store, @generator, req.path, req, res |
| 174 | + end |
| 175 | + ensure |
| 176 | + res.content_type ||= 'text/html' |
| 177 | + end |
| 178 | +end |
0 commit comments