3 require 'rdoc/generator'
4 require 'rdoc/markup/to_html'
7 # We're responsible for generating all the HTML files from the object tree
8 # defined in code_objects.rb. We generate:
10 # [files] an html file for each input file given. These
11 # input files appear as objects of class
14 # [classes] an html file for each class or module encountered.
15 # These classes are not grouped by file: if a file
16 # contains four classes, we'll generate an html
17 # file for the file itself, and four html files
18 # for the individual classes.
20 # [indices] we generate three indices for files, classes,
21 # and methods. These are displayed in a browser
22 # like window with three index panes across the
23 # top and the selected description below
25 # Method descriptions appear in whatever entity (file, class, or module) that
28 # We generate files in a structure below a specified subdirectory, normally
34 # | |__ per file summaries
37 # |__ per class/module descriptions
39 # HTML is generated using the Template class.
41 class RDoc::Generator::HTML
43 include RDoc::Generator::MarkUp
46 # Generator may need to return specific subclasses depending on the
47 # options they are passed. Because of this we create them using a factory
50 RDoc::Generator::AllReferences.reset
51 RDoc::Generator::Method.reset
53 if options.all_one_file
54 RDoc::Generator::HTMLInOne.new options
65 # Set up a new HTML generator. Basically all we do here is load up the
66 # correct output temlate
68 def initialize(options) #:not-new:
75 # Build the initial indices and output objects
76 # based on an array of TopLevel objects containing
77 # the extracted information.
79 def generate(toplevels)
80 @toplevels = toplevels
93 # Load up the HTML template specified in the options.
94 # If the template name contains a slash, use it literally
96 def load_html_template
97 template = @options.template
99 unless template =~ %r{/|\\} then
100 template = File.join('rdoc', 'generator', @options.generator.key,
106 @template = self.class.const_get @options.template.upcase
107 @options.template_class = @template
110 $stderr.puts "Could not find HTML template '#{template}'"
115 # Write out the style sheet used by the main frames
117 def write_style_sheet
118 return unless @template.constants.include? :STYLE or
119 @template.constants.include? 'STYLE'
121 template = RDoc::TemplatePage.new @template::STYLE
123 unless @options.css then
124 open RDoc::Generator::CSS_NAME, 'w' do |f|
127 if @template.constants.include? :FONTS or
128 @template.constants.include? 'FONTS' then
129 values["fonts"] = @template::FONTS
132 template.write_html_on(f, values)
138 # See the comments at the top for a description of the directory structure
140 def gen_sub_directories
141 FileUtils.mkdir_p RDoc::Generator::FILE_DIR
142 FileUtils.mkdir_p RDoc::Generator::CLASS_DIR
144 $stderr.puts $!.message
149 @files, @classes = RDoc::Generator::Context.build_indicies(@toplevels,
154 # Generate all the HTML
157 # the individual descriptions for files and classes
160 # and the index files
166 # this method is defined in the template file
167 write_extra_pages if defined? write_extra_pages
172 if item.document_self
174 FileUtils.mkdir_p(File.dirname(op_file))
175 open(op_file, "w") { |file| item.write_on(file) }
182 gen_an_index @files, 'Files', @template::FILE_INDEX, "fr_file_index.html"
186 gen_an_index(@classes, 'Classes', @template::CLASS_INDEX,
187 "fr_class_index.html")
191 gen_an_index(RDoc::Generator::Method.all_methods, 'Methods',
192 @template::METHOD_INDEX, "fr_method_index.html")
195 def gen_an_index(collection, title, template, filename)
196 template = RDoc::TemplatePage.new @template::FR_INDEX_BODY, template
198 collection.sort.each do |f|
200 res << { "href" => f.path, "name" => f.index_name }
206 'list_title' => CGI.escapeHTML(title),
207 'index_url' => main_url,
208 'charset' => @options.charset,
209 'style_url' => style_url('', @options.css),
212 open filename, 'w' do |f|
213 template.write_html_on(f, values)
218 # The main index page is mostly a template frameset, but includes the
219 # initial page. If the <tt>--main</tt> option was given, we use this as
220 # our main page, otherwise we use the first file specified on the command
224 template = RDoc::TemplatePage.new @template::INDEX
226 open 'index.html', 'w' do |f|
227 classes = @classes.sort.map { |klass| klass.value_hash }
230 'main_page' => @main_page,
231 'initial_page' => main_url,
232 'style_url' => style_url('', @options.css),
233 'title' => CGI.escapeHTML(@options.title),
234 'charset' => @options.charset,
235 'classes' => classes,
238 values['inline_source'] = @options.inline_source
240 template.write_html_on f, values
245 # Returns the url of the main page
248 @main_page = @options.main_page
251 @main_page_ref = RDoc::Generator::AllReferences[@main_page]
252 if @main_page_ref then
253 @main_page_path = @main_page_ref.path
255 $stderr.puts "Could not find main page #{@main_page}"
259 unless @main_page_path then
260 file = @files.find { |context| context.document_self }
261 @main_page_path = file.path if file
264 unless @main_page_path then
265 $stderr.puts "Couldn't find anything to document"
266 $stderr.puts "Perhaps you've used :stopdoc: in all classes"
275 class RDoc::Generator::HTMLInOne < RDoc::Generator::HTML
277 def initialize(*args)
282 # Build the initial indices and output objects
283 # based on an array of TopLevel objects containing
284 # the extracted information.
297 # * a list of RDoc::Generator::File objects for each TopLevel object.
298 # * a list of RDoc::Generator::Class objects for each first level
299 # class or module in the TopLevel objects
300 # * a complete list of all hyperlinkable terms (file,
301 # class, module, and method names)
304 @files, @classes = RDoc::Generator::Context.build_indices(@toplevels,
309 # Generate all the HTML. For the one-file case, we generate
310 # all the information in to one big hash
314 'charset' => @options.charset,
315 'files' => gen_into(@files),
316 'classes' => gen_into(@classes),
317 'title' => CGI.escapeHTML(@options.title),
320 # this method is defined in the template file
321 write_extra_pages if defined? write_extra_pages
323 template = RDoc::TemplatePage.new @template::ONE_PAGE
326 opfile = open @options.op_name, 'w'
330 template.write_html_on(opfile, values)
336 res << item.value_hash
342 gen_an_index(@files, 'Files')
346 gen_an_index(@classes, 'Classes')
350 gen_an_index(RDoc::Generator::Method.all_methods, 'Methods')
353 def gen_an_index(collection, title)
355 collection.sort.each do |f|
357 res << { "href" => f.path, "name" => f.index_name }
363 'list_title' => title,
364 'index_url' => main_url,