346 lines
20 KiB
HTML
346 lines
20 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="content-type" content="text/html;charset=utf-8">
|
||
|
<title>rocco.rb</title>
|
||
|
<link rel="stylesheet" href="http://jashkenas.github.com/docco/resources/docco.css">
|
||
|
</head>
|
||
|
<body>
|
||
|
<div id='container'>
|
||
|
<div id="background"></div>
|
||
|
<table cellspacing=0 cellpadding=0>
|
||
|
<thead>
|
||
|
<tr>
|
||
|
<th class=docs><h1>rocco.rb</h1></th>
|
||
|
<th class=code></th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody>
|
||
|
<tr id='section-1'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-1">#</a>
|
||
|
</div>
|
||
|
<p><strong>Rocco</strong> is a Ruby port of <a href="http://jashkenas.github.com/docco/">Docco</a>, the quick-and-dirty,
|
||
|
hundred-line-long, literate-programming-style documentation generator.</p>
|
||
|
|
||
|
<p>Rocco reads Ruby source files and produces annotated source documentation
|
||
|
in HTML format. Comments are formatted with <a href="http://daringfireball.net/projects/markdown/">Markdown</a> and presented
|
||
|
alongside syntax highlighted code so as to give an annotation effect.
|
||
|
This page is the result of running Rocco against <a href="http://github.com/rtomayko/rocco/blob/master/lib/rocco.rb#commit">its own source file</a>.</p>
|
||
|
|
||
|
<p>Most of this was written while waiting for <a href="http://nodejs.org/">node.js</a> to build (so I
|
||
|
could use Docco!). Docco’s gorgeous HTML and CSS are taken verbatim.
|
||
|
The main difference is that Rocco is written in Ruby instead of
|
||
|
<a href="http://coffeescript.org/">CoffeeScript</a> and may be a bit easier to obtain and install in
|
||
|
existing Ruby environments or where node doesn’t run yet.</p>
|
||
|
|
||
|
<p>Install Rocco with Rubygems:</p>
|
||
|
|
||
|
<pre><code>gem install rocco
|
||
|
</code></pre>
|
||
|
|
||
|
<p>Once installed, the <code>rocco</code> command can be used to generate documentation
|
||
|
for a set of Ruby source files:</p>
|
||
|
|
||
|
<pre><code>rocco lib/*.rb
|
||
|
</code></pre>
|
||
|
|
||
|
<p>The HTML files are written to the current working directory.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-2'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-2">#</a>
|
||
|
</div>
|
||
|
<h3>Prerequisites</h3>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-3'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-3">#</a>
|
||
|
</div>
|
||
|
<p>We’ll need a Markdown library. <a href="http://github.com/rtomayko/rdiscount">RDiscount</a>, if we’re lucky. Otherwise,
|
||
|
issue a warning and fall back on using BlueCloth.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre><span class="k">begin</span>
|
||
|
<span class="nb">require</span> <span class="s1">'rdiscount'</span>
|
||
|
<span class="k">rescue</span> <span class="no">LoadError</span> <span class="o">=></span> <span class="n">boom</span>
|
||
|
<span class="nb">warn</span> <span class="s2">"warn: </span><span class="si">#{</span><span class="n">boom</span><span class="si">}</span><span class="s2">. trying bluecloth"</span>
|
||
|
<span class="nb">require</span> <span class="s1">'bluecloth'</span>
|
||
|
<span class="no">Markdown</span> <span class="o">=</span> <span class="no">BlueCloth</span>
|
||
|
<span class="k">end</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-4'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-4">#</a>
|
||
|
</div>
|
||
|
<p>We use <a href="http://defunkt.github.com/mustache/">{{ mustache }}</a> for
|
||
|
HTML templating.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre><span class="nb">require</span> <span class="s1">'mustache'</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-5'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-5">#</a>
|
||
|
</div>
|
||
|
<p>Code is run through <a href="http://pygments.org/">Pygments</a> for syntax
|
||
|
highlighting. Fail fast right here if we can’t find the <code>pygmentize</code>
|
||
|
program on PATH.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre><span class="k">if</span> <span class="o">!</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">'PATH'</span><span class="o">].</span><span class="n">split</span><span class="p">(</span><span class="s1">':'</span><span class="p">)</span><span class="o">.</span><span class="n">any?</span> <span class="p">{</span> <span class="o">|</span><span class="n">dir</span><span class="o">|</span> <span class="no">File</span><span class="o">.</span><span class="n">exist?</span><span class="p">(</span><span class="s2">"</span><span class="si">#{</span><span class="n">dir</span><span class="si">}</span><span class="s2">/pygmentize"</span><span class="p">)</span> <span class="p">}</span>
|
||
|
<span class="nb">fail</span> <span class="s2">"Pygments is required for syntax highlighting"</span>
|
||
|
<span class="k">end</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-6'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-6">#</a>
|
||
|
</div>
|
||
|
<h3>Public Interface</h3>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-7'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-7">#</a>
|
||
|
</div>
|
||
|
<p><code>Rocco.new</code> takes a source <code>filename</code> and an optional <code>block</code>.
|
||
|
When <code>block</code> is given, it must read the contents of the file using
|
||
|
whatever means necessary and return it as a string. With no <code>block</code>, the
|
||
|
file is read to retrieve data.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre><span class="k">class</span> <span class="nc">Rocco</span>
|
||
|
<span class="no">VERSION</span> <span class="o">=</span> <span class="s1">'0.2'</span>
|
||
|
|
||
|
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="o">&</span><span class="n">block</span><span class="p">)</span>
|
||
|
<span class="vi">@file</span> <span class="o">=</span> <span class="n">filename</span>
|
||
|
<span class="vi">@data</span> <span class="o">=</span>
|
||
|
<span class="k">if</span> <span class="nb">block_given?</span>
|
||
|
<span class="k">yield</span>
|
||
|
<span class="k">else</span>
|
||
|
<span class="no">File</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
|
||
|
<span class="k">end</span>
|
||
|
<span class="vi">@sections</span> <span class="o">=</span> <span class="n">highlight</span><span class="p">(</span><span class="nb">split</span><span class="p">(</span><span class="n">parse</span><span class="p">(</span><span class="vi">@data</span><span class="p">)))</span>
|
||
|
<span class="k">end</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-8'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-8">#</a>
|
||
|
</div>
|
||
|
<p>The filename as given to <code>Rocco.new</code>.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="kp">attr_reader</span> <span class="ss">:file</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-9'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-9">#</a>
|
||
|
</div>
|
||
|
<p>A list of two-tuples representing each <em>section</em> of the source file. Each
|
||
|
item in the list has the form: <code>[docs_html, code_html]</code>, where both
|
||
|
elements are strings containing the documentation and source code HTML,
|
||
|
respectively.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="kp">attr_reader</span> <span class="ss">:sections</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-10'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-10">#</a>
|
||
|
</div>
|
||
|
<p>Generate HTML output for the entire document.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="nb">require</span> <span class="s1">'rocco/layout'</span>
|
||
|
<span class="k">def</span> <span class="nf">to_html</span>
|
||
|
<span class="no">Rocco</span><span class="o">::</span><span class="no">Layout</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="nb">self</span><span class="p">)</span><span class="o">.</span><span class="n">render</span>
|
||
|
<span class="k">end</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-11'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-11">#</a>
|
||
|
</div>
|
||
|
<h3>Internal Parsing and Highlighting</h3>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-12'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-12">#</a>
|
||
|
</div>
|
||
|
<p>Parse the raw file data into a list of two-tuples. Each tuple has the
|
||
|
form <code>[docs, code]</code> where both elements are arrays containing the
|
||
|
raw lines parsed from the input file.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
|
||
|
<span class="n">sections</span> <span class="o">=</span> <span class="o">[]</span>
|
||
|
<span class="n">docs</span><span class="p">,</span> <span class="n">code</span> <span class="o">=</span> <span class="o">[]</span><span class="p">,</span> <span class="o">[]</span>
|
||
|
<span class="n">data</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">line</span><span class="o">|</span>
|
||
|
<span class="k">case</span> <span class="n">line</span>
|
||
|
<span class="k">when</span><span class="sr"> /^\s*#/</span>
|
||
|
<span class="k">if</span> <span class="n">code</span><span class="o">.</span><span class="n">any?</span>
|
||
|
<span class="n">sections</span> <span class="o"><<</span> <span class="o">[</span><span class="n">docs</span><span class="p">,</span> <span class="n">code</span><span class="o">]</span>
|
||
|
<span class="n">docs</span><span class="p">,</span> <span class="n">code</span> <span class="o">=</span> <span class="o">[]</span><span class="p">,</span> <span class="o">[]</span>
|
||
|
<span class="k">end</span>
|
||
|
<span class="n">docs</span> <span class="o"><<</span> <span class="n">line</span>
|
||
|
<span class="k">else</span>
|
||
|
<span class="n">code</span> <span class="o"><<</span> <span class="n">line</span>
|
||
|
<span class="k">end</span>
|
||
|
<span class="k">end</span>
|
||
|
<span class="n">sections</span> <span class="o"><<</span> <span class="o">[</span><span class="n">docs</span><span class="p">,</span> <span class="n">code</span><span class="o">]</span> <span class="k">if</span> <span class="n">docs</span><span class="o">.</span><span class="n">any?</span> <span class="o">||</span> <span class="n">code</span><span class="o">.</span><span class="n">any?</span>
|
||
|
<span class="n">sections</span>
|
||
|
<span class="k">end</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-13'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-13">#</a>
|
||
|
</div>
|
||
|
<p>Take the list of paired <em>sections</em> two-tuples and split into two
|
||
|
separate lists: one holding the comments with leaders removed and
|
||
|
one with the code blocks.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">split</span><span class="p">(</span><span class="n">sections</span><span class="p">)</span>
|
||
|
<span class="n">docs_blocks</span><span class="p">,</span> <span class="n">code_blocks</span> <span class="o">=</span> <span class="o">[]</span><span class="p">,</span> <span class="o">[]</span>
|
||
|
<span class="n">sections</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">docs</span><span class="p">,</span><span class="n">code</span><span class="o">|</span>
|
||
|
<span class="n">docs_blocks</span> <span class="o"><<</span> <span class="n">docs</span><span class="o">.</span><span class="n">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">line</span><span class="o">|</span> <span class="n">line</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sr">/^\s*#\s?/</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span> <span class="p">}</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
|
||
|
<span class="n">code_blocks</span> <span class="o"><<</span> <span class="n">code</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
|
||
|
<span class="k">end</span>
|
||
|
<span class="o">[</span><span class="n">docs_blocks</span><span class="p">,</span> <span class="n">code_blocks</span><span class="o">]</span>
|
||
|
<span class="k">end</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-14'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-14">#</a>
|
||
|
</div>
|
||
|
<p>Take the result of <code>split</code> and apply Markdown formatting to comments and
|
||
|
syntax highlighting to source code.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">highlight</span><span class="p">(</span><span class="n">blocks</span><span class="p">)</span>
|
||
|
<span class="n">docs_blocks</span><span class="p">,</span> <span class="n">code_blocks</span> <span class="o">=</span> <span class="n">blocks</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-15'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-15">#</a>
|
||
|
</div>
|
||
|
<p>Combine all docs blocks into a single big markdown document with section
|
||
|
dividers and run through the Markdown processor. Then split it back out
|
||
|
into separate sections.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="n">markdown</span> <span class="o">=</span> <span class="n">docs_blocks</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"</span><span class="se">\n\n</span><span class="s2">##### DIVIDER</span><span class="se">\n\n</span><span class="s2">"</span><span class="p">)</span>
|
||
|
<span class="n">docs_html</span> <span class="o">=</span> <span class="no">Markdown</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">markdown</span><span class="p">,</span> <span class="ss">:smart</span><span class="p">)</span><span class="o">.</span>
|
||
|
<span class="n">to_html</span><span class="o">.</span>
|
||
|
<span class="nb">split</span><span class="p">(</span><span class="sr">/\n*<h5>DIVIDER<\/h5>\n*/m</span><span class="p">)</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-16'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-16">#</a>
|
||
|
</div>
|
||
|
<p>Combine all code blocks into a single big stream and run through
|
||
|
Pygments. We <code>popen</code> a read/write pygmentize process in the parent and
|
||
|
then fork off a child process to write the input.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="n">code_html</span> <span class="o">=</span> <span class="kp">nil</span>
|
||
|
<span class="nb">open</span><span class="p">(</span><span class="s2">"|pygmentize -l ruby -f html"</span><span class="p">,</span> <span class="s1">'r+'</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">fd</span><span class="o">|</span>
|
||
|
<span class="n">pid</span> <span class="o">=</span>
|
||
|
<span class="nb">fork</span> <span class="p">{</span>
|
||
|
<span class="n">fd</span><span class="o">.</span><span class="n">close_read</span>
|
||
|
<span class="n">fd</span><span class="o">.</span><span class="n">write</span> <span class="n">code_blocks</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"</span><span class="se">\n\n</span><span class="s2"># DIVIDER</span><span class="se">\n\n</span><span class="s2">"</span><span class="p">)</span>
|
||
|
<span class="n">fd</span><span class="o">.</span><span class="n">close_write</span>
|
||
|
<span class="nb">exit!</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="n">fd</span><span class="o">.</span><span class="n">close_write</span>
|
||
|
<span class="n">code_html</span> <span class="o">=</span> <span class="n">fd</span><span class="o">.</span><span class="n">read</span>
|
||
|
<span class="n">fd</span><span class="o">.</span><span class="n">close_read</span>
|
||
|
<span class="no">Process</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="n">pid</span><span class="p">)</span>
|
||
|
<span class="k">end</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-17'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-17">#</a>
|
||
|
</div>
|
||
|
<p>Do some post-processing on the pygments output to split things back
|
||
|
into sections and remove partial <code><pre></code> blocks.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="n">code_html</span> <span class="o">=</span> <span class="n">code_html</span><span class="o">.</span>
|
||
|
<span class="nb">split</span><span class="p">(</span><span class="sr">/\n*<span class="c1"># DIVIDER<\/span>\n*/m</span><span class="p">)</span><span class="o">.</span>
|
||
|
<span class="n">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">code</span><span class="o">|</span> <span class="n">code</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sr">/\n?<div class="highlight"><pre>/m</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span> <span class="p">}</span><span class="o">.</span>
|
||
|
<span class="n">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">code</span><span class="o">|</span> <span class="n">code</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sr">/\n?<\/pre><\/div>\n/m</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span> <span class="p">}</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-18'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-18">#</a>
|
||
|
</div>
|
||
|
<p>Lastly, combine the docs and code lists back into a list of two-tuples.</p>
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre> <span class="n">docs_html</span><span class="o">.</span><span class="n">zip</span><span class="p">(</span><span class="n">code_html</span><span class="p">)</span>
|
||
|
<span class="k">end</span>
|
||
|
<span class="k">end</span></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr id='section-19'>
|
||
|
<td class=docs>
|
||
|
<div class="octowrap">
|
||
|
<a class="octothorpe" href="#section-19">#</a>
|
||
|
</div>
|
||
|
<p>And that’s it.</p>
|
||
|
|
||
|
</td>
|
||
|
<td class=code>
|
||
|
<div class='highlight'><pre></pre></div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
</body>
|