Find file
Fetching contributors…
Cannot retrieve contributors at this time
271 lines (225 sloc) 24.3 KB
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="resources/docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p><strong>Docco</strong> is a quick-and-dirty, hundred-line-long, literate-programming-style
documentation generator. It produces HTML
that displays your comments alongside your code. Comments are passed through
<a href="">Markdown</a>, and code is
passed through <a href="">Pygments</a> syntax highlighting.
This page is the result of running Docco against its own source file.</p>
<p>If you install Docco, you can run it from the command-line:</p>
<pre><code>docco src/*.coffee
<p>...will generate an HTML documentation page for each of the named source files,
with a menu linking to the other pages, saving it into a <code>docs</code> folder.</p>
<p>The <a href="">source for Docco</a> is available on GitHub,
and released under the MIT license.</p>
<p>To install Docco, first make sure you have <a href="">Node.js</a>,
<a href="">Pygments</a> (install the latest dev version of Pygments
from <a href="">its Mercurial repo</a>), and
<a href="">CoffeeScript</a>. Then, with NPM:</p>
<pre><code>sudo npm install -g docco
<p>Docco can be used to process CoffeeScript, JavaScript, Ruby, Python, or TeX files.
Only single-line comments are processed -- block comments are ignored.</p>
<h3>Partners in Crime:</h3>
<li><p>If <strong>Node.js</strong> doesn't run on your platform, or you'd prefer a more
convenient package, get <a href="">Ryan Tomayko</a>'s
<a href="">Rocco</a>, the Ruby port that's
available as a gem. </p></li>
<li><p>If you're writing shell scripts, try
<a href="">Shocco</a>, a port for the <strong>POSIX shell</strong>,
also by Mr. Tomayko.</p></li>
<li><p>If Python's more your speed, take a look at
<a href="">Nick Fitzgerald</a>'s <a href="">Pycco</a>. </p></li>
<li><p>For <strong>Clojure</strong> fans, <a href="">Fogus</a>'s
<a href="">Marginalia</a> is a bit of a departure from
"quick-and-dirty", but it'll get the job done.</p></li>
<li><p><strong>Lua</strong> enthusiasts can get their fix with
<a href="">Robert Gieseke</a>'s <a href="">Locco</a>.</p></li>
<li><p>And if you happen to be a <strong>.NET</strong>
aficionado, check out <a href="">Don Wilson</a>'s
<a href="">Nocco</a>.</p></li>
</ul> </td> <td class="code"> <div class="highlight"><pre>
</pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <h3>Main Documentation Generation Functions</h3> </td> <td class="code"> <div class="highlight"><pre>
</pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Generate the documentation for a source file by reading it in, splitting it
up into comment/code sections, highlighting them for the appropriate language,
and merging them into an HTML template.</p> </td> <td class="code"> <div class="highlight"><pre>generateDocumentation = (source, config, callback) ->
fs.readFile source, (error, buffer) ->
<span class="keyword">throw</span> error <span class="keyword">if</span> error
code = buffer.toString()
sections = parse source, code
highlight source, sections, ->
generateHtml source, sections, config
</pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Given a string of source code, parse out each comment and the code that
follows it, and create an individual <strong>section</strong> for it.
Sections take the form:</p>
docsText: ...
docsHtml: ...
codeText: ...
codeHtml: ...
</code></pre> </td> <td class="code"> <div class="highlight"><pre>parse = (source, code) ->
lines = code.split <span class="string">'\n'</span>
sections = []
language = getLanguage source
hasCode = docsText = codeText = <span class="string">''</span>
save = (docsText, codeText) ->
sections.push {docsText, codeText}
<span class="keyword">for</span> line in lines
<span class="keyword">if</span> line.match(language.commentMatcher) <span class="keyword">and</span> not line.match(language.commentFilter)
<span class="keyword">if</span> hasCode
save docsText, codeText
hasCode = docsText = codeText = <span class="string">''</span>
docsText += line.replace(language.commentMatcher, <span class="string">''</span>) + <span class="string">'\n'</span>
<span class="keyword">else</span>
hasCode = yes
codeText += line + <span class="string">'\n'</span>
save docsText, codeText
</pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Highlights a single chunk of CoffeeScript code, using <strong>Pygments</strong> over stdio,
and runs the text of its corresponding comment through <strong>Markdown</strong>, using
<a href="">Showdown.js</a>.</p>
<p>We process the entire file in a single call to Pygments by inserting little
marker comments between each section and then splitting the result string
wherever our markers occur.</p> </td> <td class="code"> <div class="highlight"><pre>highlight = (source, sections, callback) ->
hl = <span class="keyword">require</span>(<span class="string">"highlight.js"</span>).highlightAuto;
language = getLanguage source
output = <span class="string">""</span>
text = (hl(section.codeText).value <span class="keyword">for</span> section in sections)
output = output.replace(highlightStart, <span class="string">""</span>).replace(highlightEnd, <span class="string">""</span>)
<span class="keyword">for</span> section, i in sections
section.codeHtml = highlightStart + text[i] + highlightEnd
section.docsHtml = showdown.makeHtml(section.docsText)
</pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Once all of the code is finished highlighting, we can generate the HTML file by
passing the completed sections into the template, and then writing the file to
the specified output path.</p> </td> <td class="code"> <div class="highlight"><pre>generateHtml = (source, sections, config) ->
destination = (filepath) ->
path.join(config.output, path.basename(filepath, path.extname(filepath)) + '.html')
title = path.basename source
dest = destination source
html = config.doccoTemplate {
title : title,
sections : sections,
sources : config.sources,
path : path,
destination: destination
css : path.basename(config.css)
console.log "docco: #{source} -> #{dest}"
fs.writeFileSync dest, html
</pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <h3>Helpers &amp; Setup</h3> </td> <td class="code"> <div class="highlight"><pre>
</pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Require our external dependencies, including <strong>Showdown.js</strong>
(the JavaScript implementation of Markdown).</p> </td> <td class="code"> <div class="highlight"><pre>fs = <span class="keyword">require</span> <span class="string">'fs'</span>
path = <span class="keyword">require</span> <span class="string">'path'</span>
showdown = <span class="keyword">require</span>(<span class="string">'./../vendor/showdown'</span>).Showdown
{spawn, exec} = <span class="keyword">require</span> <span class="string">'child_process'</span>
commander = <span class="keyword">require</span> <span class="string">'commander'</span>
</pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Read resource file and return its content.</p> </td> <td class="code"> <div class="highlight"><pre>getResource = (name) ->
fullPath = path.join __dirname, '..', 'resources', name
</pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Languages are stored in JSON format in the file <code>resources/languages.json</code>
Each item maps the file extension to the name of the Pygments lexer and the
symbol that indicates a comment. To add a new language, modify the file.</p> </td> <td class="code"> <div class="highlight"><pre>languages = JSON.parse getResource 'languages.json'
</pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>Build out the appropriate matchers and delimiters for each language.</p> </td> <td class="code"> <div class="highlight"><pre><span class="keyword">for</span> ext, l of languages
</pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Does the line begin with a comment?</p> </td> <td class="code"> <div class="highlight"><pre> l.commentMatcher = <span class="comment">///^\s*#{l.symbol}\s?///</span>
</pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Ignore <a href="">hashbangs</a>
and interpolations...</p> </td> <td class="code"> <div class="highlight"><pre> l.commentFilter = /(^<span class="comment">#![/]|^\s*#\{)/</span>
</pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>The dividing token we feed into Pygments, to delimit the boundaries between
sections.</p> </td> <td class="code"> <div class="highlight"><pre> l.dividerText = "\n#{l.symbol}DIVIDER\n"
</pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>The mirror of <code>dividerText</code> that we expect Pygments to return. We can split
on this to recover the original sections.
Note: the class is "c" for Python and "c1" for the other languages</p> </td> <td class="code"> <div class="highlight"><pre> l.dividerHtml = <span class="comment">///\n*&lt;span\sclass="c1?">#{l.symbol}DIVIDER&lt;\/span>\n*///</span>
</pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Get the current language we're documenting, based on the extension.</p> </td> <td class="code"> <div class="highlight"><pre>getLanguage = (source) -> languages[path.extname(source)]
</pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Ensure that the destination directory exists.</p> </td> <td class="code"> <div class="highlight"><pre>ensureDirectory = (dir, callback) ->
exec "mkdir -p #{dir}", -> callback()
</pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>Micro-templating, originally by John Resig, borrowed by way of
<a href="">Underscore.js</a>.</p> </td> <td class="code"> <div class="highlight"><pre>template = (str) ->
new Function 'obj',
'var p=[],print=function(){p.push.apply(p,arguments);};' +
'with(obj){p.push(\'' +
str.replace(/[\r\t\n]/g, " ")
.replace(/&lt;%=(.+?)%>/g, "',$1,'")
.split('%>').join("p.push('") +
"');}return p.join('');"
</pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>The start of each Pygments highlight block.</p> </td> <td class="code"> <div class="highlight"><pre>highlightStart = '&lt;div class="highlight">&lt;pre>'
</pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>The end of each Pygments highlight block.</p> </td> <td class="code"> <div class="highlight"><pre>highlightEnd = '&lt;/pre>&lt;/div>'
</pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>Extract the docco version from <code>package.json</code></p> </td> <td class="code"> <div class="highlight"><pre>version = JSON.parse(fs.readFileSync("#{__dirname}/../package.json")).version
</pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>Default configuration options.</p> </td> <td class="code"> <div class="highlight"><pre>defaults =
template: "#{__dirname}/../resources/docco.jst"
css : "#{__dirname}/../resources/resources/docco.css"
output : "docs/"
</pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <h3>Run from Commandline</h3> </td> <td class="code"> <div class="highlight"><pre>
</pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <p>Run Docco from a set of command line arguments. </p>
<li>Parse command line using <a href="">Commander JS</a>.</li>
<li>Document sources, or print the usage help if none are specified.</li>
</ol> </td> <td class="code"> <div class="highlight"><pre>run = (args=process.argv) ->
.usage(<span class="string">"[options] &lt;filePattern ...>"</span>)
.option(<span class="string">"-c, --css [file]"</span>,<span class="string">"use a custom css file"</span>,defaults.css)
.option(<span class="string">"-o, --output [path]"</span>,<span class="string">"use a custom output path"</span>,defaults.output)
.option(<span class="string">"-t, --template [file]"</span>,<span class="string">"use a custom .jst template"</span>,defaults.template)
.name = <span class="string">"docco"</span>
<span class="keyword">if</span> commander.args.length
<span class="keyword">else</span>
console.log commander.helpInformation()
</pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <h3>Document Sources</h3> </td> <td class="code"> <div class="highlight"><pre>
</pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">&#182;</a> </div> <p>Run Docco over a list of <code>sources</code> with the given <code>options</code>.</p>
<li>Construct config to use by taking <code>defaults</code> first, then merging in <code>options</code></li>
<li>Generate the resolved source list, filtering out unknown types.</li>
<li>Load the specified template and css files.</li>
<li>Ensure the output path is created, write out the CSS file,
document each source, and invoke the completion callback if it is specified.</li>
</ol> </td> <td class="code"> <div class="highlight"><pre>document = (sources, options = {}, callback = <span class="keyword">null</span>) ->
config = {}
config[key] = defaults[key] <span class="keyword">for</span> key,value of defaults
config[key] = value <span class="keyword">for</span> key,value of options <span class="keyword">if</span> key of defaults
resolved = []
resolved = resolved.concat(resolveSource(src)) <span class="keyword">for</span> src in sources
config.sources = resolved.filter((source) -> getLanguage source).sort()
console.log <span class="string">"docco: skipped unknown type (#{m})"</span> <span class="keyword">for</span> m in resolved when m not in config.sources
config.doccoTemplate = template fs.readFileSync(config.template).toString()
doccoStyles = fs.readFileSync(config.css).toString()
ensureDirectory config.output, ->
fs.writeFileSync path.join(config.output,path.basename(config.css)), doccoStyles
files = config.sources.slice()
nextFile = ->
callback() <span class="keyword">if</span> callback? <span class="keyword">and</span> not files.length
generateDocumentation files.shift(), config, nextFile <span class="keyword">if</span> files.length
</pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <h3>Resolve Wildcard Source Inputs</h3> </td> <td class="code"> <div class="highlight"><pre>
</pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>Resolve a wildcard <code>source</code> input to the files it matches.</p>
<li>If the input contains no wildcard characters, just return it.</li>
<li>Convert the wildcard match to a regular expression, and return
an array of files in the path that match it.</li>
</ol> </td> <td class="code"> <div class="highlight"><pre>resolveSource = (source) ->
<span class="keyword">return</span> source <span class="keyword">if</span> not source.match(/([\*\?])/)
regex_str = path.basename(source)
.replace(/\./g, <span class="string">"\\$&amp;"</span>)
.replace(/\*/,<span class="string">".*"</span>)
.replace(/\?/,<span class="string">"."</span>)
regex = <span class="keyword">new</span> RegExp(<span class="string">'^('</span> + regex_str + <span class="string">')$'</span>)
file_path = path.dirname(source)
files = fs.readdirSync file_path
<span class="keyword">return</span> (path.join(file_path,file) <span class="keyword">for</span> file in files when file.match regex)
</pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <h3>Exports</h3> </td> <td class="code"> <div class="highlight"><pre>
</pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Information about docco, and functions for programatic usage.</p> </td> <td class="code"> <div class="highlight"><pre>exports[key] = value <span class="keyword">for</span> key, value of {
run : run
document : document
parse : parse
resolveSource : resolveSource
version : version
defaults : defaults
languages : languages
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>