rebuild pages for 0.2 release
This commit is contained in:
parent
0eb0097b3f
commit
bb32ab6d27
345
index.html
Normal file
345
index.html
Normal file
@ -0,0 +1,345 @@
|
|||||||
|
<!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>
|
56
layout.html
Normal file
56
layout.html
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html;charset=utf-8">
|
||||||
|
<title>layout.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>layout.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>
|
||||||
|
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre><span class="nb">require</span> <span class="s1">'mustache'</span>
|
||||||
|
|
||||||
|
<span class="k">class</span> <span class="nc">Rocco</span><span class="o">::</span><span class="no">Layout</span> <span class="o"><</span> <span class="no">Mustache</span>
|
||||||
|
<span class="nb">self</span><span class="o">.</span><span class="n">template_path</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="bp">__FILE__</span><span class="p">)</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
|
||||||
|
<span class="vi">@doc</span> <span class="o">=</span> <span class="n">doc</span>
|
||||||
|
<span class="k">end</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">title</span>
|
||||||
|
<span class="no">File</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="vi">@doc</span><span class="o">.</span><span class="n">file</span><span class="p">)</span>
|
||||||
|
<span class="k">end</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">sections</span>
|
||||||
|
<span class="n">num</span> <span class="o">=</span> <span class="mi">0</span>
|
||||||
|
<span class="vi">@doc</span><span class="o">.</span><span class="n">sections</span><span class="o">.</span><span class="n">map</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="p">{</span>
|
||||||
|
<span class="ss">:docs</span> <span class="o">=></span> <span class="n">docs</span><span class="p">,</span>
|
||||||
|
<span class="ss">:code</span> <span class="o">=></span> <span class="n">code</span><span class="p">,</span>
|
||||||
|
<span class="ss">:num</span> <span class="o">=></span> <span class="p">(</span><span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">)</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
<span class="k">end</span>
|
||||||
|
<span class="k">end</span>
|
||||||
|
<span class="k">end</span></pre></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</body>
|
345
rocco.html
Normal file
345
rocco.html
Normal file
@ -0,0 +1,345 @@
|
|||||||
|
<!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>
|
219
tasks.html
Normal file
219
tasks.html
Normal file
@ -0,0 +1,219 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html;charset=utf-8">
|
||||||
|
<title>tasks.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>tasks.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>
|
||||||
|
<h3>Rocco Rake Tasks</h3>
|
||||||
|
|
||||||
|
<p>To use the Rocco Rake tasks, require <code>rocco/tasks</code> in your <code>Rakefile</code>
|
||||||
|
and define a Rake task with <code>rocco_task</code>. In its simplest form, <code>rocco_task</code>
|
||||||
|
takes the path to a destination directory where HTML docs should be built:</p>
|
||||||
|
|
||||||
|
<pre><code>require 'rocco/tasks'
|
||||||
|
|
||||||
|
desc "Build Rocco Docs"
|
||||||
|
Rocco::make 'docs/'
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
<p>This creates a <code>:rocco</code> rake task, which can then be run with:</p>
|
||||||
|
|
||||||
|
<pre><code>rake rocco
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
<p>It’s a good idea to guard against Rocco not being available, since your
|
||||||
|
Rakefile will fail to load otherwise. Consider doing something like this,
|
||||||
|
so that your Rakefile will still work</p>
|
||||||
|
|
||||||
|
<pre><code>begin
|
||||||
|
require 'rocco/tasks'
|
||||||
|
Rocco::make 'docs/'
|
||||||
|
rescue LoadError
|
||||||
|
warn "#$! -- rocco tasks not loaded."
|
||||||
|
task :rocco
|
||||||
|
end
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
<p>It’s also possible to pass a glob pattern:</p>
|
||||||
|
|
||||||
|
<pre><code>Rocco::make 'html/', 'lib/thing/**/*.rb'
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
<p>Or a list of glob patterns:</p>
|
||||||
|
|
||||||
|
<pre><code>Rocco::make 'html/', ['lib/thing.rb', 'lib/thing/*.rb']
|
||||||
|
</code></pre>
|
||||||
|
</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>
|
||||||
|
<p>Might be nice to defer this until we actually need to build docs but this
|
||||||
|
will have to do for now.</p>
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre><span class="nb">require</span> <span class="s1">'rocco'</span></pre></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr id='section-3'>
|
||||||
|
<td class=docs>
|
||||||
|
<div class="octowrap">
|
||||||
|
<a class="octothorpe" href="#section-3">#</a>
|
||||||
|
</div>
|
||||||
|
<p>Reopen the Rocco class and add a <code>make</code> class method. This is a simple bit
|
||||||
|
of sugar over <code>Rocco::Task.new</code>. If you want your Rake task to be named
|
||||||
|
something other than <code>:rocco</code>, you can use <code>Rocco::Task</code> directly.</p>
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre><span class="k">class</span> <span class="nc">Rocco</span>
|
||||||
|
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">make</span><span class="p">(</span><span class="n">dest</span><span class="o">=</span><span class="s1">'docs/'</span><span class="p">,</span> <span class="n">source_files</span><span class="o">=</span><span class="s1">'lib/**/*.rb'</span><span class="p">)</span>
|
||||||
|
<span class="no">Task</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:rocco</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">source_files</span><span class="p">)</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><code>Rocco::Task.new</code> takes a task name, the destination directory docs
|
||||||
|
should be built under, and a source file pattern or file list.</p>
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre> <span class="k">class</span> <span class="nc">Task</span>
|
||||||
|
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">task_name</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'docs/'</span><span class="p">,</span> <span class="n">sources</span><span class="o">=</span><span class="s1">'lib/**/*.rb'</span><span class="p">)</span>
|
||||||
|
<span class="vi">@name</span> <span class="o">=</span> <span class="n">task_name</span>
|
||||||
|
<span class="vi">@dest</span> <span class="o">=</span> <span class="n">dest</span><span class="o">[-</span><span class="mi">1</span><span class="o">]</span> <span class="o">==</span> <span class="sc">?/</span> <span class="p">?</span> <span class="n">dest</span> <span class="p">:</span> <span class="s2">"</span><span class="si">#{</span><span class="n">dest</span><span class="si">}</span><span class="s2">/"</span>
|
||||||
|
<span class="vi">@sources</span> <span class="o">=</span> <span class="no">FileList</span><span class="o">[</span><span class="n">sources</span><span class="o">]</span></pre></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr id='section-5'>
|
||||||
|
<td class=docs>
|
||||||
|
<div class="octowrap">
|
||||||
|
<a class="octothorpe" href="#section-5">#</a>
|
||||||
|
</div>
|
||||||
|
<p>Make sure there’s a <code>directory</code> task defined for our destination.</p>
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre> <span class="n">define_directory_task</span> <span class="vi">@dest</span></pre></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr id='section-6'>
|
||||||
|
<td class=docs>
|
||||||
|
<div class="octowrap">
|
||||||
|
<a class="octothorpe" href="#section-6">#</a>
|
||||||
|
</div>
|
||||||
|
<p>Run over the source file list, constructing destination filenames
|
||||||
|
and defining file tasks.</p>
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre> <span class="vi">@sources</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">source_file</span><span class="o">|</span>
|
||||||
|
<span class="n">dest_file</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">source_file</span><span class="p">,</span> <span class="s1">'.rb'</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'.html'</span>
|
||||||
|
<span class="n">define_file_task</span> <span class="n">source_file</span><span class="p">,</span> <span class="s2">"</span><span class="si">#{</span><span class="vi">@dest</span><span class="si">}#{</span><span class="n">dest_file</span><span class="si">}</span><span class="s2">"</span></pre></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr id='section-7'>
|
||||||
|
<td class=docs>
|
||||||
|
<div class="octowrap">
|
||||||
|
<a class="octothorpe" href="#section-7">#</a>
|
||||||
|
</div>
|
||||||
|
<p>If <code>rake/clean</code> was required, add the generated files to the list.
|
||||||
|
That way all Rocco generated are removed when running <code>rake clean</code>.</p>
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre> <span class="no">CLEAN</span><span class="o">.</span><span class="n">include</span> <span class="s2">"</span><span class="si">#{</span><span class="vi">@dest</span><span class="si">}#{</span><span class="n">dest_file</span><span class="si">}</span><span class="s2">"</span> <span class="k">if</span> <span class="n">defined?</span> <span class="no">CLEAN</span>
|
||||||
|
<span class="k">end</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>Define the destination directory task and make the <code>:rocco</code> task depend
|
||||||
|
on it. This causes the destination directory to be created if it doesn’t
|
||||||
|
already exist.</p>
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">define_directory_task</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
|
||||||
|
<span class="n">directory</span> <span class="n">path</span>
|
||||||
|
<span class="n">task</span> <span class="vi">@name</span> <span class="o">=></span> <span class="n">path</span>
|
||||||
|
<span class="k">end</span></pre></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr id='section-9'>
|
||||||
|
<td class=docs>
|
||||||
|
<div class="octowrap">
|
||||||
|
<a class="octothorpe" href="#section-9">#</a>
|
||||||
|
</div>
|
||||||
|
<p>Setup a <code>file</code> task for a single Rocco output file (<code>dest_file</code>). It
|
||||||
|
depends on the source file, the destination directory, and all of Rocco’s
|
||||||
|
internal source code, so that the destination file is rebuilt when any of
|
||||||
|
those changes.</p>
|
||||||
|
|
||||||
|
<p>You can run these tasks directly with Rake:</p>
|
||||||
|
|
||||||
|
<pre><code>rake docs/foo.html docs/bar.html
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
<p>… would generate the <code>foo.html</code> and <code>bar.html</code> files but only if they
|
||||||
|
don’t already exist or one of their dependencies was changed.</p>
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">define_file_task</span><span class="p">(</span><span class="n">source_file</span><span class="p">,</span> <span class="n">dest_file</span><span class="p">)</span>
|
||||||
|
<span class="n">prerequisites</span> <span class="o">=</span> <span class="o">[</span><span class="vi">@dest</span><span class="p">,</span> <span class="n">source_file</span><span class="o">]</span> <span class="o">+</span> <span class="n">rocco_source_files</span>
|
||||||
|
<span class="n">file</span> <span class="n">dest_file</span> <span class="o">=></span> <span class="n">prerequisites</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span>
|
||||||
|
<span class="n">verbose</span> <span class="p">{</span> <span class="nb">puts</span> <span class="s2">"rocco: </span><span class="si">#{</span><span class="n">source_file</span><span class="si">}</span><span class="s2"> -> </span><span class="si">#{</span><span class="n">dest_file</span><span class="si">}</span><span class="s2">"</span> <span class="p">}</span>
|
||||||
|
<span class="n">rocco</span> <span class="o">=</span> <span class="no">Rocco</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">source_file</span><span class="p">)</span>
|
||||||
|
<span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">dest_file</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">fd</span><span class="o">|</span> <span class="n">fd</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">rocco</span><span class="o">.</span><span class="n">to_html</span><span class="p">)</span> <span class="p">}</span>
|
||||||
|
<span class="k">end</span>
|
||||||
|
<span class="n">task</span> <span class="vi">@name</span> <span class="o">=></span> <span class="n">dest_file</span>
|
||||||
|
<span class="k">end</span></pre></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr id='section-10'>
|
||||||
|
<td class=docs>
|
||||||
|
<div class="octowrap">
|
||||||
|
<a class="octothorpe" href="#section-10">#</a>
|
||||||
|
</div>
|
||||||
|
<p>Return a <code>FileList</code> that includes all of Roccos source files. This causes
|
||||||
|
output files to be regenerated properly when someone upgrades the Rocco
|
||||||
|
library.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class=code>
|
||||||
|
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">rocco_source_files</span>
|
||||||
|
<span class="n">libdir</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">expand_path</span><span class="p">(</span><span class="s1">'../..'</span><span class="p">,</span> <span class="bp">__FILE__</span><span class="p">)</span>
|
||||||
|
<span class="no">FileList</span><span class="o">[</span><span class="s2">"</span><span class="si">#{</span><span class="n">libdir</span><span class="si">}</span><span class="s2">/rocco.rb"</span><span class="p">,</span> <span class="s2">"</span><span class="si">#{</span><span class="n">libdir</span><span class="si">}</span><span class="s2">/rocco/**"</span><span class="o">]</span>
|
||||||
|
<span class="k">end</span>
|
||||||
|
|
||||||
|
<span class="k">end</span>
|
||||||
|
<span class="k">end</span></pre></div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</body>
|
Loading…
Reference in New Issue
Block a user