<ppdoc>
<copyright>
    Copyright (c) 2001 by Addison Wesley Longman.  This
    material may be distributed only subject to the terms and
    conditions set forth in the Open Publication License, v1.0 or
    later (the latest version is presently available at
    http://www.opencontent.org/openpub/).
</copyright>
<chapter  ref="xref-000001" name="Embedded Documentation">
<p/>
<figure type="figure">Figure not available...</figure>
<p/>
<figure type="figure">Figure not available...</figure>
<p/>
So you've written a masterpiece, a class in a class of its own, and
you'd like to share it with the world. But, being a responsible
developer, you feel the need to document your creation. What do you
do?  The simplest solution is to use Ruby's built-in documentation
format, RD, and <tt>rdtool</tt>, a Ruby utility suite that converts this
documentation into a variety of output formats.
<p/>
<tt>rdtool</tt> scans a file for <tt>=begin</tt> and <tt>=end</tt>{=begin...=end@<tt></tt>{=begin
pairs, and
extracts the text between them all. This text is assumed to be
documentation in RD format. The text is then processed according to a
simple set of rules:
<p/>
<ul>
<li> Lines of text flush to the left margin are converted to
  paragraphs.
</li><li> Lines starting with one to four equals signs are headings. ``=''
  is a first-level heading, ``=='' a second-level heading, and so
  on. ``+'' and ``++'' can be used to signal fifth- and sixth-level
  headings if you really want to go that deep.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[    = Top Level Heading
    == Second Level Heading
    ...
]]></fullcode>
=<nbsp/>Top<nbsp/>Level<nbsp/>Heading
==<nbsp/>Second<nbsp/>Level<nbsp/>Heading
...
</alltt>
</codefragment>
</li><li> Lines in which the first nonspace is an asterisk indicate the
  beginnings of bullet lists. Continuation lines for each bullet item
  should line up with the text on the first line. Lists may be nested.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[    This is normal text
    * start of a
      multiline bullet item
    * and another
      * nested item
      * second nested
    * third item at top level
]]></fullcode>
This<nbsp/>is<nbsp/>normal<nbsp/>text
*<nbsp/>start<nbsp/>of<nbsp/>a
<nbsp/><nbsp/>multiline<nbsp/>bullet<nbsp/>item
*<nbsp/>and<nbsp/>another
<nbsp/><nbsp/>*<nbsp/>nested<nbsp/>item
<nbsp/><nbsp/>*<nbsp/>second<nbsp/>nested
*<nbsp/>third<nbsp/>item<nbsp/>at<nbsp/>top<nbsp/>level
</alltt>
</codefragment>
</li><li> Lines where the first nonspace characters are digits between
  parentheses indicate numbered lists. The actual digits used are
  ignored. Again, lists may be nested.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[    (1) A numbered item
        * subitem in a bulleted list
        * subitem
    (2) Second numbered item
    (9) This will actually be labeled '3.'
]]></fullcode>
(1)<nbsp/>A<nbsp/>numbered<nbsp/>item
<nbsp/><nbsp/><nbsp/><nbsp/>*<nbsp/>subitem<nbsp/>in<nbsp/>a<nbsp/>bulleted<nbsp/>list
<nbsp/><nbsp/><nbsp/><nbsp/>*<nbsp/>subitem
(2)<nbsp/>Second<nbsp/>numbered<nbsp/>item
(9)<nbsp/>This<nbsp/>will<nbsp/>actually<nbsp/>be<nbsp/>labeled<nbsp/>'3.'
</alltt>
</codefragment>
</li><li> Lines starting with a colon indicate labeled lists. The text on
  the colon line is the label. The immediately following text (which
  may not be indented less than the label) is the descriptive
  text. Again, each type of list may be nested.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[    : red
      when the light is red, you
      must stop
    : amber
      the amber light means that things are about to change. Either:
      * step on the gas, or
      * slam on the brakes
    : green
      green means GO
]]></fullcode>
:<nbsp/>red
<nbsp/><nbsp/>when<nbsp/>the<nbsp/>light<nbsp/>is<nbsp/>red,<nbsp/>you
<nbsp/><nbsp/>must<nbsp/>stop
:<nbsp/>amber
<nbsp/><nbsp/>the<nbsp/>amber<nbsp/>light<nbsp/>means<nbsp/>that<nbsp/>things<nbsp/>are<nbsp/>about<nbsp/>to<nbsp/>change.<nbsp/>Either:
<nbsp/><nbsp/>*<nbsp/>step<nbsp/>on<nbsp/>the<nbsp/>gas,<nbsp/>or
<nbsp/><nbsp/>*<nbsp/>slam<nbsp/>on<nbsp/>the<nbsp/>brakes
:<nbsp/>green
<nbsp/><nbsp/>green<nbsp/>means<nbsp/>GO
</alltt>
</codefragment>
</li><li> Lines starting with three minus signs are a special kind of
  labeled list, when the labels are method names and signatures. The
  source in Figure A.1 on page 512 shows a handful of these in
  action.
</li></ul>
<p/>
Indented text that isn't part of a list is set verbatim (such as the
stuff under ``Synopsis'' in Figures A.1 and A.2).
<section>Inline Formatting</section>
<p/>
Within blocks of text and headings, you can use special <em>inline
  sequences</em> to control text formatting. All sequences are nested
within a set of double parentheses.
<p/>
<table>
<th>
  <td><b>Sequence</b></td>
  <td><b>Example</b></td>
  <td><b>Intended Use</b></td>
</th>
<tr>
  <td>((*emphasis*))</td>
  <td><em>emphasis</em></td>
  <td>Emphasis (normally italic)</td>
</tr>
<tr>
  <td>(({code stuff}))</td>
  <td><tt>code stuff</tt></td>
  <td>Code</td>
</tr>
<tr>
  <td>((|variable|))</td>
  <td><em>variable</em></td>
  <td>Variable name</td>
</tr>
<tr>
  <td>((%type me%))</td>
  <td><tt>type me</tt></td>
  <td>Keyboard input</td>
</tr>
<tr>
  <td>((:index term:))</td>
  <td>index term</td>
  <td>Something to be indexed</td>
</tr>
<tr>
  <td>((&lt;reference&gt;))</td>
  <td><u>reference</u></td>
  <td>Hyperlink reference</td>
</tr>
<tr>
  <td>((-footnote-))</td>
  <td>text.<sup>4</sup></td>
  <td>Footnote text. A reference is placed
inline, and the text of the footnote appears at the bottom of the
page.</td>
</tr>
<tr>
  <td>(('verb'))</td>
  <td>verb</td>
  <td>Verbatim text</td>
</tr>
<bottomrule/></table>
<p/>
<section>Cross References</section>
<p/>
The content of headings, the labels of labeled lists, and the names of
methods are automatically
made into potential cross reference targets.  You make links to these
targets from elsewhere in the document by citing their contents in the
<tt>((&lt;...&gt;))</tt> construct.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[  = Synopsis
  ...
  See ((<Return Codes>)) for details.
  ..
  == Instance Methods

  --- Tempfile.open( filename )
      Opens the file...
      
  == Return Codes
  ..
  The method ((<Tempfile.open>)) raises an (({IOException}))...
]]></fullcode>
=<nbsp/>Synopsis
...
See<nbsp/>((&lt;Return<nbsp/>Codes&gt;))<nbsp/>for<nbsp/>details.
..
==<nbsp/>Instance<nbsp/>Methods
<p/>
---<nbsp/>Tempfile.open(<nbsp/>filename<nbsp/>)
<nbsp/><nbsp/><nbsp/><nbsp/>Opens<nbsp/>the<nbsp/>file...
<p/>
==<nbsp/>Return<nbsp/>Codes
..
The<nbsp/>method<nbsp/>((&lt;Tempfile.open&gt;))<nbsp/>raises<nbsp/>an<nbsp/>(({IOException}))...
</alltt>
</codefragment>
<p/>
If a reference starts with ``URL:'', <tt>rdtool</tt> attempts to format it as an
external hyperlink.
<p/>
The reference  <tt>((&lt;display part|label&gt;))</tt> generates a link to
<tt>label</tt> but places the text ``display part'' in the output
document. This is used in the description section of the example in
Figure A.1 on page 512 to generate references to the method names:
<p/>
<codefragment>
<alltt><fullcode><![CDATA[perspective, apart from the unusual ((<(({new}))|Tempfile.new>)),
...
]]></fullcode>
perspective,<nbsp/>apart<nbsp/>from<nbsp/>the<nbsp/>unusual<nbsp/>((&lt;(({new}))|Tempfile.new&gt;)),
...
</alltt>
</codefragment>
<p/>
This construct displays the word ``new'' in code font but uses it as
a hyperlink to the method <tt>Tempfile.new</tt>.
<section>Method Names</section>
<p/>
<tt>rdtool</tt> makes certain assumptions about the format of method
names. Class or module methods should appear as <tt>Class.method</tt>, 
instance methods as <tt>Class#method</tt>, and class or module
constants as <tt>Class::Const</tt>.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[  --- Tempfile::IOWRITE
      Open the file write-only.
      ...
  --- Tempfile.new( filename )
      Constructs a temporary file in the given directory. The file
      ...
  --- Tempfile#open
      Reopens ((|aTempfile|)) using mode ``r+'', which allows reading
      ..
]]></fullcode>
---<nbsp/>Tempfile::IOWRITE
<nbsp/><nbsp/><nbsp/><nbsp/>Open<nbsp/>the<nbsp/>file<nbsp/>write-only.
<nbsp/><nbsp/><nbsp/><nbsp/>...
---<nbsp/>Tempfile.new(<nbsp/>filename<nbsp/>)
<nbsp/><nbsp/><nbsp/><nbsp/>Constructs<nbsp/>a<nbsp/>temporary<nbsp/>file<nbsp/>in<nbsp/>the<nbsp/>given<nbsp/>directory.<nbsp/>The<nbsp/>file
<nbsp/><nbsp/><nbsp/><nbsp/>...
---<nbsp/>Tempfile#open
<nbsp/><nbsp/><nbsp/><nbsp/>Reopens<nbsp/>((|aTempfile|))<nbsp/>using<nbsp/>mode<nbsp/>``r+'',<nbsp/>which<nbsp/>allows<nbsp/>reading
<nbsp/><nbsp/><nbsp/><nbsp/>..
</alltt>
</codefragment>
<section>Including Other Files</section>
<p/>
The contents of <em>filename</em> will be inserted  wherever the
document contains
<p/>
<syntax>
&lt;&lt;&lt; <nt>filename</nt>
</syntax>
<p/>
If the file is specified with an <tt>.rd</tt> or <tt>.rb</tt> extension, it
will be interpreted as RD documentation.
<p/>
If the filename has no extension, <tt>rdtool</tt> will look for a file with an
extension that matches the type of output being produced (<tt>.html</tt>
for HTML files, <tt>.man</tt> for man files, and so on) and interpolate
that file's contents in the <tt>output</tt> stream. Thus, a line such as:
<p/>
<codefragment>
<alltt><fullcode><![CDATA[  <<< header
]]></fullcode>
&lt;&lt;&lt;<nbsp/>header
</alltt>
</codefragment>
<p/>
could be used to add an output-dependent header to a document.
<section>Using rdtool</section>
<p/>
RD documentation can be included directly in a Ruby source program or 
written into a separate file (which by convention will have the
extension <tt>.rd</tt>). These files are processed using the <tt>rd2</tt>
command to produce appropriately formatted output.
<p/>
<syntax>
rd2  <opt><nt>options</nt></opt>  <nt>inputfile</nt>   <opt> &gt;<nt>outputfile</nt> </opt>
</syntax>
<p/>
Some common options include:
<p/>
<table>
<tr>
  <td><tt>-r</tt><em>format</em></td>
  <td>Select an output
  format. <tt>-rrd/rd2html-lib.rb</tt> produces HTML output (the
  default). <tt>-rrd/rd2man-lib.rb</tt> produces Unix man page output.</td>
</tr>
<tr>
  <td><tt>-o</tt><em>name</em></td>
  <td>Set the base part of the output filename.</td>
</tr>
<tr>
  <td><tt>--help</tt></td>
  <td>List the full set of options.</td>
</tr>
</table>
<p/>
<section>Mandatory Disclaimer</section>
<p/>
As we are writing this, RD and <tt>rdtool</tt> are undergoing continuous
development. It is likely that some of the details we give here will
be out of date (or just plain wrong) by the time you read this.
<p/>
Included with the <tt>rdtool</tt> distribution is the file <tt>README.rd</tt>. We
suggest you do so, as it will give you the current scoop on producing
Ruby documentation.
</chapter>
</ppdoc>
