|
This package contains Rdoc and SimpleMarkup. Rdoc is an application that produces documentation for one or more Ruby source files. We work similarly to JavaDoc, parsing the source, and extracting the definition for classes, modules, and methods (along with includes and requires). We associate with these optional documentation contained in the immediately preceding comment block, and then render the result using a pluggable output formatter. (Currently, HTML is the only supported format. Markup is a library that converts plain text into various output formats. The Markup library is used to interpret the comment blocks that Rdoc uses to document methods, classes, and so on.
This distribution contains two packages, rdoc itself and a text markup library, 'markup'. You can install them both using the single command
% ruby install.rb
in this directory. If you just want to install 'markup', change to the markup directory and run the install.rb script there.
Once installed, you can create documentation using the 'rdoc' command (the command is 'rdoc.rb' under Windows)
% rdoc [options] [names...]
Type "rdoc --help" for an up-to-date option summary.
A typical use might be to generate documentation for a package of Ruby source (such as rdoc itself).
% rdoc
This command generates documentation for all the Ruby and C source files in and below the current directory. These will be stored in a documentation tree starting in the subdirectory 'doc'.
You can make this slightly more useful for your readers by having the index page contain the documentation for the primary file. In our case, we could type
% rdoc --main rdoc/rdoc.rb
You'll find information on the various formatting tricks you can use in comment blocks in the documentation this generates.
RDoc uses file extensions to determine how to process each file. File names ending .rb and .rbw are assumed to be Ruby source. Files ending .c are parsed as C files. All other files are assumed to contain just SimpleMarkup-style markup (with or without leading '#' comment markers). If directory names are passed to RDoc, they are scanned recursively for C and Ruby source files only.
RDoc is Copyright (c) 2001-2003 Dave Thomas, The Pragmatic Programmers. It is free software, and may be redistributed under the terms specified in the README file of the Ruby distribution.
The Rdoc homepage is rdoc.sourceforge.net. There you'll find links for downloading the Rdoc package, and instructions on how to get the still-quivering sources from CVS. I'm also using Sourceforge to track bugs and manage feature requests. If you submit patches, it would help if they were inline (not attachments) and generated using "diff -u". I don't have access to a wide variety of browsers, so reports that output looks funny under Browser XYZ aren't too helpful: far better are suggested changes to the generated HTML that fix the problem.
For other information, feel free to ask on the ruby-talk mailing list (which is mirrored to comp.lang.ruby) or contact dave@pragmaticprogrammer.com.
RDoc is invoked from the command line using:
% rdoc <options> [name...]
Files are parsed, and the information they contain collected, before any output is produced. This allows cross references between all files to be resolved. If a name is a directory, it is traversed. If no names are specified, all Ruby files in the current directory (and subdirectories) are processed.
Options are:
A typical small Ruby program commented using RDoc might be as follows. You can see the formatted result in EXAMPLE.rb and Anagram.
# The program takes an initial word or phrase from # the command line (or in the absence of a # parameter from the first line of standard # input). In then reads successive words or # phrases from standard input and reports whether # they are angrams of the first word. # # Author:: Dave Thomas (mailto:dave@x.y) # Copyright:: Copyright (c) 2002 The Pragmatic Programmers, LLC # License:: Distributes under the same terms as Ruby # This class holds the letters in the original # word or phrase. The is_anagram? method allows us # to test if subsequent words or phrases are # anagrams of the original.
class Anagram # Remember the letters in the initial word def initialize(text) @initial_letters = letters_of(text) end
# Test to see if a new word contains the same # letters as the original def is_anagram?(text) @initial_letters == letters_of(text) end # Determine the letters in a word or phrase # # * all letters are converted to lower case # * anything not a letter is stripped out # * the letters are converted into an array # * the array is sorted # * the letters are joined back into a string
def letters_of(text) text.downcase.delete('^a-z').split('').sort.join end end tester = Anagram.new(ARGV.shift || gets)
ARGF.each do |text| puts "Anagram! " if tester.is_anagram? text end
Comment blocks can be written fairly naturally.
Paragraphs are lines that share the left margin. Text indented past this margin are formatted verbatim.
For example, the input that produced the above paragraph looked like
1. Lists are typed as indented paragraphs with: * a '*' or '-' (for bullet lists) * a digit followed by a period for numbered lists
[cat] small domestic animal [+cat+] command to copy standard input
cat:: small domestic animal +cat+:: command to copy standard input
For both kinds of labeled lists, if the body text starts on the same line as the label, then the start of that text determines the block indent for the rest of the body. The text may also start on the line following the label, indented from the start of the label. This is often preferable if the label is long. Both the following are valid labeled list entries:
<tt>--output</tt> <i>name [, name]</i>:: specify the name of one or more output files. If multiple files are present, the first is used as the index. <tt>--quiet:</tt>:: do not output the names, sizes, byte counts, index areas, or bit ratios of units as they are processed.
= Level One Heading == Level Two Heading
and so on
italic: | word or <em>text</em> |
bold: | word or <b>text</b> |
typewriter: | word or <tt>text</tt> |
The first form only words around 'words', where a word is a sequence of upper and lower case letters and underscores. Putting a backslash before inline markup stops it being interpreted, which is how I created the table above:
_italic_:: \_word_ or \<em>text</em> *bold*:: \*word* or \<b>text</b> +typewriter+:: \+word+ or \<tt>text</tt>
Hyperlinks can also be of the form label[url], in which case the label is used in the displayed text, and url is used as the target.
def fred ... yield line, address
This will get documented as
fred() { |line, address| ... }
You can override this using a comment containing ':yields: ...' immediately after the method definition
def fred # :yields: index, position ... yield line, address
which will get documented as
fred() { |index, position| ... }
module SM #:nodoc: class Input end end module Markup #:nodoc: all class Output end end
In the above code, only class SM::Input will be documented.
# Extract the age and calculate the # date-of-birth. #-- # FIXME: fails if the birthday falls on # February 29th #++ # The DOB is returned as a Time object. def get_dob(person) ...
See also markup/simple_markup.rb.
Author: | Dave Thomas <dave@pragmaticprogrammer.com> |
Requires: | Ruby 1.6.5 or later |
License: | Copyright (c) 2001-2003 Dave Thomas. Released under the same license as Ruby. |
This software is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.